mirror of
https://github.com/bitinflow/nuxt-oauth.git
synced 2026-03-13 21:56:00 +00:00
PKCE implementation
This commit is contained in:
36
src/runtime/support.ts
Normal file
36
src/runtime/support.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Source: https://docs.cotter.app/sdk-reference/api-for-other-mobile-apps/api-for-mobile-apps
|
||||
*/
|
||||
|
||||
function dec2hex(dec: any) {
|
||||
return ('0' + dec.toString(16)).substr(-2)
|
||||
}
|
||||
|
||||
export function generateRandomString() {
|
||||
const array = new Uint32Array(56 / 2);
|
||||
window.crypto.getRandomValues(array);
|
||||
return Array.from(array, dec2hex).join('');
|
||||
}
|
||||
|
||||
function sha256(plain: any) {
|
||||
const encoder = new TextEncoder();
|
||||
const data = encoder.encode(plain);
|
||||
return window.crypto.subtle.digest('SHA-256', data);
|
||||
}
|
||||
|
||||
function base64urlencode(a: any) {
|
||||
let str = "";
|
||||
const bytes = new Uint8Array(a);
|
||||
const len = bytes.byteLength;
|
||||
for (let i = 0; i < len; i++) {
|
||||
str += String.fromCharCode(bytes[i]);
|
||||
}
|
||||
return btoa(str)
|
||||
.replace(/\+/g, "-")
|
||||
.replace(/\//g, "_")
|
||||
.replace(/=+$/, "");
|
||||
}
|
||||
|
||||
export async function getChallengeFromVerifier(v: any) {
|
||||
return base64urlencode(await sha256(v));
|
||||
}
|
||||
Reference in New Issue
Block a user