Skip to content

Instantly share code, notes, and snippets.

@dcaponi
Created January 1, 2021 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcaponi/769dce87c752d06bf2b188f57dd945e8 to your computer and use it in GitHub Desktop.
Save dcaponi/769dce87c752d06bf2b188f57dd945e8 to your computer and use it in GitHub Desktop.
Creates a legal code_verifier based on the OAuth Spec
const createCodeVerifier = ( size ) => {
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~';
const charsetIndexBuffer = new Uint8Array( size );
for ( let i = 0; i < size; i += 1 ) {
charsetIndexBuffer[i] = ( Math.random() * charset.length ) | 0;
}
let randomChars = [];
for ( let i = 0; i < charsetIndexBuffer.byteLength; i += 1 ) {
let index = charsetIndexBuffer[i] % charset.length;
randomChars.push( charset[index] );
}
return randomChars.join( '' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment