this repo has no description
1function dec2hex(dec) {
2 return ('0' + dec.toString(16)).slice(-2);
3}
4export function verifier() {
5 var array = new Uint32Array(56 / 2);
6 window.crypto.getRandomValues(array);
7 return Array.from(array, dec2hex).join('');
8}
9function sha256(plain) {
10 // returns promise ArrayBuffer
11 const encoder = new TextEncoder();
12 const data = encoder.encode(plain);
13 return window.crypto.subtle.digest('SHA-256', data);
14}
15function base64urlencode(a) {
16 let str = '';
17 const bytes = new Uint8Array(a);
18 const len = bytes.byteLength;
19 for (var i = 0; i < len; i++) {
20 str += String.fromCharCode(bytes[i]);
21 }
22 return btoa(str).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
23}
24export async function generateCodeChallenge(v) {
25 const hashed = await sha256(v);
26 return base64urlencode(hashed);
27}
28
29// If /.well-known/oauth-authorization-server exists and code_challenge_methods_supported includes "S256", means support PKCE
30export async function supportsPKCE({ instanceURL }) {
31 if (!instanceURL) return false;
32 try {
33 const res = await fetch(
34 `https://${instanceURL}/.well-known/oauth-authorization-server`,
35 );
36 if (!res.ok || res.status !== 200) return false;
37 const json = await res.json();
38 if (json.code_challenge_methods_supported?.includes('S256')) return true;
39 return false;
40 } catch (e) {
41 return false;
42 }
43}
44
45// For debugging
46window.__generateCodeChallenge = generateCodeChallenge;