Skip to content

Commit 84c76a2

Browse files
vishalshrm539Sharma
andauthored
Added private methods in PegaAuth class (#79)
Co-authored-by: Sharma <[email protected]>
1 parent 06c6eee commit 84c76a2

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

src/app/_helpers/auth.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,28 @@ class PegaAuth {
2424
this.config = peConfig ? obj : null;
2525
}
2626

27-
updateConfig() {
27+
#updateConfig() {
2828
const sSI = JSON.stringify(this.config);
2929
window.sessionStorage.setItem(this.ssKeyConfig, this.bEncodeSI ? window.btoa(sSI) : sSI);
3030
}
3131

3232
// For PKCE the authorize includes a code_challenge & code_challenge_method as well
33-
async buildAuthorizeUrl(state) {
33+
async #buildAuthorizeUrl(state) {
3434
const {clientId, redirectUri, authorizeUri, authService, sessionIndex, appAlias, useLocking,
3535
userIdentifier, password} = this.config;
3636

3737
// Generate random string of 64 chars for verifier. RFC 7636 says from 43-128 chars
3838
let buf = new Uint8Array(64);
3939
window.crypto.getRandomValues(buf);
40-
this.config.codeVerifier = this.base64UrlSafeEncode(buf);
40+
this.config.codeVerifier = this.#base64UrlSafeEncode(buf);
4141
// Persist codeVerifier in session storage so it survives the redirects that are to follow
42-
this.updateConfig();
42+
this.#updateConfig();
4343

4444
if( !state ) {
4545
// Calc random state variable
4646
buf = new Uint8Array(32);
4747
window.crypto.getRandomValues(buf);
48-
state = this.base64UrlSafeEncode(buf);
48+
state = this.#base64UrlSafeEncode(buf);
4949
}
5050

5151
// Trim alias to include just the real alias piece
@@ -59,7 +59,7 @@ class PegaAuth {
5959
(userIdentifier ? `&UserIdentifier=${encodeURIComponent(userIdentifier)}` : '') +
6060
(userIdentifier && password ? `&Password=${encodeURIComponent(window.atob(password))}` : '');
6161

62-
return this.getCodeChallenge(this.config.codeVerifier).then( cc => {
62+
return this.#getCodeChallenge(this.config.codeVerifier).then( cc => {
6363
// Now includes new enable_psyncId=true and session_index params
6464
return `${authorizeUri}?client_id=${clientId}&response_type=code&redirect_uri=${redirectUri}&scope=openid${addtlScope}&state=${state}&code_challenge=${cc}&code_challenge_method=S256${moreAuthArgs}`;
6565
});
@@ -79,7 +79,7 @@ class PegaAuth {
7979

8080
return new Promise( (resolve, reject) => {
8181

82-
this.buildAuthorizeUrl(state).then((url) => {
82+
this.#buildAuthorizeUrl(state).then((url) => {
8383
let myWindow = null; // popup or iframe
8484
let elIframe = null;
8585
const iframeTimeout = this.config.silentTimeout !== undefined ? this.config.silentTimeout : 5000;
@@ -140,7 +140,7 @@ class PegaAuth {
140140
// remove password from config
141141
if( this.config.password ) {
142142
delete this.config.password;
143-
this.updateConfig();
143+
this.#updateConfig();
144144
}
145145
elIframe.parentNode.removeChild(elIframe);
146146
elIframe = null;
@@ -197,7 +197,7 @@ class PegaAuth {
197197
loginRedirect() {
198198
// eslint-disable-next-line no-restricted-globals
199199
const state = btoa(location.origin);
200-
this.buildAuthorizeUrl(state).then((url) => {
200+
this.#buildAuthorizeUrl(state).then((url) => {
201201
// eslint-disable-next-line no-restricted-globals
202202
location.href = url;
203203
});
@@ -251,7 +251,7 @@ class PegaAuth {
251251
bUpdateConfig = true;
252252
}
253253
if( bUpdateConfig ) {
254-
this.updateConfig();
254+
this.#updateConfig();
255255
}
256256
return token;
257257
})
@@ -343,37 +343,37 @@ class PegaAuth {
343343
// Also clobber any sessionIndex
344344
if( this.config.sessionIndex ) {
345345
delete this.config.sessionIndex;
346-
this.updateConfig();
346+
this.#updateConfig();
347347
}
348348
}
349349
/* eslint-enable camelcase */
350350

351351
// eslint-disable-next-line class-methods-use-this
352-
sha256Hash(str) {
352+
#sha256Hash(str) {
353353
return window.crypto.subtle.digest("SHA-256", new TextEncoder().encode(str));
354354
}
355355

356356
// Base64 encode
357357
// eslint-disable-next-line class-methods-use-this
358-
encode64(buff) {
358+
#encode64(buff) {
359359
return window.btoa(new Uint8Array(buff).reduce((s, b) => s + String.fromCharCode(b), ''));
360360
}
361361

362362
/*
363363
* Base64 url safe encoding of an array
364364
*/
365-
base64UrlSafeEncode(buf) {
366-
const s = this.encode64(buf).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
365+
#base64UrlSafeEncode(buf) {
366+
const s = this.#encode64(buf).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
367367
return s;
368368
}
369369

370370
/* Calc code verifier if necessary
371371
*/
372372
/* eslint-disable camelcase */
373-
getCodeChallenge(code_verifier) {
374-
return this.sha256Hash(code_verifier).then (
373+
#getCodeChallenge(code_verifier) {
374+
return this.#sha256Hash(code_verifier).then (
375375
(hashed) => {
376-
return this.base64UrlSafeEncode(hashed)
376+
return this.#base64UrlSafeEncode(hashed)
377377
}
378378
).catch(
379379
(error) => {

0 commit comments

Comments
 (0)