forked from auth0/spa-pkce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
61 lines (57 loc) · 1.69 KB
/
util.js
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
/**
* Generates a cryptographically secure random string
* of variable length.
*
* The returned string is also url-safe.
*
* @param {Number} length the length of the random string.
* @returns {String}
*/
function randomString(length) {
const validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let array = new Uint8Array(length);
window.crypto.getRandomValues(array);
array = array.map(x => validChars.charCodeAt(x % validChars.length));
return String.fromCharCode(...array);
}
/**
* Takes a base64 encoded string and returns a url encoded string
* by replacing the characters + and / with -, _ respectively,
* and removing the = (fill) character.
*
* @param {String} input base64 encoded string.
* @returns {String}
*/
function urlEncodeB64(input) {
const b64Chars = {'+': '-', '/': '_', '=': ''};
return input.replace(/[\+\/=]/g, m => b64Chars[m]);;
}
/**
* Takes an ArrayBuffer and convert it to Base64 url encoded string.
* @param {ArrayBuffer} input
* @returns {String}
*/
function bufferToBase64UrlEncoded(input) {
var bytes = new Uint8Array(input);
return urlEncodeB64(window.btoa(String.fromCharCode(...bytes)));
}
/**
* Returns the sha256 digst of a given message.
* This function is async.
*
* @param {String} message
* @returns {Promise<ArrayBuffer>}
*/
function sha256(message) {
let encoder = new TextEncoder();
let data = encoder.encode(message);
return window.crypto.subtle.digest('SHA-256', data);
}
/**
* fetch the openid configuration for the issuer
* @returns {Promise<any>}
*/
async function getConfig() {
const response = await fetch(`https://${AUTH0_DOMAIN}/.well-known/openid-configuration`);
return response.json();
}