@pega/dx-component-builder-sdk 0.1.24 → 0.1.25
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 +2 -1
- package/src/auth.js +573 -0
- package/src/cli.js +1 -1
- package/src/constants.js +32 -1
- package/src/tasks/authenticate/helper.js +91 -8
- package/src/tasks/authenticate/index.js +137 -61
- package/src/tasks/create/index.js +6 -1
- package/src/tasks/create/templates/SDK-React/template/FORM-region/demo.stories.jsx.mustache +183 -0
- package/src/tasks/create/templates/SDK-React/template/FORM-region/mock.stories.js.mustache +132 -0
- package/src/tasks/publish/helper.js +6 -0
- package/src/tasks/publish/index.js +8 -1
- package/src/tasks/publish-all/helper.js +7 -0
- package/src/tasks/publish-all/index.js +8 -1
- package/src/util.js +0 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pega/dx-component-builder-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.25",
|
|
4
4
|
"description": "Utility for building custom UI components",
|
|
5
5
|
"bin": "./bin/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"mustache": "^4.2.0",
|
|
36
36
|
"ncp": "^2.0.0",
|
|
37
37
|
"node-fetch": "^3.2.10",
|
|
38
|
+
"open": "^9.0.0",
|
|
38
39
|
"ora": "^6.1.2",
|
|
39
40
|
"pascalcase": "^2.0.0",
|
|
40
41
|
"postcss": "^8.4.16",
|
package/src/auth.js
ADDED
|
@@ -0,0 +1,573 @@
|
|
|
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
|
+
class PegaAuth {
|
|
13
|
+
constructor(ssKeyConfig) {
|
|
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
|
|
25
|
+
|
|
26
|
+
// Prefer webcrypto.subtle over regular subtle
|
|
27
|
+
// this.subtle = this.isNode ? (crypto.webcrypto ? crypto.webcrypto.subtle : crypto.subtle) : window.crypto.subtle;
|
|
28
|
+
|
|
29
|
+
// Prefer regular subtle over webcrypto.suble for now
|
|
30
|
+
this.subtle = this.isNode ? (crypto.subtle ? crypto.subtle : crypto.webcrypto.subtle) : window.crypto.subtle;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
#reloadConfig() {
|
|
34
|
+
if (!this.ssKeyConfig) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const peConfig = window.sessionStorage.getItem(this.ssKeyConfig);
|
|
38
|
+
let obj = {};
|
|
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 : {};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
#updateConfig() {
|
|
54
|
+
if (this.ssKeyConfig) {
|
|
55
|
+
window.sessionStorage.setItem(this.ssKeyConfig, this.#btoa(JSON.stringify(this.config)));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// For PKCE the authorize includes a code_challenge & code_challenge_method as well
|
|
60
|
+
async #buildAuthorizeUrl(state) {
|
|
61
|
+
const { serverType, clientId, redirectUri, authorizeUri, authService, sessionIndex, appAlias, userIdentifier, password, noPKCE } =
|
|
62
|
+
this.config;
|
|
63
|
+
|
|
64
|
+
if (!noPKCE) {
|
|
65
|
+
// Generate random string of 64 chars for verifier. RFC 7636 says from 43-128 chars
|
|
66
|
+
const buf = new Uint8Array(64);
|
|
67
|
+
this.crypto.getRandomValues(buf);
|
|
68
|
+
this.config.codeVerifier = this.#base64UrlSafeEncode(buf);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// If sessionIndex exists then increment attempts count (we will stop sending session_index after two failures)
|
|
72
|
+
if (sessionIndex) {
|
|
73
|
+
this.config.sessionIndexAttempts += 1;
|
|
74
|
+
}
|
|
75
|
+
// Persist codeVerifier in session storage so it survives the redirects that are to follow
|
|
76
|
+
this.#updateConfig();
|
|
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
|
+
: '');
|
|
97
|
+
|
|
98
|
+
let pkceArgs = '';
|
|
99
|
+
if (!noPKCE) {
|
|
100
|
+
const cc = await this.#getCodeChallenge(this.config.codeVerifier);
|
|
101
|
+
pkceArgs = `&code_challenge=${cc}&code_challenge_method=S256`;
|
|
102
|
+
}
|
|
103
|
+
const authUrl = `${authorizeUri}?client_id=${clientId}&response_type=code&redirect_uri=${redirectUri}&scope=${scope}&state=${state}${pkceArgs}${moreAuthArgs}`;
|
|
104
|
+
// console.log(`authUrl: ${authUrl}`);
|
|
105
|
+
return authUrl;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async login() {
|
|
109
|
+
const { grantType } = this.config;
|
|
110
|
+
if (grantType && grantType !== 'authCode') {
|
|
111
|
+
return this.getToken();
|
|
112
|
+
} else {
|
|
113
|
+
return this.#authCodeStart();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// authCode login issues the authorize endpoint transaction and deals with redirects
|
|
118
|
+
async #authCodeStart() {
|
|
119
|
+
const fnGetRedirectUriOrigin = () => {
|
|
120
|
+
let redirectUri = this.config.redirectUri;
|
|
121
|
+
let nRootOffset = redirectUri.indexOf("//");
|
|
122
|
+
let nFirstPathOffset = nRootOffset !== -1 ? redirectUri.indexOf("/", nRootOffset + 2) : -1;
|
|
123
|
+
return nFirstPathOffset !== -1 ? redirectUri.substring(0, nFirstPathOffset) : redirectUri;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const redirectOrigin = fnGetRedirectUriOrigin();
|
|
127
|
+
const state = this.isNode ? '' : this.#btoa(location.origin);
|
|
128
|
+
|
|
129
|
+
return new Promise((resolve, reject) => {
|
|
130
|
+
/* eslint-disable consistent-return */
|
|
131
|
+
this.#buildAuthorizeUrl(state).then((url) => {
|
|
132
|
+
let myWindow = null; // popup or iframe
|
|
133
|
+
let elIframe = null,
|
|
134
|
+
elCloseBtn = null;
|
|
135
|
+
const iframeTimeout = this.config.silentTimeout !== undefined ? this.config.silentTimeout : 5000;
|
|
136
|
+
let bWinIframe = !this.isNode &&
|
|
137
|
+
iframeTimeout > 0 &&
|
|
138
|
+
((!!this.config.userIdentifier && !!this.config.password) ||
|
|
139
|
+
this.config.iframeLoginUI ||
|
|
140
|
+
this.config.authService !== 'pega');
|
|
141
|
+
let tmrAuthComplete = null,
|
|
142
|
+
checkWindowClosed = null;
|
|
143
|
+
const myWinOnLoad = () => {
|
|
144
|
+
try {
|
|
145
|
+
if (bWinIframe) {
|
|
146
|
+
elIframe.contentWindow.postMessage({ type: "PegaAuth" }, redirectOrigin);
|
|
147
|
+
console.log("authjs(login): loaded a page in iFrame");
|
|
148
|
+
} else {
|
|
149
|
+
myWindow.postMessage({ type: "PegaAuth" }, redirectOrigin);
|
|
150
|
+
}
|
|
151
|
+
} catch (e) {
|
|
152
|
+
console.log("authjs(login): Exception trying to postMessage on load");
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
const fnOpenPopup = () => {
|
|
156
|
+
myWindow = (this.isNode ? open : window.open)(url, '_blank', 'width=700,height=500,left=200,top=100');
|
|
157
|
+
if (!myWindow) {
|
|
158
|
+
// Blocked by popup-blocker
|
|
159
|
+
return reject("blocked");
|
|
160
|
+
}
|
|
161
|
+
checkWindowClosed = setInterval(() => {
|
|
162
|
+
if (myWindow.closed) {
|
|
163
|
+
clearInterval(checkWindowClosed);
|
|
164
|
+
reject("closed");
|
|
165
|
+
}
|
|
166
|
+
}, 500);
|
|
167
|
+
if (!this.isNode) {
|
|
168
|
+
try {
|
|
169
|
+
myWindow.addEventListener("load", myWinOnLoad, true);
|
|
170
|
+
} catch (e) {
|
|
171
|
+
console.log("authjs(login): Exception trying to add onload handler to opened window;");
|
|
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>
|
|
239
|
+
</g>
|
|
240
|
+
</g>
|
|
241
|
+
</g>
|
|
242
|
+
</svg>`;
|
|
243
|
+
const bCloseWithinFrame = false;
|
|
244
|
+
elCloseBtn = document.createElement('img');
|
|
245
|
+
elCloseBtn.onclick = fnCloseAndReject;
|
|
246
|
+
elCloseBtn.src = `data:image/svg+xml;base64,${this.#btoa(svgCloseBtn)}`;
|
|
247
|
+
const oBtnStyle = elCloseBtn.style;
|
|
248
|
+
oBtnStyle.cursor = 'pointer';
|
|
249
|
+
// If svg doesn't set width and height might want to set oBtStyle width and height to something like '2em'
|
|
250
|
+
oBtnStyle.position = 'absolute';
|
|
251
|
+
oBtnStyle.display = 'none';
|
|
252
|
+
oBtnStyle.zIndex = nFrameZLevel + 1;
|
|
253
|
+
const nTopOffset = bCloseWithinFrame ? 5 : -10;
|
|
254
|
+
const nRightOffset = bCloseWithinFrame ? -34 : -20;
|
|
255
|
+
oBtnStyle.top = `${Math.round(Math.max(window.innerHeight - loginBoxHeight, 0) / 2) + nTopOffset}px`;
|
|
256
|
+
oBtnStyle.left = `${Math.round(Math.max(window.innerWidth - loginBoxWidth, 0) / 2) + loginBoxWidth + nRightOffset
|
|
257
|
+
}px`;
|
|
258
|
+
document.body.insertBefore(elCloseBtn, document.body.firstChild);
|
|
259
|
+
// If the password was wrong, then the login screen will be in the iframe
|
|
260
|
+
// ..and with Pega without realization of US-372314 it may replace the top (main portal) window
|
|
261
|
+
// For now set a timer and if the timer expires, remove the iFrame and use same url within
|
|
262
|
+
// visible window
|
|
263
|
+
tmrAuthComplete = setTimeout(() => {
|
|
264
|
+
clearTimeout(tmrAuthComplete);
|
|
265
|
+
// remove password from config
|
|
266
|
+
if (this.config.password) {
|
|
267
|
+
delete this.config.password;
|
|
268
|
+
this.#updateConfig();
|
|
269
|
+
}
|
|
270
|
+
// Display the iframe where the redirects did not succeed (or invoke a popup window)
|
|
271
|
+
if (this.config.iframeLoginUI) {
|
|
272
|
+
elIframe.style.display = 'block';
|
|
273
|
+
elCloseBtn.style.display = 'block';
|
|
274
|
+
} else {
|
|
275
|
+
fnCloseIframe();
|
|
276
|
+
fnOpenPopup();
|
|
277
|
+
}
|
|
278
|
+
}, iframeTimeout);
|
|
279
|
+
} else {
|
|
280
|
+
if (this.isNode) {
|
|
281
|
+
// Determine port to listen to by extracting it from redirect uri
|
|
282
|
+
const { redirectUri, cert, key } = this.config;
|
|
283
|
+
const isHttp = redirectUri.startsWith('http:');
|
|
284
|
+
const nLocalhost = redirectUri.indexOf("localhost:");
|
|
285
|
+
const nSlash = redirectUri.indexOf('/', nLocalhost + 10);
|
|
286
|
+
const nPort = parseInt(redirectUri.substring(nLocalhost + 10, nSlash));
|
|
287
|
+
if (-1 !== nLocalhost) {
|
|
288
|
+
const options = isHttp ? {} : {
|
|
289
|
+
key: fs.readFileSync(key),
|
|
290
|
+
cert: fs.readFileSync(cert)
|
|
291
|
+
};
|
|
292
|
+
const server = (isHttp ? http : https).createServer(options, (req, res) => {
|
|
293
|
+
const { method, url } = req;
|
|
294
|
+
const { winTitle, winBodyHtml } = this.config;
|
|
295
|
+
// console.log(`HTTP request received: ${url}`);
|
|
296
|
+
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
297
|
+
// Auto closing window for now. Can always leave it up and allow authConfig props to set title and bodyHtml
|
|
298
|
+
res.end(`<html><head><title>${winTitle}</title><script>window.close();</script></head><body>${winBodyHtml}</body></html>`);
|
|
299
|
+
const queryString = url.split('?')[1];
|
|
300
|
+
const urlParams = new URLSearchParams(queryString);
|
|
301
|
+
const code = urlParams.get('code');
|
|
302
|
+
if (code) {
|
|
303
|
+
// console.log(`Auth code received: ${code}`);
|
|
304
|
+
// Stop receiving connections and close when all are handled.
|
|
305
|
+
server.close();
|
|
306
|
+
fnGetTokenAndFinish(code);
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
server.listen(nPort);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
fnOpenPopup();
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (!this.isNode) {
|
|
316
|
+
/* Handler to receive the auth code */
|
|
317
|
+
authMessageReceiver = (event) => {
|
|
318
|
+
// Check origin to make sure it is the redirect origin
|
|
319
|
+
if (event.origin !== redirectOrigin) return;
|
|
320
|
+
if (!event.data || !event.data.type || event.data.type !== 'PegaAuth') return;
|
|
321
|
+
console.log('authjs(login): postMessage received with code');
|
|
322
|
+
const code = event.data.code.toString();
|
|
323
|
+
fnGetTokenAndFinish(code);
|
|
324
|
+
};
|
|
325
|
+
window.addEventListener('message', authMessageReceiver, false);
|
|
326
|
+
window.authCodeCallback = function (code) {
|
|
327
|
+
console.log('authjs(login): authCodeCallback used with code');
|
|
328
|
+
fnGetTokenAndFinish(code);
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// For PKCE token endpoint includes code_verifier
|
|
336
|
+
getToken(authCode) {
|
|
337
|
+
// Reload config to pick up the previously stored codeVerifier
|
|
338
|
+
this.#reloadConfig();
|
|
339
|
+
|
|
340
|
+
const { serverType, isolationId, clientId, clientSecret, redirectUri, tokenUri, codeVerifier, grantType,
|
|
341
|
+
sessionIndex, customTokenParams, userIdentifier, password, noPKCE, ignoreInvalidCerts } = this.config;
|
|
342
|
+
|
|
343
|
+
const bAuthCode = !grantType || grantType === 'authCode';
|
|
344
|
+
if (bAuthCode && !authCode && !this.isNode) {
|
|
345
|
+
const queryString = window.location.search;
|
|
346
|
+
const urlParams = new URLSearchParams(queryString);
|
|
347
|
+
authCode = urlParams.get('code');
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const formData = new URLSearchParams();
|
|
351
|
+
formData.append('client_id', clientId);
|
|
352
|
+
if (clientSecret) {
|
|
353
|
+
formData.append('client_secret', clientSecret);
|
|
354
|
+
}
|
|
355
|
+
const fullGTName = {
|
|
356
|
+
"authCode": "authorization_code",
|
|
357
|
+
"clientCreds": "client_credentials",
|
|
358
|
+
"customBearer": "custom-bearer",
|
|
359
|
+
"passwordCreds": "password"
|
|
360
|
+
}[grantType];
|
|
361
|
+
const grant_type = fullGTName ? fullGTName :
|
|
362
|
+
(grantType ? grantType : 'authorization_code');
|
|
363
|
+
formData.append('grant_type', grant_type);
|
|
364
|
+
if (serverType === 'launchpad' && grantType !== 'authCode') {
|
|
365
|
+
formData.append('isolation_ids', isolationId);
|
|
366
|
+
}
|
|
367
|
+
if (bAuthCode) {
|
|
368
|
+
formData.append('code', authCode);
|
|
369
|
+
formData.append('redirect_uri', redirectUri);
|
|
370
|
+
if (!noPKCE) {
|
|
371
|
+
formData.append('code_verifier', codeVerifier);
|
|
372
|
+
}
|
|
373
|
+
} else if (sessionIndex) {
|
|
374
|
+
formData.append('session_index', sessionIndex);
|
|
375
|
+
}
|
|
376
|
+
if (grantType === 'customBearer' && customTokenParams) {
|
|
377
|
+
Object.keys(customTokenParams).forEach((param) => {
|
|
378
|
+
formData.append(param, customTokenParams[param]);
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
if (grantType === 'passwordCreds') {
|
|
382
|
+
formData.append('username', userIdentifier);
|
|
383
|
+
formData.append('password', this.#atob(password));
|
|
384
|
+
}
|
|
385
|
+
// console.log(`getToken form data: ${formData.toString()}`);
|
|
386
|
+
|
|
387
|
+
return fetch(tokenUri, {
|
|
388
|
+
agent: this.isNode && ignoreInvalidCerts ? new https.Agent({ rejectUnauthorized: false }) : undefined,
|
|
389
|
+
method: 'POST',
|
|
390
|
+
headers: new Headers({
|
|
391
|
+
'content-type': 'application/x-www-form-urlencoded'
|
|
392
|
+
}),
|
|
393
|
+
|
|
394
|
+
body: formData.toString()
|
|
395
|
+
})
|
|
396
|
+
.then((response) => response.json())
|
|
397
|
+
.then((token) => {
|
|
398
|
+
if (token.errors || token.error) {
|
|
399
|
+
// console.log(`formData: ${formData.toString()}`);
|
|
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
|
+
});
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/* eslint-disable camelcase */
|
|
428
|
+
async refreshToken(refresh_token) {
|
|
429
|
+
const { clientId, clientSecret, tokenUri, ignoreInvalidCerts } = this.config;
|
|
430
|
+
|
|
431
|
+
const formData = new URLSearchParams();
|
|
432
|
+
formData.append('client_id', clientId);
|
|
433
|
+
if (clientSecret) {
|
|
434
|
+
formData.append('client_secret', clientSecret);
|
|
435
|
+
}
|
|
436
|
+
formData.append('grant_type', 'refresh_token');
|
|
437
|
+
formData.append('refresh_token', refresh_token);
|
|
438
|
+
|
|
439
|
+
return fetch(tokenUri, {
|
|
440
|
+
agent: this.isNode && ignoreInvalidCerts ? new https.Agent({ rejectUnauthorized: false }) : undefined,
|
|
441
|
+
method: 'POST',
|
|
442
|
+
headers: new Headers({
|
|
443
|
+
'content-type': 'application/x-www-form-urlencoded'
|
|
444
|
+
}),
|
|
445
|
+
|
|
446
|
+
body: formData.toString()
|
|
447
|
+
})
|
|
448
|
+
.then((response) => {
|
|
449
|
+
if (!response.ok && 401 === response.status) {
|
|
450
|
+
return null;
|
|
451
|
+
}
|
|
452
|
+
return response.json();
|
|
453
|
+
})
|
|
454
|
+
.then((token) => {
|
|
455
|
+
if (token) {
|
|
456
|
+
// .expires_in contains the # of seconds before access token expires
|
|
457
|
+
// add property to keep track of current time when the token expires
|
|
458
|
+
token.eA = Date.now() + token.expires_in * 1000;
|
|
459
|
+
}
|
|
460
|
+
return token;
|
|
461
|
+
})
|
|
462
|
+
.catch((e) => {
|
|
463
|
+
console.log(e);
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
async revokeTokens(access_token, refresh_token = null) {
|
|
468
|
+
if (Object.keys(this.config).length === 0) {
|
|
469
|
+
// Must have a config structure to proceed
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
const { clientId, clientSecret, revokeUri, ignoreInvalidCerts } = this.config;
|
|
473
|
+
|
|
474
|
+
let hdrs = { 'content-type': 'application/x-www-form-urlencoded' };
|
|
475
|
+
if (clientSecret) {
|
|
476
|
+
hdrs['authorization'] = 'Basic ' + this.#btoa(clientId + ':' + clientSecret);
|
|
477
|
+
}
|
|
478
|
+
let aTknProps = ['access_token'];
|
|
479
|
+
if (refresh_token) {
|
|
480
|
+
aTknProps.push('refresh_token');
|
|
481
|
+
}
|
|
482
|
+
aTknProps.forEach((prop) => {
|
|
483
|
+
const formData = new URLSearchParams();
|
|
484
|
+
if (!clientSecret) {
|
|
485
|
+
formData.append('client_id', clientId);
|
|
486
|
+
}
|
|
487
|
+
formData.append('token', prop === 'access_token' ? access_token : refresh_token);
|
|
488
|
+
formData.append('token_type_hint', prop);
|
|
489
|
+
fetch(revokeUri, {
|
|
490
|
+
agent: this.isNode && ignoreInvalidCerts ? new https.Agent({ rejectUnauthorized: false }) : undefined,
|
|
491
|
+
method: 'POST',
|
|
492
|
+
headers: new Headers(hdrs),
|
|
493
|
+
body: formData.toString()
|
|
494
|
+
})
|
|
495
|
+
.then((response) => {
|
|
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
|
+
}
|
|
509
|
+
}
|
|
510
|
+
/* eslint-enable camelcase */
|
|
511
|
+
|
|
512
|
+
#sha256Hash(str) {
|
|
513
|
+
// Found that the Node implementation of subtle.digest is yielding incorrect results
|
|
514
|
+
// so using a different set of apis to get expected results.
|
|
515
|
+
if (this.isNode) {
|
|
516
|
+
return new Promise((resolve) => {
|
|
517
|
+
return resolve(this.crypto.createHash("sha256").update(str).digest());
|
|
518
|
+
});
|
|
519
|
+
} else {
|
|
520
|
+
return this.subtle.digest('SHA-256', new TextEncoder().encode(str));
|
|
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
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// Base64 encode
|
|
543
|
+
/* eslint-disable-next-line class-methods-use-this */
|
|
544
|
+
#encode64(buff) {
|
|
545
|
+
return this.#btoa(new Uint8Array(buff).reduce((s, b) => s + String.fromCharCode(b), ''));
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/*
|
|
549
|
+
* Base64 url safe encoding of an array
|
|
550
|
+
*/
|
|
551
|
+
#base64UrlSafeEncode(buf) {
|
|
552
|
+
return this.#encode64(buf).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/* Calc code verifier if necessary
|
|
556
|
+
*/
|
|
557
|
+
/* eslint-disable camelcase */
|
|
558
|
+
#getCodeChallenge(code_verifier) {
|
|
559
|
+
return this.#sha256Hash(code_verifier)
|
|
560
|
+
.then((hashed) => {
|
|
561
|
+
return this.#base64UrlSafeEncode(hashed);
|
|
562
|
+
})
|
|
563
|
+
.catch((error) => {
|
|
564
|
+
console.log(error);
|
|
565
|
+
})
|
|
566
|
+
.finally(() => {
|
|
567
|
+
return null;
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
/* eslint-enable camelcase */
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
export default PegaAuth;
|
package/src/cli.js
CHANGED
package/src/constants.js
CHANGED
|
@@ -36,9 +36,40 @@ export const LIST_COMPONENT_SERVICE_REST_ENDPOINT = 'PRRestService/dev/v1/list_c
|
|
|
36
36
|
export const OOTB_COMPONENT_SERVICE_REST_ENDPOINT = 'PRRestService/dev/v1/ootb_component';
|
|
37
37
|
export const DELETE_COMPONENT_SERVICE_REST_ENDPOINT = 'PRRestService/dev/v1/delete_component';
|
|
38
38
|
export const TOKEN_URL_V1 = 'PRRestService/oauth2/v1/token';
|
|
39
|
-
|
|
39
|
+
export const AUTHORIZE_URL_V1 = 'PRRestService/oauth2/v1/authorize';
|
|
40
|
+
export const LP_TOKEN_URL = 'uas/oauth/token';
|
|
41
|
+
export const LP_AUTHORIZE_URL = 'uas/oauth/authorize';
|
|
40
42
|
export const SERVER_CONFIG = "serverConfig";
|
|
43
|
+
export const SERVER_TYPE = "serverType";
|
|
41
44
|
export const SERVER_REST_URL = "infinityRestServerUrl";
|
|
45
|
+
export const LP_SERVER_REST_URL = "launchpadRestServerUrl";
|
|
46
|
+
|
|
47
|
+
export const AUTHENTICATE_SCHEMA = {
|
|
48
|
+
serverType: [
|
|
49
|
+
{
|
|
50
|
+
name: 'Infinity',
|
|
51
|
+
value: 'infinity'
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
name: 'Launchpad',
|
|
55
|
+
value: 'launchpad'
|
|
56
|
+
}
|
|
57
|
+
],
|
|
58
|
+
grantType: [
|
|
59
|
+
{
|
|
60
|
+
name: 'Authorization code',
|
|
61
|
+
value: 'authCode'
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
name: 'Password credentials',
|
|
65
|
+
value: 'passwordCreds'
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
name: 'Client credentials',
|
|
69
|
+
value: 'clientCreds'
|
|
70
|
+
}
|
|
71
|
+
]
|
|
72
|
+
};
|
|
42
73
|
|
|
43
74
|
export const COMPONENT_SCHEMA = {
|
|
44
75
|
framework: [
|