Skip to content

Commit

Permalink
Follow RFC7636 to generate code verifier #282
Browse files Browse the repository at this point in the history
  • Loading branch information
tung2744 authored Apr 15, 2024
2 parents 587088e + fa6024d commit fac9229
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 27 deletions.
11 changes: 2 additions & 9 deletions packages/authgear-capacitor/src/pkce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@ import { _base64URLEncode } from "@authgear/core";

import { randomBytes, sha256String } from "./plugin";

function byteToHex(byte: number): string {
return ("0" + byte.toString(16)).substr(-2);
}

export async function generateCodeVerifier(): Promise<string> {
const arr = await randomBytes(32);
let output = "";
for (let i = 0; i < arr.length; ++i) {
output += byteToHex(arr[i]);
}
return output;
const base64 = _base64URLEncode(new Uint8Array(arr));
return base64;
}

export async function computeCodeChallenge(
Expand Down
11 changes: 2 additions & 9 deletions packages/authgear-react-native/src/pkce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@ import { _base64URLEncode } from "@authgear/core";

import { randomBytes, sha256String } from "./nativemodule";

function byteToHex(byte: number): string {
return ("0" + byte.toString(16)).substr(-2);
}

export async function generateCodeVerifier(): Promise<string> {
const arr = await randomBytes(32);
let output = "";
for (let i = 0; i < arr.length; ++i) {
output += byteToHex(arr[i]);
}
return output;
const base64 = _base64URLEncode(new Uint8Array(arr));
return base64;
}

export async function computeCodeChallenge(
Expand Down
11 changes: 2 additions & 9 deletions packages/authgear-web/src/pkce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

import { _encodeUTF8, _base64URLEncode } from "@authgear/core";

function byteToHex(byte: number): string {
return ("0" + byte.toString(16)).substr(-2);
}

// windowCryptoSubtleDigest is window.crypto.subtle.digest with IE 11 support.
async function windowCryptoSubtleDigest(
algorithm: string,
Expand Down Expand Up @@ -44,9 +40,6 @@ export async function computeCodeChallenge(
export function generateCodeVerifier(): string {
const arr = new Uint8Array(32);
window.crypto.getRandomValues(arr);
let output = "";
for (let i = 0; i < arr.length; ++i) {
output += byteToHex(arr[i]);
}
return output;
const base64 = _base64URLEncode(arr);
return base64;
}

0 comments on commit fac9229

Please sign in to comment.