-
Notifications
You must be signed in to change notification settings - Fork 115
/
web.ts
230 lines (217 loc) · 11.7 KB
/
web.ts
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import {WebPlugin} from '@capacitor/core';
import type {OAuth2AuthenticateOptions, OAuth2ClientPlugin, OAuth2RefreshTokenOptions} from "./definitions";
import {WebOptions, WebUtils} from "./web-utils";
export class OAuth2ClientPluginWeb extends WebPlugin implements OAuth2ClientPlugin {
private webOptions: WebOptions;
private windowHandle: Window | null;
private intervalId: number;
private loopCount = 2000;
private intervalLength = 100;
private windowClosedByPlugin: boolean;
/**
* Get a new access token using an existing refresh token.
*/
async refreshToken(_options: OAuth2RefreshTokenOptions): Promise<any> {
return new Promise<any>((_resolve, reject) => {
reject(new Error("Functionality not implemented for PWAs yet"));
});
}
async authenticate(options: OAuth2AuthenticateOptions): Promise<any> {
const windowOptions = WebUtils.buildWindowOptions(options);
// we open the window first to avoid popups being blocked because of
// the asynchronous buildWebOptions call
this.windowHandle = window.open(
'',
windowOptions.windowTarget,
windowOptions.windowOptions
);
this.webOptions = await WebUtils.buildWebOptions(options);
return new Promise<any>((resolve, reject) => {
// validate
if (!this.webOptions.appId || this.webOptions.appId.length == 0) {
reject(new Error("ERR_PARAM_NO_APP_ID"));
} else if (!this.webOptions.authorizationBaseUrl || this.webOptions.authorizationBaseUrl.length == 0) {
reject(new Error("ERR_PARAM_NO_AUTHORIZATION_BASE_URL"));
} else if (!this.webOptions.redirectUrl || this.webOptions.redirectUrl.length == 0) {
reject(new Error("ERR_PARAM_NO_REDIRECT_URL"));
} else if (!this.webOptions.responseType || this.webOptions.responseType.length == 0) {
reject(new Error("ERR_PARAM_NO_RESPONSE_TYPE"));
} else {
// init internal control params
let loopCount = this.loopCount;
this.windowClosedByPlugin = false;
// open window
const authorizationUrl = WebUtils.getAuthorizationUrl(this.webOptions);
if (this.webOptions.logsEnabled) {
this.doLog("Authorization url: " + authorizationUrl);
}
if (this.windowHandle) {
this.windowHandle.location.href = authorizationUrl;
}
// wait for redirect and resolve the
this.intervalId = window.setInterval(() => {
if (loopCount-- < 0) {
this.closeWindow();
} else if (this.windowHandle?.closed && !this.windowClosedByPlugin) {
window.clearInterval(this.intervalId);
reject(new Error("USER_CANCELLED"));
} else {
let href: string = undefined!;
try {
href = this.windowHandle?.location.href!;
} catch (ignore) {
// ignore DOMException: Blocked a frame with origin "http://localhost:4200" from accessing a cross-origin frame.
}
if (href != null && href.indexOf(this.webOptions.redirectUrl) >= 0) {
if (this.webOptions.logsEnabled) {
this.doLog("Url from Provider: " + href);
}
let authorizationRedirectUrlParamObj = WebUtils.getUrlParams(href);
if (authorizationRedirectUrlParamObj) {
if (this.webOptions.logsEnabled) {
this.doLog("Authorization response:", authorizationRedirectUrlParamObj);
}
window.clearInterval(this.intervalId);
// check state
if (authorizationRedirectUrlParamObj.state === this.webOptions.state) {
if (this.webOptions.accessTokenEndpoint) {
const self = this;
let authorizationCode = authorizationRedirectUrlParamObj.code;
if (authorizationCode) {
const tokenRequest = new XMLHttpRequest();
tokenRequest.onload = function () {
if (this.status === 200) {
let accessTokenResponse = JSON.parse(this.response);
if (self.webOptions.logsEnabled) {
self.doLog("Access token response:", accessTokenResponse);
}
self.requestResource(accessTokenResponse.access_token, resolve, reject, authorizationRedirectUrlParamObj, accessTokenResponse);
}
};
tokenRequest.onerror = function () {
// always log error because of CORS hint
self.doLog("ERR_GENERAL: See client logs. It might be CORS. Status text: " + this.statusText);
reject(new Error("ERR_GENERAL"));
};
tokenRequest.open("POST", this.webOptions.accessTokenEndpoint, true);
tokenRequest.setRequestHeader('accept', 'application/json');
tokenRequest.setRequestHeader('cache-control', 'no-cache');
tokenRequest.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
tokenRequest.send(WebUtils.getTokenEndpointData(this.webOptions, authorizationCode));
} else {
reject(new Error("ERR_NO_AUTHORIZATION_CODE"));
}
this.closeWindow();
} else {
// if no accessTokenEndpoint exists request the resource
this.requestResource(authorizationRedirectUrlParamObj.access_token, resolve, reject, authorizationRedirectUrlParamObj);
}
} else {
if (this.webOptions.logsEnabled) {
this.doLog("State from web options: " + this.webOptions.state);
this.doLog("State returned from provider: " + authorizationRedirectUrlParamObj.state);
}
reject(new Error("ERR_STATES_NOT_MATCH"));
this.closeWindow();
}
}
// this is no error no else clause required
}
}
}, this.intervalLength);
}
});
}
private readonly MSG_RETURNED_TO_JS = "Returned to JS:";
private requestResource(accessToken: string, resolve: any, reject: (reason?: any) => void, authorizationResponse: any, accessTokenResponse: any = null) {
if (this.webOptions.resourceUrl) {
const logsEnabled = this.webOptions.logsEnabled;
if (logsEnabled) {
this.doLog("Resource url: " + this.webOptions.resourceUrl);
}
if (accessToken) {
if (logsEnabled) {
this.doLog("Access token:", accessToken);
}
const self = this;
const request = new XMLHttpRequest();
request.onload = function () {
if (this.status === 200) {
let resp = JSON.parse(this.response);
if (logsEnabled) {
self.doLog("Resource response:", resp);
}
if (resp) {
self.assignResponses(resp, accessToken, authorizationResponse, accessTokenResponse);
}
if (logsEnabled) {
self.doLog(self.MSG_RETURNED_TO_JS, resp);
}
resolve(resp);
} else {
reject(new Error(this.statusText));
}
self.closeWindow();
};
request.onerror = function () {
if (logsEnabled) {
self.doLog("ERR_GENERAL: " + this.statusText);
}
reject(new Error("ERR_GENERAL"));
self.closeWindow();
};
request.open("GET", this.webOptions.resourceUrl, true);
request.setRequestHeader('Authorization', `Bearer ${accessToken}`);
if (this.webOptions.additionalResourceHeaders) {
for (const key in this.webOptions.additionalResourceHeaders) {
request.setRequestHeader(key, this.webOptions.additionalResourceHeaders[key]);
}
}
request.send();
} else {
if (logsEnabled) {
this.doLog("No accessToken was provided although you configured a resourceUrl. Remove the resourceUrl from the config.");
}
reject(new Error("ERR_NO_ACCESS_TOKEN"));
this.closeWindow();
}
} else {
// if no resource url exists just return the accessToken response
const resp = {};
this.assignResponses(resp, accessToken, authorizationResponse, accessTokenResponse);
if (this.webOptions.logsEnabled) {
this.doLog(this.MSG_RETURNED_TO_JS, resp);
}
resolve(resp);
this.closeWindow();
}
}
assignResponses(resp: any, accessToken: string, authorizationResponse: any, accessTokenResponse: any = null): void {
// #154
if (authorizationResponse) {
resp["authorization_response"] = authorizationResponse;
}
if (accessTokenResponse) {
resp["access_token_response"] = accessTokenResponse;
}
resp["access_token"] = accessToken;
}
async logout(options: OAuth2AuthenticateOptions): Promise<boolean> {
return new Promise<any>((resolve, _reject) => {
localStorage.removeItem(WebUtils.getAppId(options));
resolve(true);
});
}
private closeWindow() {
window.clearInterval(this.intervalId);
// #164 if the provider's login page is opened in the same tab or window it must not be closed
// if (this.webOptions.windowTarget !== "_self") {
// this.windowHandle?.close();
// }
this.windowHandle?.close();
this.windowClosedByPlugin = true;
}
private doLog(msg: string, obj: any = null) {
console.log("I/Capacitor/OAuth2ClientPlugin: " + msg, obj);
}
}