-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuth2.js
More file actions
216 lines (171 loc) · 8.73 KB
/
Copy pathOAuth2.js
File metadata and controls
216 lines (171 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
Class:
df.OAuth2
Extends:
df.WebObject
This class handles the initial OAuth 2.0 (http://oauth.net - RFC: http://tools.ietf.org/html/rfc6749)
user login and initial consent grant. It opens a new browser window (users may need to turn off pop-up
blockers) with the vendor's OAuth 2.0 url, passing a number of parameters in the query string. It then
waits for the window to be redirected to the redirect url (generally an empty HTML page) and on detecting
that redirection (by examining the URL of the new windows in a timer loop) it parses the URL for data
embedded in the that. If no error is reported and an Authorisation code or access token is returned (and
also if the passed "state" - in this case just the VDF session cookie - is the same as the returned
"state", to ensure against Cross Site Request Forgery attacks) it will trigger the "LoginDone"
server-side action (which in turn will fire the OnLogin event), otherwise it will trigger the "LoginFail"
server-side action (which in turn will fire the OnLoginFail event).
Most of the action is handled by the vendor's OAuth 2.0 mechanism - we just trigger it and deal with what
it sends us back.
Revision:
2022/05/25 (MJP, UIG)
Upgrade to do things properly with properties and used the dfcc (DataFlex
Custom Components) namespace instead of the core df namespace. Frankly, I've
leaned a lot more about creating DF custom web controls since 2015! :-)
2015/07/15 (MJP, UIG)
Initial version: 1.0
*/
if (!dfcc) {
var dfcc = {};
}
dfcc.OAuthParams = [
{
sParam : df.tString,
sValue : df.tString
}
];
dfcc.OAuth2 = function OAuth2(sName, oParent){
dfcc.OAuth2.base.constructor.call(this, sName, oParent);
// The properties we need:
this.prop(df.tBool, "wpbLoggedIn", false);
this.prop(df.tString, "wpsOAuth2Url", "" );
this.prop(df.tString, "wpsOAuth2LogoutUrl", "" );
this.prop(df.tString, "wpsClientIDName", "" );
this.prop(df.tString, "wpsClientID", "" );
this.prop(df.tString, "wpsRedirectUrlName", "" );
this.prop(df.tString, "wpsRedirectUrl", "" );
this.prop(df.tString, "wpsLogoutRedirName", "" );
this.prop(df.tString, "wpsRespTypeName", "" );
this.prop(df.tString, "wpsResponseType", "" );
this.prop(df.tString, "wpsStateName", "" );
this.prop(df.tInt, "wpiPollInterval, 500" );
this.prop(df.tString, "wpsErrorCode", "" );
this.prop(df.tString, "wpsErrorDesc", "" );
this.prop(df.tString, "wpsAuthCode", "" );
this.prop(df.tInt, "wpiExpiresIn", 0 );
this.prop(df.tString, "wpsRedirectedTo", "" );
this.prop(df.tString, "wpsRetStateName", "" );
this.prop(df.tString, "wpsErrCodeName", "" );
this.prop(df.tString, "wpsErrDescName", "" );
this.prop(df.tAdv, "wptGrant", "" );
this.prop(df.tAdv, "wpatParams", "" );
this.prop(df.tString, "psTest", "");
};
df.defineClass("dfcc.OAuth2", "df.WebObject", {
deserializeParams: df.sys.vt.generateDeserializer(dfcc.OAuthParams),
login: function () {
var state, url, win, open, pollTimer, error, errorDesc, code, retState,
expires, i, params = "", obj = this, aParams = this.deserializeParams(this.wpatParams);
this.psTest = "bar";
state = df.sys.cookie.get("dfWebApp");
for (i = 0; i < aParams.length; i++) {
params += "&" + aParams[i].sParam + "=" + aParams[i].sValue;
}
open = this.wpsOAuth2Url + '?' + this.wpsClientIDName + '=' + this.wpsClientID + '&' +
this.wpsRedirUrlName + '=' + this.wpsRedirectUrl + '&' +
this.wpsRespTypeName + '=' + this.wpsResponseType + '&' +
this.wpsStateName + '=' + state + params;
win = window.open(open, "Login Window", 'width=800, height=800');
var pollTimer = window.setInterval(function() {
try {
if (win.document.URL.indexOf(obj.wpsRedirectUrl) != -1) {
window.clearInterval(pollTimer);
url = win.document.URL;
error = obj.queryValue(url, obj.wpsErrCodeName);
errorDesc = obj.queryValue(url, obj.wpsErrDescName);
code = obj.queryValue(url, obj.wpsAuthCdName);
retState = obj.queryValue(url, obj.wpsRetStateName);
expires = obj.queryValue(url, obj.wpsExpiresName);
win.close();
obj.set("wpsRedirectedTo", url, false);
if (errorDesc !== "" || error !== "" || code === "") {
obj.set("wpsAuthCode", "", false);
obj.set("wpiExpiresIn", 0, false);
obj.set("wpbLoggedIn", false, false);
obj.set("wpsErrorCode", error, false);
obj.set("wpsErrorDesc", errorDesc, false);
obj.serverAction("LoginFail");
}
else {
if (retState !== state) { // Check that the "state" we passed is the same as we got back
obj.set("wpsAuthCode", "", false);
obj.set("wpiExpiresIn", 0, false);
obj.set("wpbLoggedIn", false, false);
obj.set("wpsErrorCode", "CSRF", false);
obj.set("wpsErrorDesc", "Returned state does not match passed state: possible attempted Cross Site Request Forgery attack", false);
obj.serverAction("LoginFail");
}
else {
obj.set("wpsAuthCode", code, false);
obj.set("wpiExpiresIn", expires, false);
obj.set("wpbLoggedIn", true, false);
obj.set("wpsErrorCode", "", false);
obj.set("wpsErrorDesc", "", false);
obj.serverAction("LoginDone");
}
}
}
}
catch(e) {
}
}, this.wpiPollInterval);
},
logout: function() {
var open, win, obj = this;
open = this.wpsOAuth2LogoutUrl + '?' + this.wpsLogoutRedirName + '=' + this.wpsRedirectUrl;
win = window.open(open, "Logout Window", 'width=800, height=800');
var pollTimer = window.setInterval(function () {
try {
if (win.document.URL.indexOf(obj.wpsRedirectUrl) != -1) {
window.clearInterval(pollTimer);
url = win.document.URL;
win.close();
obj.set("wpsRedirectedTo", url, false);
obj.set("wpsAuthCode", "", false);
obj.set("wpbLoggedIn", false, false);
obj.serverAction("LogoutDone");
}
}
catch (e) {
}
}, this.wpiPollInterval);
},
// Finds and returns the value for the passed "name" in the passed "url"
// (or any string, come to that). I've seen this done with a RegExp, but
// it didn't always work, so I am doing it the stupid way.
queryValue: function (url, name) {
var nameLoc, valLoc, ampLoc, hashloc, value;
nameLoc = url.indexOf(name + "=");
if (nameLoc === -1) { // "{name}=" not found in the URL
return "";
}
valLoc = nameLoc + name.length + 1; // + 1 for the "=" sign
ampLoc = url.indexOf('&', nameLoc);
hashLoc = url.indexOf('#', nameLoc); // Some services (i.e. Facebook) use this instead of "?" in places
if ((ampLoc === -1) & (hashLoc === -1)) { // no ampersand or hash found after name position - extract to end
value = url.substr(valLoc);
}
else { // else extract to ampersand or hash
if (ampLoc !== -1) {
if ((hashLoc !== -1) & (hashLoc < ampLoc)) {
value = url.substr(valLoc, (hashLoc - valLoc));
}
else {
value = url.substr(valLoc, (ampLoc - valLoc));
}
}
else if (hashLoc !== -1) {
value = url.substr(valLoc, (hashLoc - valLoc));
}
}
return value;
}
});