Skip to content

Instantly share code, notes, and snippets.

@t1mofe1
Last active March 7, 2024 08:49
Show Gist options
  • Select an option

  • Save t1mofe1/2eaedc6587c2c20eabdda5057905e073 to your computer and use it in GitHub Desktop.

Select an option

Save t1mofe1/2eaedc6587c2c20eabdda5057905e073 to your computer and use it in GitHub Desktop.

Revisions

  1. t1mofe1 revised this gist Dec 29, 2022. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion pkce.ts
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,12 @@
    import * as crypto from 'crypto';

    export default function generatePKCE(length = 43) {
    export type PkcePayload = {
    code_verifier: string;
    code_challenge: string;
    code_challenge_method: 'S256';
    };

    export default function generatePKCE(length = 128): PkcePayload {
    if (length < 43) length = 43;
    if (length > 128) length = 128;

  2. t1mofe1 created this gist Dec 29, 2022.
    22 changes: 22 additions & 0 deletions pkce.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    import * as crypto from 'crypto';

    export default function generatePKCE(length = 43) {
    if (length < 43) length = 43;
    if (length > 128) length = 128;

    const bytesLength = Math.ceil((length * 3) / 4);

    const code_verifier = crypto.randomBytes(bytesLength).toString('base64url');

    const code_challenge = crypto
    .createHash('sha256')
    .update(code_verifier)
    .digest()
    .toString('base64url');

    return {
    code_verifier,
    code_challenge,
    code_challenge_method: 'S256', // TODO: support plain?
    };
    }