Created
November 6, 2020 18:24
-
-
Save jimmyz/f80cfc089e35323fc83d50bd760995fc to your computer and use it in GitHub Desktop.
PKCE Code Verifiers and Challenges for JavaScript
This file contains hidden or 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
// Modified from sample code provided by Auth0 | |
// https://auth0.com/docs/flows/call-your-api-using-the-authorization-code-flow-with-pkce#javascript-sample | |
// Run with `node create_challenge.js` | |
const crypto = require('crypto'); | |
function base64URLEncode(str) { | |
return str.toString('base64') | |
.replace(/\+/g, '-') | |
.replace(/\//g, '_') | |
.replace(/=/g, ''); | |
} | |
function sha256(buffer) { | |
return crypto.createHash('sha256').update(buffer).digest(); | |
} | |
function createVerifier() { | |
return base64URLEncode(crypto.randomBytes(32)); | |
} | |
function createChallenge(){ | |
return base64URLEncode(sha256(verifier)); | |
} | |
var verifier = createVerifier(); | |
var challenge = createChallenge(); | |
console.log("Verifier: " + verifier); | |
console.log("Challenge: " + challenge); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment