-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding PKCE in admin-ui authentication #1221
- Loading branch information
Showing
5 changed files
with
91 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
export class RandomHashGenerator { | ||
constructor(randomHashGenerator) { | ||
this.randomHashGenerator = randomHashGenerator | ||
} | ||
static async generateRandomChallengePair(algo) { | ||
const secret = await RandomHashGenerator.generateRandomString(); | ||
const encryt = await RandomHashGenerator.encrypt(secret, algo); //'SHA-256' | ||
const hashed = RandomHashGenerator.base64URLEncode(encryt); | ||
return { codeVerifier: secret, codeChallenge: hashed }; | ||
} | ||
|
||
static base64URLEncode(a) { | ||
var str = ""; | ||
var bytes = new Uint8Array(a); | ||
var len = bytes.byteLength; | ||
for (var i = 0; i < len; i++) { | ||
str += String.fromCharCode(bytes[i]); | ||
} | ||
|
||
return btoa(str) | ||
.replace(/\+/g, "-") | ||
.replace(/\//g, "_") | ||
.replace(/=+$/, ""); | ||
|
||
} | ||
|
||
static dec2hex(dec) { | ||
return ('0' + dec.toString(16)).substr(-2) | ||
} | ||
|
||
static generateRandomString() { | ||
var array = new Uint32Array(56 / 2); | ||
window.crypto.getRandomValues(array); | ||
return Array.from(array, RandomHashGenerator.dec2hex).join(''); | ||
} | ||
|
||
static async encrypt(plain, algo) { // returns promise ArrayBuffer | ||
const encoder = new TextEncoder(); | ||
const data = await encoder.encode(plain); | ||
return window.crypto.subtle.digest(algo, data); | ||
} | ||
} |