bento-auth-js
Version:
Authentication library for web applications of Bento-Platform
25 lines • 1.83 kB
JavaScript
// Adapted from https://developer.okta.com/blog/2019/05/01/is-the-oauth-implicit-flow-dead
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
export const PKCE_LS_PREFIX = "pkce";
export const PKCE_LS_STATE = `${PKCE_LS_PREFIX}_state`;
export const PKCE_LS_VERIFIER = `${PKCE_LS_PREFIX}_verifier`;
/** Create a securely random string */
export const secureRandomString = (length = 32) => Array.from(crypto.getRandomValues(new Uint32Array(length)), (v) => ("0" + v.toString(16)).slice(-2)).join("");
/** Generates a SHA256 hash of a given string. */
const textSHA256 = (v) => crypto.subtle.digest("SHA-256", new TextEncoder().encode(v));
/** Create a URL-safe base-64 representation of an ArrayBuffer containing the bytes of a cryptographic hash. */
const b64URLEncode = (v) => btoa(String.fromCharCode(...new Uint8Array(v)))
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=+$/, "");
/** Create a PKCE (proof key for code exchange) challenge from a given securely randomly-generated string (verifier). */
export const pkceChallengeFromVerifier = (v) => __awaiter(void 0, void 0, void 0, function* () { return b64URLEncode(yield textSHA256(v)); });
//# sourceMappingURL=pkce.js.map