@pega/dx-component-builder-sdk 0.1.48 → 0.1.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/auth.js +726 -508
- package/src/tasks/authenticate/index.js +11 -1
- package/src/tasks/build/index.js +4 -4
- package/src/tasks/build-all/index.js +5 -5
- package/src/tasks/bundle/builderAngular/paths.js +1 -0
- package/src/tasks/bundle/builderAssociated/index.js +218 -0
- package/src/tasks/bundle/builderAssociated/modules.js +139 -0
- package/src/tasks/bundle/builderAssociated/paths.js +51 -0
- package/src/tasks/bundle/builderAssociated/webpack.config.js +337 -0
- package/src/tasks/bundle/builderReact/paths.js +2 -1
- package/src/tasks/bundle/index.js +63 -5
- package/src/tasks/list/helper.js +2 -3
- package/src/tasks/override/index.js +17 -17
- package/src/tasks/publish/helper.js +3 -3
- package/src/tasks/publish/index.js +7 -7
- package/src/tasks/publish-all/helper.js +3 -3
- package/src/tasks/publish-all/index.js +9 -6
- package/src/util.js +15 -20
package/src/auth.js
CHANGED
|
@@ -1,573 +1,791 @@
|
|
|
1
|
-
// Below imports needed for when this file is used for a Node apps/Jest tests
|
|
2
|
-
// (can't do conditional imports...so should remove them for regular browser scenarios)
|
|
3
|
-
|
|
4
|
-
import fetch from 'node-fetch';
|
|
5
|
-
import open from 'open';
|
|
6
|
-
import crypto from 'node:crypto';
|
|
7
|
-
import https from 'node:https';
|
|
8
|
-
import http from 'node:http';
|
|
9
|
-
import fs from 'node:fs';
|
|
10
|
-
|
|
11
|
-
|
|
12
1
|
class PegaAuth {
|
|
13
|
-
|
|
14
|
-
if (typeof ssKeyConfig === "string") {
|
|
15
|
-
this.ssKeyConfig = ssKeyConfig;
|
|
16
|
-
this.#reloadConfig();
|
|
17
|
-
} else {
|
|
18
|
-
// object with config structure is passed in
|
|
19
|
-
this.config = ssKeyConfig;
|
|
20
|
-
}
|
|
21
|
-
this.isNode = typeof window === 'undefined';
|
|
22
|
-
this.crypto = this.isNode ? crypto : window.crypto;
|
|
23
|
-
|
|
24
|
-
// Tried both preferring the subtle and the webcrypto but both still yileded different behavior on digest
|
|
2
|
+
#config = null;
|
|
25
3
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
4
|
+
constructor(ssKeyConfig) {
|
|
5
|
+
if (typeof ssKeyConfig === 'string') {
|
|
6
|
+
this.ssKeyConfig = ssKeyConfig;
|
|
7
|
+
this.#reloadConfig();
|
|
8
|
+
} else {
|
|
9
|
+
// object with config structure is passed in
|
|
10
|
+
this.#config = ssKeyConfig;
|
|
11
|
+
}
|
|
12
|
+
this.urlencoded = 'application/x-www-form-urlencoded';
|
|
13
|
+
this.isNode = typeof window === 'undefined';
|
|
14
|
+
|
|
15
|
+
// For isNode path the below attributes are initialized on first method invocation
|
|
16
|
+
if (!this.isNode) {
|
|
17
|
+
this.crypto = window.crypto;
|
|
18
|
+
this.subtle = window.crypto.subtle;
|
|
19
|
+
}
|
|
20
|
+
if (Object.keys(this.#config).length > 0 && !this.#config.serverType) {
|
|
21
|
+
this.#config.serverType = 'infinity';
|
|
22
|
+
}
|
|
31
23
|
}
|
|
32
24
|
|
|
33
25
|
#reloadConfig() {
|
|
34
|
-
|
|
35
|
-
|
|
26
|
+
if (!this.ssKeyConfig) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const peConfig = window.sessionStorage.getItem(this.ssKeyConfig);
|
|
30
|
+
let obj = {};
|
|
31
|
+
if (peConfig) {
|
|
32
|
+
try {
|
|
33
|
+
obj = JSON.parse(peConfig);
|
|
34
|
+
} catch (e) {
|
|
35
|
+
try {
|
|
36
|
+
obj = JSON.parse(atob(peConfig));
|
|
37
|
+
} catch (err) {
|
|
38
|
+
obj = {};
|
|
39
|
+
}
|
|
36
40
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
if (peConfig) {
|
|
40
|
-
try {
|
|
41
|
-
obj = JSON.parse(peConfig);
|
|
42
|
-
} catch (e) {
|
|
43
|
-
try {
|
|
44
|
-
obj = JSON.parse(this.#atob(peConfig));
|
|
45
|
-
} catch (e) {
|
|
46
|
-
obj = {};
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
this.config = peConfig ? obj : {};
|
|
41
|
+
}
|
|
42
|
+
this.#config = peConfig ? obj : {};
|
|
51
43
|
}
|
|
52
44
|
|
|
53
45
|
#updateConfig() {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
46
|
+
if (this.ssKeyConfig) {
|
|
47
|
+
const val = JSON.stringify(this.#config);
|
|
48
|
+
// transform must occur unless it is explicitly disabled
|
|
49
|
+
const transform = this.#config.transform !== false;
|
|
50
|
+
window.sessionStorage.setItem(this.ssKeyConfig, transform ? btoa(val) : val);
|
|
51
|
+
}
|
|
57
52
|
}
|
|
58
53
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
54
|
+
async #importSingleLib(libName, libProp, bLoadAlways = false) {
|
|
55
|
+
// eslint-disable-next-line no-undef
|
|
56
|
+
if (!bLoadAlways && typeof (this.isNode ? global : window)[libProp] !== 'undefined') {
|
|
57
|
+
// eslint-disable-next-line no-undef
|
|
58
|
+
this[libProp] = (this.isNode ? global : window)[libProp];
|
|
59
|
+
return this[libProp];
|
|
60
|
+
}
|
|
61
|
+
// Needed to explicitly make import argument a string by using template literals to fix a compile
|
|
62
|
+
// error: Critical dependency: the request of a dependency is an expression
|
|
63
|
+
return import(`${libName}`)
|
|
64
|
+
.then((mod) => {
|
|
65
|
+
this[libProp] = mod.default;
|
|
66
|
+
})
|
|
67
|
+
.catch((e) => {
|
|
68
|
+
// eslint-disable-next-line no-console
|
|
69
|
+
console.error(`Library ${libName} failed to load. ${e}`);
|
|
70
|
+
throw e;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
70
73
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
+
async #importNodeLibs() {
|
|
75
|
+
// Also current assumption is using Node 18 or better
|
|
76
|
+
// With 18.3 there is now a native fetch (but may want to force use of node-fetch)
|
|
77
|
+
const useNodeFetch = !!this.#config.useNodeFetch;
|
|
78
|
+
|
|
79
|
+
return Promise.all([
|
|
80
|
+
this.#importSingleLib('node-fetch', 'fetch', useNodeFetch),
|
|
81
|
+
this.#importSingleLib('open', 'open'),
|
|
82
|
+
this.#importSingleLib('node:crypto', 'crypto', true),
|
|
83
|
+
this.#importSingleLib('node:https', 'https'),
|
|
84
|
+
this.#importSingleLib('node:http', 'http'),
|
|
85
|
+
this.#importSingleLib('node:fs', 'fs')
|
|
86
|
+
]).then(() => {
|
|
87
|
+
this.subtle = this.crypto?.subtle || this.crypto.webcrypto.subtle;
|
|
88
|
+
if ((typeof fetch === 'undefined' || useNodeFetch) && this.fetch) {
|
|
89
|
+
/* eslint-disable-next-line no-global-assign */
|
|
90
|
+
fetch = this.fetch;
|
|
74
91
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
if (!state) {
|
|
79
|
-
// Calc random state variable
|
|
80
|
-
const buf = new Uint8Array(32);
|
|
81
|
-
this.crypto.getRandomValues(buf);
|
|
82
|
-
state = this.#base64UrlSafeEncode(buf);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Trim alias to include just the real alias piece
|
|
86
|
-
const addtlScope = appAlias ? '+app.alias.' + appAlias.replace(/^app\//, '') : '';
|
|
87
|
-
const scope = (serverType === 'infinity' ? `openid${addtlScope}` : 'user_info');
|
|
88
|
-
|
|
89
|
-
// Add explicit creds if specified to try to avoid login popup
|
|
90
|
-
const moreAuthArgs = (serverType === 'infinity' ?
|
|
91
|
-
'&enable_psyncid=true' +
|
|
92
|
-
(authService ? `&authentication_service=${encodeURIComponent(authService)}` : '') +
|
|
93
|
-
(sessionIndex && this.config.sessionIndexAttempts < 3 ? `&session_index=${sessionIndex}` : '') +
|
|
94
|
-
(userIdentifier
|
|
95
|
-
? `&UserIdentifier=${encodeURIComponent(userIdentifier)}${password ? `&Password=${encodeURIComponent(this.#atob(password))}` : ''}` : '')
|
|
96
|
-
: `&isolationID=${isolationId}`);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
97
94
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
95
|
+
// For PKCE the authorize includes a code_challenge & code_challenge_method as well
|
|
96
|
+
async #buildAuthorizeUrl(state) {
|
|
97
|
+
const {
|
|
98
|
+
serverType,
|
|
99
|
+
clientId,
|
|
100
|
+
redirectUri,
|
|
101
|
+
authorizeUri,
|
|
102
|
+
authService,
|
|
103
|
+
sessionIndex,
|
|
104
|
+
appAlias,
|
|
105
|
+
userIdentifier,
|
|
106
|
+
password,
|
|
107
|
+
noPKCE,
|
|
108
|
+
isolationId
|
|
109
|
+
} = this.#config;
|
|
110
|
+
const bInfinity = serverType === 'infinity';
|
|
111
|
+
|
|
112
|
+
if (!noPKCE) {
|
|
113
|
+
// Generate random string of 64 chars for verifier. RFC 7636 says from 43-128 chars
|
|
114
|
+
const buf = new Uint8Array(64);
|
|
115
|
+
this.crypto.getRandomValues(buf);
|
|
116
|
+
this.#config.codeVerifier = this.#base64UrlSafeEncode(buf);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// If sessionIndex exists then increment attempts count (we will stop sending session_index after two failures)
|
|
120
|
+
// With Infinity '24 we can now properly detect a invalid_session_index error, but can't for earlier versions
|
|
121
|
+
if (sessionIndex) {
|
|
122
|
+
this.#config.sessionIndexAttempts += 1;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// We use state to verify that the received code is for the right authorize transaction
|
|
126
|
+
// eslint-disable-next-line no-unneeded-ternary
|
|
127
|
+
this.#config.state = `${state ? state : ''}.${this.#getRandomString(32)}`;
|
|
128
|
+
|
|
129
|
+
// The same redirectUri needs to be provided to token endpoint, so save this away incase redirectUri is
|
|
130
|
+
// adjusted for next authorize
|
|
131
|
+
this.#config.acRedirectUri = redirectUri;
|
|
132
|
+
|
|
133
|
+
// Persist codeVerifier in session storage so it survives the redirects that are to follow
|
|
134
|
+
this.#updateConfig();
|
|
135
|
+
|
|
136
|
+
// Trim alias to include just the real alias piece
|
|
137
|
+
const addtlScope = appAlias ? `+app.alias.${appAlias.replace(/^app\//, '')}` : '';
|
|
138
|
+
const scope = bInfinity ? `openid${addtlScope}` : 'user_info';
|
|
139
|
+
|
|
140
|
+
// Add explicit creds if specified to try to avoid login popup
|
|
141
|
+
const authServiceArg = authService ? `&authentication_service=${encodeURIComponent(authService)}` : '';
|
|
142
|
+
const sessionIndexArg =
|
|
143
|
+
sessionIndex && this.#config.sessionIndexAttempts < 3 ? `&session_index=${sessionIndex}` : '';
|
|
144
|
+
const userIdentifierArg = userIdentifier ? `&UserIdentifier=${encodeURIComponent(userIdentifier)}` : '';
|
|
145
|
+
const passwordArg = password && userIdentifier ? `&Password=${encodeURIComponent(atob(password))}` : '';
|
|
146
|
+
const moreAuthArgs = bInfinity
|
|
147
|
+
? `&enable_psyncId=true${authServiceArg}${sessionIndexArg}${userIdentifierArg}${passwordArg}`
|
|
148
|
+
: `&isolationID=${isolationId}`;
|
|
149
|
+
|
|
150
|
+
let pkceArgs = '';
|
|
151
|
+
if (!noPKCE) {
|
|
152
|
+
const cc = await this.#getCodeChallenge(this.#config.codeVerifier);
|
|
153
|
+
pkceArgs = `&code_challenge=${cc}&code_challenge_method=S256`;
|
|
154
|
+
}
|
|
155
|
+
return `${authorizeUri}?client_id=${clientId}&response_type=code&redirect_uri=${redirectUri}&scope=${scope}&state=${this.#config.state}${pkceArgs}${moreAuthArgs}`;
|
|
106
156
|
}
|
|
107
157
|
|
|
108
158
|
async login() {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
159
|
+
if (this.isNode && !this.crypto) {
|
|
160
|
+
// Deferring dynamic loading of node libraries til this first method to avoid doing this in constructor
|
|
161
|
+
await this.#importNodeLibs();
|
|
162
|
+
}
|
|
163
|
+
const { grantType, noPKCE } = this.#config;
|
|
164
|
+
if (grantType && grantType !== 'authCode') {
|
|
165
|
+
return this.getToken();
|
|
166
|
+
}
|
|
167
|
+
// Make sure browser in a secure context, else PKCE will fail
|
|
168
|
+
if (!this.isNode && !noPKCE && !window.isSecureContext) {
|
|
169
|
+
throw new Error(
|
|
170
|
+
`Authorization code grant flow failed due to insecure browser context at ${window.location.origin}. Use localhost or https.`
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
return this.#authCodeStart();
|
|
115
174
|
}
|
|
116
175
|
|
|
117
176
|
// authCode login issues the authorize endpoint transaction and deals with redirects
|
|
118
177
|
async #authCodeStart() {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
178
|
+
const fnGetRedirectUriOrigin = () => {
|
|
179
|
+
const redirectUri = this.#config.redirectUri;
|
|
180
|
+
const nRootOffset = redirectUri.indexOf('//');
|
|
181
|
+
const nFirstPathOffset = nRootOffset !== -1 ? redirectUri.indexOf('/', nRootOffset + 2) : -1;
|
|
182
|
+
return nFirstPathOffset !== -1 ? redirectUri.substring(0, nFirstPathOffset) : redirectUri;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const redirectOrigin = fnGetRedirectUriOrigin();
|
|
186
|
+
const state = this.isNode ? '' : btoa(window.location.origin);
|
|
187
|
+
|
|
188
|
+
return new Promise((resolve, reject) => {
|
|
189
|
+
let theUrl = null; // holds the crafted authorize url
|
|
190
|
+
|
|
191
|
+
let myWindow = null; // popup or iframe
|
|
192
|
+
let elIframe = null;
|
|
193
|
+
let elCloseBtn = null;
|
|
194
|
+
const iframeTimeout = this.#config.silentTimeout !== undefined ? this.#config.silentTimeout : 5000;
|
|
195
|
+
let bWinIframe = true;
|
|
196
|
+
let tmrAuthComplete = null;
|
|
197
|
+
let checkWindowClosed = null;
|
|
198
|
+
let bDisablePromptNone = false;
|
|
199
|
+
const myWinOnLoad = () => {
|
|
200
|
+
try {
|
|
201
|
+
if (bWinIframe) {
|
|
202
|
+
elIframe.contentWindow.postMessage({ type: 'PegaAuth' }, redirectOrigin);
|
|
203
|
+
} else {
|
|
204
|
+
myWindow.postMessage({ type: 'PegaAuth' }, redirectOrigin);
|
|
205
|
+
}
|
|
206
|
+
} catch (e) {
|
|
207
|
+
// Exception trying to postMessage on load (perhaps should console.warn)
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
const fnSetSilentAuthFailed = (bSet) => {
|
|
211
|
+
this.#config.silentAuthFailed = bSet;
|
|
212
|
+
this.#updateConfig();
|
|
213
|
+
};
|
|
214
|
+
/* eslint-disable prefer-promise-reject-errors */
|
|
215
|
+
const fnOpenPopup = () => {
|
|
216
|
+
if (this.#config.noPopups) {
|
|
217
|
+
return reject('no-popups');
|
|
218
|
+
}
|
|
219
|
+
// Since displaying a visible window, clear the silent auth failed flag
|
|
220
|
+
fnSetSilentAuthFailed(false);
|
|
221
|
+
myWindow = (this.isNode ? this.open : window.open)(theUrl, '_blank', 'width=700,height=500,left=200,top=100');
|
|
222
|
+
if (!myWindow) {
|
|
223
|
+
// Blocked by popup-blocker
|
|
224
|
+
return reject('blocked');
|
|
225
|
+
}
|
|
226
|
+
checkWindowClosed = setInterval(() => {
|
|
227
|
+
if (myWindow.closed) {
|
|
228
|
+
clearInterval(checkWindowClosed);
|
|
229
|
+
reject('closed');
|
|
230
|
+
}
|
|
231
|
+
}, 500);
|
|
232
|
+
if (!this.isNode) {
|
|
233
|
+
try {
|
|
234
|
+
myWindow.addEventListener('load', myWinOnLoad, true);
|
|
235
|
+
} catch (e) {
|
|
236
|
+
// Exception trying to add onload handler to opened window
|
|
237
|
+
// eslint-disable-next-line no-console
|
|
238
|
+
console.error(`Error adding event listener on popup window: ${e}`);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
/* eslint-enable prefer-promise-reject-errors */
|
|
244
|
+
const fnCloseIframe = () => {
|
|
245
|
+
elIframe.parentNode.removeChild(elIframe);
|
|
246
|
+
elCloseBtn.parentNode.removeChild(elCloseBtn);
|
|
247
|
+
elIframe = null;
|
|
248
|
+
elCloseBtn = null;
|
|
249
|
+
bWinIframe = false;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const fnCloseAndReject = () => {
|
|
253
|
+
fnCloseIframe();
|
|
254
|
+
/* eslint-disable-next-line prefer-promise-reject-errors */
|
|
255
|
+
reject('closed');
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
const fnAuthMessageReceiver = (event) => {
|
|
259
|
+
// Check origin to make sure it is the redirect origin
|
|
260
|
+
if (event.origin !== redirectOrigin) return;
|
|
261
|
+
if (!event.data || !event.data.type || event.data.type !== 'PegaAuth') return;
|
|
262
|
+
const aArgs = ['code', 'state', 'error', 'errorDesc'];
|
|
263
|
+
const aValues = [];
|
|
264
|
+
for (let i = 0; i < aArgs.length; i += 1) {
|
|
265
|
+
const arg = aArgs[i];
|
|
266
|
+
aValues[arg] = event.data[arg] ? event.data[arg].toString() : null;
|
|
267
|
+
}
|
|
268
|
+
if (aValues.error || (aValues.code && aValues.state === this.#config.state)) {
|
|
269
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
270
|
+
fnGetTokenAndFinish(aValues.code, aValues.error, aValues.errorDesc);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
const fnEnableMessageReceiver = (bEnable) => {
|
|
275
|
+
if (bEnable) {
|
|
276
|
+
window.addEventListener('message', fnAuthMessageReceiver, false);
|
|
277
|
+
window.authCodeCallback = (code, state1, error, errorDesc) => {
|
|
278
|
+
if (error || (code && state1 === this.#config.state)) {
|
|
279
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
280
|
+
fnGetTokenAndFinish(code, error, errorDesc);
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
} else {
|
|
284
|
+
window.removeEventListener('message', fnAuthMessageReceiver, false);
|
|
285
|
+
delete window.authCodeCallback;
|
|
286
|
+
}
|
|
124
287
|
};
|
|
125
288
|
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
this.#
|
|
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
|
-
const fnCloseIframe = () => {
|
|
176
|
-
elIframe.parentNode.removeChild(elIframe);
|
|
177
|
-
elCloseBtn.parentNode.removeChild(elCloseBtn);
|
|
178
|
-
elIframe = elCloseBtn = null;
|
|
179
|
-
bWinIframe = false;
|
|
180
|
-
};
|
|
181
|
-
const fnCloseAndReject = () => {
|
|
182
|
-
fnCloseIframe();
|
|
183
|
-
reject("closed");
|
|
184
|
-
};
|
|
185
|
-
let authMessageReceiver = null;
|
|
186
|
-
/* Retrieve token(s) and close login window */
|
|
187
|
-
const fnGetTokenAndFinish = (code) => {
|
|
188
|
-
if (!this.isNode) {
|
|
189
|
-
window.removeEventListener("message", authMessageReceiver, false);
|
|
190
|
-
}
|
|
191
|
-
this.getToken(code).then(token => {
|
|
192
|
-
if (bWinIframe) {
|
|
193
|
-
clearTimeout(tmrAuthComplete);
|
|
194
|
-
fnCloseIframe();
|
|
195
|
-
} else {
|
|
196
|
-
if (!this.isNode) {
|
|
197
|
-
clearInterval(checkWindowClosed);
|
|
198
|
-
myWindow.close();
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
resolve(token);
|
|
202
|
-
})
|
|
203
|
-
.catch(e => {
|
|
204
|
-
reject(e);
|
|
205
|
-
});
|
|
206
|
-
};
|
|
207
|
-
// If there is a userIdentifier and password specified or an external SSO auth service,
|
|
208
|
-
// we can try to use this silently in an iFrame first
|
|
209
|
-
if (bWinIframe) {
|
|
210
|
-
const nFrameZLevel = 99999;
|
|
211
|
-
elIframe = document.createElement('iframe');
|
|
212
|
-
elIframe.id = 'pe' + this.config.clientId;
|
|
213
|
-
const loginBoxWidth = 500,
|
|
214
|
-
loginBoxHeight = 700;
|
|
215
|
-
const oStyle = elIframe.style;
|
|
216
|
-
oStyle.position = 'absolute';
|
|
217
|
-
oStyle.display = 'none';
|
|
218
|
-
oStyle.zIndex = nFrameZLevel;
|
|
219
|
-
oStyle.top = `${Math.round(Math.max(window.innerHeight - loginBoxHeight, 0) / 2)}px`;
|
|
220
|
-
oStyle.left = `${Math.round(Math.max(window.innerWidth - loginBoxWidth, 0) / 2)}px`;
|
|
221
|
-
oStyle.width = '500px';
|
|
222
|
-
oStyle.height = '700px';
|
|
223
|
-
// Add Iframe to top of document DOM to have it load
|
|
224
|
-
document.body.insertBefore(elIframe, document.body.firstChild);
|
|
225
|
-
// document.getElementsByTagName('body')[0].appendChild(elIframe);
|
|
226
|
-
elIframe.addEventListener('load', myWinOnLoad, true);
|
|
227
|
-
// Disallow iframe content attempts to navigate main window
|
|
228
|
-
elIframe.setAttribute('sandbox', 'allow-scripts allow-forms allow-same-origin');
|
|
229
|
-
elIframe.setAttribute('src', url);
|
|
230
|
-
|
|
231
|
-
const svgCloseBtn = `<?xml version="1.0" encoding="UTF-8"?>
|
|
232
|
-
<svg width="34px" height="34px" viewBox="0 0 34 34" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
233
|
-
<title>Dismiss - Black</title>
|
|
234
|
-
<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
|
|
235
|
-
<g transform="translate(1.000000, 1.000000)">
|
|
236
|
-
<circle fill="#252C32" cx="16" cy="16" r="16"></circle>
|
|
237
|
-
<g transform="translate(9.109375, 9.214844)" fill="#FFFFFF" fill-rule="nonzero">
|
|
238
|
-
<path d="M12.7265625,0 L0,12.6210938 L1.0546875,13.5703125 L13.78125,1.0546875 L12.7265625,0 Z M13.7460938,12.5507812 L1.01953125,0 L0,1.01953125 L12.7617188,13.6054688 L13.7460938,12.5507812 Z"></path>
|
|
289
|
+
const doAuthorize = () => {
|
|
290
|
+
// If there is a userIdentifier and password specified or an external SSO auth service,
|
|
291
|
+
// we can try to use this silently in an iFrame first
|
|
292
|
+
bWinIframe =
|
|
293
|
+
!this.isNode &&
|
|
294
|
+
!this.#config.silentAuthFailed &&
|
|
295
|
+
iframeTimeout > 0 &&
|
|
296
|
+
((!!this.#config.userIdentifier && !!this.#config.password) ||
|
|
297
|
+
this.#config.iframeLoginUI ||
|
|
298
|
+
this.#config.authService !== 'pega');
|
|
299
|
+
// Enable message receiver
|
|
300
|
+
if (!this.isNode) {
|
|
301
|
+
fnEnableMessageReceiver(true);
|
|
302
|
+
}
|
|
303
|
+
if (bWinIframe) {
|
|
304
|
+
const nFrameZLevel = 99999;
|
|
305
|
+
elIframe = document.createElement('iframe');
|
|
306
|
+
elIframe.id = `pe${this.#config.clientId}`;
|
|
307
|
+
const loginBoxWidth = 500;
|
|
308
|
+
const loginBoxHeight = 700;
|
|
309
|
+
const oStyle = elIframe.style;
|
|
310
|
+
oStyle.position = 'absolute';
|
|
311
|
+
oStyle.display = 'none';
|
|
312
|
+
oStyle.zIndex = nFrameZLevel;
|
|
313
|
+
oStyle.top = `${Math.round(Math.max(window.innerHeight - loginBoxHeight, 0) / 2)}px`;
|
|
314
|
+
oStyle.left = `${Math.round(Math.max(window.innerWidth - loginBoxWidth, 0) / 2)}px`;
|
|
315
|
+
oStyle.width = '500px';
|
|
316
|
+
oStyle.height = '700px';
|
|
317
|
+
// Add Iframe to top of document DOM to have it load
|
|
318
|
+
document.body.insertBefore(elIframe, document.body.firstChild);
|
|
319
|
+
// document.getElementsByTagName('body')[0].appendChild(elIframe);
|
|
320
|
+
elIframe.addEventListener('load', myWinOnLoad, true);
|
|
321
|
+
// Disallow iframe content attempts to navigate main window
|
|
322
|
+
elIframe.setAttribute('sandbox', 'allow-scripts allow-forms allow-same-origin');
|
|
323
|
+
// Adding prompt=none as this is standard OIDC way to communicate no UI is expected (expecting Pega security to support this one day)
|
|
324
|
+
elIframe.setAttribute('src', bDisablePromptNone ? theUrl : `${theUrl}&prompt=none`);
|
|
325
|
+
|
|
326
|
+
const svgCloseBtn = `<?xml version="1.0" encoding="UTF-8"?>
|
|
327
|
+
<svg width="34px" height="34px" viewBox="0 0 34 34" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
328
|
+
<title>Dismiss - Black</title>
|
|
329
|
+
<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
|
|
330
|
+
<g transform="translate(1.000000, 1.000000)">
|
|
331
|
+
<circle fill="#252C32" cx="16" cy="16" r="16"></circle>
|
|
332
|
+
<g transform="translate(9.109375, 9.214844)" fill="#FFFFFF" fill-rule="nonzero">
|
|
333
|
+
<path d="M12.7265625,0 L0,12.6210938 L1.0546875,13.5703125 L13.78125,1.0546875 L12.7265625,0 Z M13.7460938,12.5507812 L1.01953125,0 L0,1.01953125 L12.7617188,13.6054688 L13.7460938,12.5507812 Z"></path>
|
|
334
|
+
</g>
|
|
335
|
+
</g>
|
|
239
336
|
</g>
|
|
240
|
-
</
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
337
|
+
</svg>`;
|
|
338
|
+
const bCloseWithinFrame = false;
|
|
339
|
+
elCloseBtn = document.createElement('img');
|
|
340
|
+
elCloseBtn.onclick = fnCloseAndReject;
|
|
341
|
+
elCloseBtn.src = `data:image/svg+xml;base64,${btoa(svgCloseBtn)}`;
|
|
342
|
+
const oBtnStyle = elCloseBtn.style;
|
|
343
|
+
oBtnStyle.cursor = 'pointer';
|
|
344
|
+
// If svg doesn't set width and height might want to set oBtStyle width and height to something like '2em'
|
|
345
|
+
oBtnStyle.position = 'absolute';
|
|
346
|
+
oBtnStyle.display = 'none';
|
|
347
|
+
oBtnStyle.zIndex = nFrameZLevel + 1;
|
|
348
|
+
const nTopOffset = bCloseWithinFrame ? 5 : -10;
|
|
349
|
+
const nRightOffset = bCloseWithinFrame ? -34 : -20;
|
|
350
|
+
const nTop = Math.round(Math.max(window.innerHeight - loginBoxHeight, 0) / 2) + nTopOffset;
|
|
351
|
+
oBtnStyle.top = `${nTop}px`;
|
|
352
|
+
const nLeft = Math.round(Math.max(window.innerWidth - loginBoxWidth, 0) / 2) + loginBoxWidth + nRightOffset;
|
|
353
|
+
oBtnStyle.left = `${nLeft}px`;
|
|
354
|
+
document.body.insertBefore(elCloseBtn, document.body.firstChild);
|
|
355
|
+
// If the password was wrong, then the login screen will be in the iframe
|
|
356
|
+
// ..and with Pega without realization of US-372314 it may replace the top (main portal) window
|
|
357
|
+
// For now set a timer and if the timer expires, remove the iFrame and use same url within
|
|
358
|
+
// visible window
|
|
359
|
+
tmrAuthComplete = setTimeout(() => {
|
|
360
|
+
clearTimeout(tmrAuthComplete);
|
|
361
|
+
/*
|
|
362
|
+
// remove password from config
|
|
363
|
+
if (this.#config.password) {
|
|
364
|
+
delete this.#config.password;
|
|
365
|
+
this.#updateConfig();
|
|
366
|
+
}
|
|
367
|
+
*/
|
|
368
|
+
// Display the iframe where the redirects did not succeed (or invoke a popup window)
|
|
369
|
+
if (this.#config.iframeLoginUI) {
|
|
370
|
+
elIframe.style.display = 'block';
|
|
371
|
+
elCloseBtn.style.display = 'block';
|
|
372
|
+
} else {
|
|
373
|
+
fnCloseIframe();
|
|
374
|
+
fnOpenPopup();
|
|
375
|
+
}
|
|
376
|
+
}, iframeTimeout);
|
|
377
|
+
} else {
|
|
378
|
+
if (this.isNode) {
|
|
379
|
+
// Determine port to listen to by extracting it from redirect uri
|
|
380
|
+
const { redirectUri, cert, key } = this.#config;
|
|
381
|
+
const isHttp = redirectUri.startsWith('http:');
|
|
382
|
+
const nLocalhost = redirectUri.indexOf('localhost:');
|
|
383
|
+
const nSlash = redirectUri.indexOf('/', nLocalhost + 10);
|
|
384
|
+
const nPort = parseInt(redirectUri.substring(nLocalhost + 10, nSlash), 10);
|
|
385
|
+
if (nLocalhost !== -1) {
|
|
386
|
+
const options =
|
|
387
|
+
key && cert && !isHttp
|
|
388
|
+
? {
|
|
389
|
+
key: this.fs.readFileSync(key),
|
|
390
|
+
cert: this.fs.readFileSync(cert)
|
|
391
|
+
}
|
|
392
|
+
: {};
|
|
393
|
+
const server = (isHttp ? this.http : this.https).createServer(options, (req, res) => {
|
|
394
|
+
const { winTitle, winBodyHtml } = this.#config;
|
|
395
|
+
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
396
|
+
// Auto closing window for now. Can always leave it up and allow authConfig props to set title and bodyHtml
|
|
397
|
+
res.end(
|
|
398
|
+
`<html><head><title>${winTitle}</title><script>window.close();</script></head><body>${winBodyHtml}</body></html>`
|
|
399
|
+
);
|
|
400
|
+
const queryString = req.url.split('?')[1];
|
|
401
|
+
const urlParams = new URLSearchParams(queryString);
|
|
402
|
+
const code = urlParams.get('code');
|
|
403
|
+
const state1 = urlParams.get('state');
|
|
404
|
+
const error = urlParams.get('error');
|
|
405
|
+
const errorDesc = urlParams.get('error_description');
|
|
406
|
+
if (error || (code && state1 === this.#config.state)) {
|
|
407
|
+
// Stop receiving connections and close when all are handled.
|
|
408
|
+
server.close();
|
|
409
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
410
|
+
fnGetTokenAndFinish(code, error, errorDesc);
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
/* eslint-enable no-undef */
|
|
414
|
+
server.listen(nPort);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
fnOpenPopup();
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
/* Retrieve token(s) and close login window */
|
|
422
|
+
const fnGetTokenAndFinish = (code, error, errorDesc) => {
|
|
423
|
+
// Can clear state in session info at this point
|
|
424
|
+
delete this.#config.state;
|
|
425
|
+
this.#updateConfig();
|
|
426
|
+
|
|
427
|
+
if (!this.isNode) {
|
|
428
|
+
fnEnableMessageReceiver(false);
|
|
429
|
+
if (bWinIframe) {
|
|
430
|
+
clearTimeout(tmrAuthComplete);
|
|
431
|
+
fnCloseIframe();
|
|
432
|
+
} else {
|
|
433
|
+
clearInterval(checkWindowClosed);
|
|
434
|
+
myWindow.close();
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
if (code) {
|
|
438
|
+
this.getToken(code)
|
|
439
|
+
.then((token) => {
|
|
440
|
+
resolve(token);
|
|
441
|
+
})
|
|
442
|
+
.catch((e) => {
|
|
443
|
+
reject(e);
|
|
444
|
+
});
|
|
445
|
+
} else if (error) {
|
|
446
|
+
// Handle some errors in a special manner and pass others back to client
|
|
447
|
+
if (error === 'login_required') {
|
|
448
|
+
// eslint-disable-next-line no-console
|
|
449
|
+
console.warn('silent authentication failed...starting full authentication');
|
|
450
|
+
const bSpecialDebugPath = false;
|
|
451
|
+
if (bSpecialDebugPath) {
|
|
452
|
+
fnSetSilentAuthFailed(false);
|
|
453
|
+
bDisablePromptNone = true;
|
|
454
|
+
} else {
|
|
455
|
+
fnSetSilentAuthFailed(true);
|
|
456
|
+
bDisablePromptNone = false;
|
|
457
|
+
}
|
|
458
|
+
this.#buildAuthorizeUrl(state).then((url) => {
|
|
459
|
+
theUrl = url;
|
|
460
|
+
doAuthorize();
|
|
461
|
+
});
|
|
462
|
+
} else if (error === 'invalid_session_index') {
|
|
463
|
+
// eslint-disable-next-line no-console
|
|
464
|
+
console.warn('auth session no longer valid...starting new session');
|
|
465
|
+
// In these scenarios, not much user can do without just starting a new session, so do that
|
|
466
|
+
this.#updateSessionIndex(null);
|
|
467
|
+
fnSetSilentAuthFailed(false);
|
|
468
|
+
this.#buildAuthorizeUrl(state).then((url) => {
|
|
469
|
+
theUrl = url;
|
|
470
|
+
doAuthorize();
|
|
471
|
+
});
|
|
472
|
+
} else {
|
|
473
|
+
// eslint-disable-next-line no-console
|
|
474
|
+
console.warn(`Authorize failed: ${error}. ${errorDesc}\nFailing authorize url: ${theUrl}`);
|
|
475
|
+
throw new Error(error, { cause: errorDesc });
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
this.#buildAuthorizeUrl(state).then((url) => {
|
|
481
|
+
theUrl = url;
|
|
482
|
+
doAuthorize();
|
|
332
483
|
});
|
|
484
|
+
});
|
|
333
485
|
}
|
|
334
486
|
|
|
335
|
-
//
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
487
|
+
// Login redirect
|
|
488
|
+
loginRedirect() {
|
|
489
|
+
// eslint-disable-next-line no-restricted-globals
|
|
490
|
+
const state = btoa(location.origin);
|
|
491
|
+
this.#buildAuthorizeUrl(state).then((url) => {
|
|
492
|
+
// eslint-disable-next-line no-restricted-globals
|
|
493
|
+
location.href = url;
|
|
494
|
+
});
|
|
495
|
+
}
|
|
342
496
|
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
497
|
+
// Clear session index within config
|
|
498
|
+
#updateSessionIndex(sessionIndex) {
|
|
499
|
+
if (sessionIndex) {
|
|
500
|
+
this.#config.sessionIndex = sessionIndex;
|
|
501
|
+
this.#config.sessionIndexAttempts = 0;
|
|
502
|
+
} else if (this.#config.sessionIndex) {
|
|
503
|
+
delete this.#config.sessionIndex;
|
|
504
|
+
}
|
|
505
|
+
this.#updateConfig();
|
|
506
|
+
}
|
|
349
507
|
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
508
|
+
// For PKCE token endpoint includes code_verifier
|
|
509
|
+
getToken(authCode) {
|
|
510
|
+
// Reload config to pick up the previously stored codeVerifier
|
|
511
|
+
this.#reloadConfig();
|
|
512
|
+
|
|
513
|
+
const {
|
|
514
|
+
serverType,
|
|
515
|
+
isolationId,
|
|
516
|
+
clientId,
|
|
517
|
+
clientSecret,
|
|
518
|
+
acRedirectUri,
|
|
519
|
+
tokenUri,
|
|
520
|
+
codeVerifier,
|
|
521
|
+
grantType,
|
|
522
|
+
sessionIndex,
|
|
523
|
+
customTokenParams,
|
|
524
|
+
userIdentifier,
|
|
525
|
+
password,
|
|
526
|
+
noPKCE
|
|
527
|
+
} = this.#config;
|
|
528
|
+
|
|
529
|
+
const bAuthCode = !grantType || grantType === 'authCode';
|
|
530
|
+
if (bAuthCode && !authCode && !this.isNode) {
|
|
531
|
+
const queryString = window.location.search;
|
|
532
|
+
const urlParams = new URLSearchParams(queryString);
|
|
533
|
+
authCode = urlParams.get('code');
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
const formData = new URLSearchParams();
|
|
537
|
+
formData.append('client_id', clientId);
|
|
538
|
+
if (clientSecret) {
|
|
539
|
+
formData.append('client_secret', clientSecret);
|
|
540
|
+
}
|
|
541
|
+
/* eslint-disable camelcase */
|
|
542
|
+
const fullGTName = {
|
|
543
|
+
authCode: 'authorization_code',
|
|
544
|
+
clientCreds: 'client_credentials',
|
|
545
|
+
customBearer: 'custom-bearer',
|
|
546
|
+
passwordCreds: 'password'
|
|
547
|
+
}[grantType];
|
|
548
|
+
const grant_type = fullGTName || grantType || 'authorization_code';
|
|
549
|
+
formData.append('grant_type', grant_type);
|
|
550
|
+
if (serverType === 'launchpad' && grantType !== 'authCode') {
|
|
551
|
+
formData.append('isolation_ids', isolationId);
|
|
552
|
+
}
|
|
553
|
+
if (bAuthCode) {
|
|
554
|
+
formData.append('code', authCode);
|
|
555
|
+
formData.append('redirect_uri', acRedirectUri);
|
|
556
|
+
if (!noPKCE) {
|
|
557
|
+
formData.append('code_verifier', codeVerifier);
|
|
366
558
|
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
559
|
+
} else if (sessionIndex) {
|
|
560
|
+
formData.append('session_index', sessionIndex);
|
|
561
|
+
}
|
|
562
|
+
/* eslint-enable camelcase */
|
|
563
|
+
if (grantType === 'customBearer' && customTokenParams) {
|
|
564
|
+
Object.keys(customTokenParams).forEach((param) => {
|
|
565
|
+
formData.append(param, customTokenParams[param]);
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
if (grantType !== 'authCode') {
|
|
569
|
+
formData.append('enable_psyncId', 'true');
|
|
570
|
+
}
|
|
571
|
+
if (grantType === 'passwordCreds') {
|
|
572
|
+
formData.append('username', userIdentifier);
|
|
573
|
+
formData.append('password', atob(password));
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
return fetch(tokenUri, {
|
|
577
|
+
agent: this.#getAgent(),
|
|
578
|
+
method: 'POST',
|
|
579
|
+
headers: new Headers({
|
|
580
|
+
'content-type': this.urlencoded
|
|
581
|
+
}),
|
|
582
|
+
|
|
583
|
+
body: formData.toString()
|
|
584
|
+
})
|
|
585
|
+
.then((response) => response.json())
|
|
586
|
+
.then((token) => {
|
|
587
|
+
if (token.errors || token.error) {
|
|
588
|
+
// eslint-disable-next-line no-console
|
|
589
|
+
console.error(`Token endpoint error: ${JSON.stringify(token.errors || token.error)}`);
|
|
590
|
+
} else {
|
|
591
|
+
// .expires_in contains the # of seconds before access token expires
|
|
592
|
+
// add property to keep track of current time when the token expires
|
|
593
|
+
token.eA = Date.now() + token.expires_in * 1000;
|
|
594
|
+
// Clear authCode related config state: state, codeVerifier, acRedirectUri
|
|
595
|
+
if (this.#config.state) {
|
|
596
|
+
delete this.#config.state;
|
|
372
597
|
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
headers: new Headers({
|
|
391
|
-
'content-type': 'application/x-www-form-urlencoded'
|
|
392
|
-
}),
|
|
393
|
-
|
|
394
|
-
body: formData.toString()
|
|
598
|
+
if (this.#config.codeVerifier) {
|
|
599
|
+
delete this.#config.codeVerifier;
|
|
600
|
+
}
|
|
601
|
+
if (this.#config.acRedirectUri) {
|
|
602
|
+
delete this.#config.acRedirectUri;
|
|
603
|
+
}
|
|
604
|
+
// If there is a session_index then move this to the peConfig structure (as used on authorize)
|
|
605
|
+
if (token.session_index) {
|
|
606
|
+
this.#config.sessionIndex = token.session_index;
|
|
607
|
+
}
|
|
608
|
+
// If we got a token and have a session index, then reset the sessionIndexAttempts
|
|
609
|
+
if (this.#config.sessionIndex) {
|
|
610
|
+
this.#config.sessionIndexAttempts = 0;
|
|
611
|
+
}
|
|
612
|
+
this.#updateConfig();
|
|
613
|
+
}
|
|
614
|
+
return token;
|
|
395
615
|
})
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
// console.log(`Token endpoint error: ${JSON.stringify(token.errors ? token.errors : token)}`);
|
|
401
|
-
} else {
|
|
402
|
-
// .expires_in contains the # of seconds before access token expires
|
|
403
|
-
// add property to keep track of current time when the token expires
|
|
404
|
-
/* eslint-disable camelcase */
|
|
405
|
-
token.eA = Date.now() + token.expires_in * 1000;
|
|
406
|
-
if (this.config.codeVerifier) {
|
|
407
|
-
delete this.config.codeVerifier;
|
|
408
|
-
}
|
|
409
|
-
// If there is a session_index then move this to the peConfig structure (as used on authorize)
|
|
410
|
-
if (token.session_index) {
|
|
411
|
-
this.config.sessionIndex = token.session_index;
|
|
412
|
-
}
|
|
413
|
-
// If we got a token and have a session index, then reset the sessionIndexAttempts
|
|
414
|
-
if (this.config.sessionIndex) {
|
|
415
|
-
this.config.sessionIndexAttempts = 0;
|
|
416
|
-
}
|
|
417
|
-
this.#updateConfig();
|
|
418
|
-
}
|
|
419
|
-
return token;
|
|
420
|
-
/* eslint-enable camelcase */
|
|
421
|
-
})
|
|
422
|
-
.catch((e) => {
|
|
423
|
-
console.log(e);
|
|
424
|
-
});
|
|
616
|
+
.catch((e) => {
|
|
617
|
+
// eslint-disable-next-line no-console
|
|
618
|
+
console.error(`Token endpoint error: ${e}`);
|
|
619
|
+
});
|
|
425
620
|
}
|
|
426
621
|
|
|
427
622
|
/* eslint-disable camelcase */
|
|
428
623
|
async refreshToken(refresh_token) {
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
624
|
+
const { clientId, clientSecret, tokenUri } = this.#config;
|
|
625
|
+
|
|
626
|
+
if (this.isNode && !this.crypto) {
|
|
627
|
+
// Deferring dynamic loading of node libraries til this first method to avoid doing this in constructor
|
|
628
|
+
await this.#importNodeLibs();
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
if (!refresh_token) {
|
|
632
|
+
return null;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
const formData = new URLSearchParams();
|
|
636
|
+
formData.append('client_id', clientId);
|
|
637
|
+
if (clientSecret) {
|
|
638
|
+
formData.append('client_secret', clientSecret);
|
|
639
|
+
}
|
|
640
|
+
formData.append('grant_type', 'refresh_token');
|
|
641
|
+
formData.append('refresh_token', refresh_token);
|
|
642
|
+
|
|
643
|
+
return fetch(tokenUri, {
|
|
644
|
+
agent: this.#getAgent(),
|
|
645
|
+
method: 'POST',
|
|
646
|
+
headers: new Headers({
|
|
647
|
+
'content-type': this.urlencoded
|
|
648
|
+
}),
|
|
649
|
+
|
|
650
|
+
body: formData.toString()
|
|
651
|
+
})
|
|
652
|
+
.then((response) => {
|
|
653
|
+
if (!response.ok && response.status === 401) {
|
|
654
|
+
return null;
|
|
655
|
+
}
|
|
656
|
+
return response.json();
|
|
447
657
|
})
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
})
|
|
462
|
-
.catch((e) => {
|
|
463
|
-
console.log(e);
|
|
464
|
-
});
|
|
658
|
+
.then((token) => {
|
|
659
|
+
if (token) {
|
|
660
|
+
// .expires_in contains the # of seconds before access token expires
|
|
661
|
+
// add property to keep track of current time when the token expires
|
|
662
|
+
token.eA = Date.now() + token.expires_in * 1000;
|
|
663
|
+
}
|
|
664
|
+
return token;
|
|
665
|
+
})
|
|
666
|
+
.catch((e) => {
|
|
667
|
+
// eslint-disable-next-line no-console
|
|
668
|
+
console.warn(`Refresh token failed: ${e}`);
|
|
669
|
+
return null;
|
|
670
|
+
});
|
|
465
671
|
}
|
|
466
672
|
|
|
467
673
|
async revokeTokens(access_token, refresh_token = null) {
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
674
|
+
if (Object.keys(this.#config).length === 0) {
|
|
675
|
+
// Must have a config structure to proceed
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
const { clientId, clientSecret, revokeUri } = this.#config;
|
|
679
|
+
|
|
680
|
+
if (this.isNode && !this.crypto) {
|
|
681
|
+
// Deferring dynamic loading of node libraries til this first method to avoid doing this in constructor
|
|
682
|
+
await this.#importNodeLibs();
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
const hdrs = { 'content-type': this.urlencoded };
|
|
686
|
+
if (clientSecret) {
|
|
687
|
+
const basicCreds = btoa(`${clientId}:${clientSecret}`);
|
|
688
|
+
hdrs.authorization = `Basic ${basicCreds}`;
|
|
689
|
+
}
|
|
690
|
+
const aTknProps = ['access_token'];
|
|
691
|
+
if (refresh_token) {
|
|
692
|
+
aTknProps.push('refresh_token');
|
|
693
|
+
}
|
|
694
|
+
aTknProps.forEach((prop) => {
|
|
695
|
+
const formData = new URLSearchParams();
|
|
696
|
+
if (!clientSecret) {
|
|
697
|
+
formData.append('client_id', clientId);
|
|
481
698
|
}
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
699
|
+
formData.append('token', prop === 'access_token' ? access_token : refresh_token);
|
|
700
|
+
formData.append('token_type_hint', prop);
|
|
701
|
+
fetch(revokeUri, {
|
|
702
|
+
agent: this.#getAgent(),
|
|
703
|
+
method: 'POST',
|
|
704
|
+
headers: new Headers(hdrs),
|
|
705
|
+
body: formData.toString()
|
|
706
|
+
})
|
|
707
|
+
.then((response) => {
|
|
708
|
+
if (!response.ok) {
|
|
709
|
+
// eslint-disable-next-line no-console
|
|
710
|
+
console.error(`Error revoking ${prop}:${response.status}`);
|
|
486
711
|
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
if (!response.ok) {
|
|
497
|
-
console.log('Error revoking ' + prop + ':' + response.status);
|
|
498
|
-
}
|
|
499
|
-
})
|
|
500
|
-
.catch((e) => {
|
|
501
|
-
console.log(e);
|
|
502
|
-
});
|
|
503
|
-
});
|
|
504
|
-
// Also clobber any sessionIndex
|
|
505
|
-
if (this.config.sessionIndex) {
|
|
506
|
-
delete this.config.sessionIndex;
|
|
507
|
-
this.#updateConfig();
|
|
508
|
-
}
|
|
712
|
+
})
|
|
713
|
+
.catch((e) => {
|
|
714
|
+
// eslint-disable-next-line no-console
|
|
715
|
+
console.error(`Error revoking ${prop}; ${e}`);
|
|
716
|
+
});
|
|
717
|
+
});
|
|
718
|
+
this.#config.silentAuthFailed = false;
|
|
719
|
+
// Also clobber any sessionIndex
|
|
720
|
+
this.#updateSessionIndex(null);
|
|
509
721
|
}
|
|
510
722
|
/* eslint-enable camelcase */
|
|
511
723
|
|
|
512
724
|
#sha256Hash(str) {
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
/* btoa to work with node or browser - create B64 string*/
|
|
525
|
-
#btoa(str) {
|
|
526
|
-
if (this.isNode) {
|
|
527
|
-
return Buffer.from(str, 'binary').toString('base64');
|
|
528
|
-
} else {
|
|
529
|
-
return window.btoa(str);
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
/* atoa to work with node or browser - convert B64 to binary */
|
|
534
|
-
#atob(str) {
|
|
535
|
-
if (this.isNode) {
|
|
536
|
-
return Buffer.from(str, 'base64').toString('binary');
|
|
537
|
-
} else {
|
|
538
|
-
return window.atob(str);
|
|
539
|
-
}
|
|
725
|
+
// Found that the Node implementation of subtle.digest is yielding incorrect results
|
|
726
|
+
// so using a different set of apis to get expected results.
|
|
727
|
+
if (this.isNode) {
|
|
728
|
+
return new Promise((resolve) => {
|
|
729
|
+
resolve(this.crypto.createHash('sha256').update(str).digest());
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
return this.subtle.digest('SHA-256', new TextEncoder().encode(str));
|
|
540
733
|
}
|
|
541
734
|
|
|
542
735
|
// Base64 encode
|
|
543
736
|
/* eslint-disable-next-line class-methods-use-this */
|
|
544
737
|
#encode64(buff) {
|
|
545
|
-
|
|
738
|
+
return btoa(new Uint8Array(buff).reduce((s, b) => s + String.fromCharCode(b), ''));
|
|
546
739
|
}
|
|
547
740
|
|
|
548
741
|
/*
|
|
549
742
|
* Base64 url safe encoding of an array
|
|
550
743
|
*/
|
|
551
744
|
#base64UrlSafeEncode(buf) {
|
|
552
|
-
|
|
745
|
+
return this.#encode64(buf).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
/*
|
|
749
|
+
* Get Random string starting with buffer of specified size
|
|
750
|
+
*/
|
|
751
|
+
#getRandomString(nSize) {
|
|
752
|
+
const buf = new Uint8Array(nSize);
|
|
753
|
+
this.crypto.getRandomValues(buf);
|
|
754
|
+
return this.#base64UrlSafeEncode(buf);
|
|
553
755
|
}
|
|
554
756
|
|
|
555
757
|
/* Calc code verifier if necessary
|
|
556
758
|
*/
|
|
557
759
|
/* eslint-disable camelcase */
|
|
558
|
-
#getCodeChallenge(code_verifier) {
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
760
|
+
async #getCodeChallenge(code_verifier) {
|
|
761
|
+
return this.#sha256Hash(code_verifier)
|
|
762
|
+
.then((hashed) => {
|
|
763
|
+
return this.#base64UrlSafeEncode(hashed);
|
|
764
|
+
})
|
|
765
|
+
.catch((error) => {
|
|
766
|
+
// eslint-disable-next-line no-console
|
|
767
|
+
console.error(`Error calculation code challenge for PKCE: ${error}`);
|
|
768
|
+
})
|
|
769
|
+
.finally(() => {
|
|
770
|
+
return null;
|
|
771
|
+
});
|
|
569
772
|
}
|
|
570
773
|
/* eslint-enable camelcase */
|
|
571
|
-
}
|
|
572
774
|
|
|
573
|
-
|
|
775
|
+
/*
|
|
776
|
+
* Return agent value for POST commands
|
|
777
|
+
*/
|
|
778
|
+
#getAgent() {
|
|
779
|
+
if (this.isNode && this.#config.ignoreInvalidCerts) {
|
|
780
|
+
const options = {rejectUnauthorized: false};
|
|
781
|
+
if (this.#config.legacyTLS) {
|
|
782
|
+
options.secureOptions = this.crypto.constants.SSL_OP_LEGACY_SERVER_CONNECT;
|
|
783
|
+
}
|
|
784
|
+
return new this.https.Agent(options);
|
|
785
|
+
} else {
|
|
786
|
+
return undefined;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
export default PegaAuth;
|