Skip to content

Commit

Permalink
feat(utils): add pkce utils
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Dec 31, 2019
1 parent 3227d6a commit 1dd6e6a
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/@vssue/utils/src/pkce.ts
@@ -0,0 +1,38 @@
// https://www.oauth.com/playground/authorization-code-with-pkce.html

/**
* Step 1 - 1
*
* Generate code verifier
*/
const generateCodeVerifier = (length: number = 43): string => {
let len: number = length
if (len < 43) len = 43
if (len > 128) len = 128

const randomArr = window.crypto.getRandomValues(new Uint8Array(len))
const validChars =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~'

let codeVerifier = ''

for (const i of randomArr) {
codeVerifier += validChars[i % len]
}

return codeVerifier
}

/**
* Step 1 -2
*
* Generate code challenge
*/
const generateCodeChallenge = async (codeVerifier: string): Promise<string> => {
const data = new TextEncoder().encode(codeVerifier)
const hash = await crypto.subtle.digest('SHA-256', data)
return btoa(String.fromCharCode(...new Uint8Array(hash)))
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '')
}

0 comments on commit 1dd6e6a

Please sign in to comment.