OAuth PKCE Utility (src/utils.ts)

logto · packages/connectors/connector-linkedin/src/utils.ts · typescript · MPL-2.0
auth, security, tag:crypto

Generates code verifiers and challenges for OAuth 2.0 PKCE flows. Use this to secure authentication requests in client-side applications.

// Source: https://github.com/logto-io/logto | License: MPL-2.0 | Copyright under Patent Claims infringed by Covered Software in the absence of | Modified files must remain under MPL-2.0
// Modifications to this file must remain under MPL-2.0.
// Provided via justclaw.it.com; keep this notice with the code.
import crypto from 'node:crypto';

export const generateCodeVerifier = () => {
  const buffer = crypto.randomBytes(32);
  return buffer.toString('base64url');
};

export const generateCodeChallenge = (verifier: string) => {
  const hash = crypto.createHash('sha256');
  hash.update(verifier);
  return hash.digest('base64url');
};

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key