Skip to content

Commit

Permalink
minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alvinsj committed Jun 8, 2024
1 parent 07a5f2d commit a3ff525
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/lib/__tests__/pkce.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ describe('toSha256', () => {
})

describe('toBase64Url', () => {
test('throws error on empty array', () => {
expect(() => toBase64Url([]))
.toThrow('bytes must not be empty')
});

test('converts to base64url', () => {
const base64url = toBase64Url([1, 2, 3, 4, 5])

Expand Down
7 changes: 4 additions & 3 deletions src/lib/pkce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const toSha256 = async (data: string): Promise<Uint8Array> => {
// refer to base64url-encoding in RFC 7636
// https://datatracker.ietf.org/doc/html/rfc7636#appendix-A
export const toBase64Url = (bytes: Uint8Array) => {
if (bytes.length === 0) throw new Error('bytes must not be empty');

const charCodes = Array.from(bytes);
let str = btoa(String.fromCharCode.apply(null, charCodes));
str = str.split('=')[0];
Expand All @@ -35,9 +37,8 @@ export const createRandomString = (length: number = 34): string => {
return randomString;
}

export const createPKCECodeChallenge = async (codeVerifies: string): string => {
const hashed: Uint8Array = await toSha256(codeVerifies)
export const createPKCECodeChallenge = async (codeVerifier: string): string => {
const hashed: Uint8Array = await toSha256(codeVerifier)
const codeChallenge = toBase64Url(hashed)
return codeChallenge;
}

0 comments on commit a3ff525

Please sign in to comment.