PKCE Utility (utils/pkce.ts)

omniroute95 · src/lib/oauth/utils/pkce.ts · typescript · MIT
auth, security, lang:typescript, tag:pkce, tag:code

Generates PKCE code verifier, challenge, and state for OAuth flows. Reuse in authentication libraries requiring PKCE support.

// Source: https://github.com/ChrisStack95/omniroute95 | License: MIT | Copyright 2026 diegosouzapw
// Provided via justclaw.it.com; keep this notice with the code.
import crypto from "crypto";

/**
 * Generate PKCE code verifier (43-128 characters)
 */
export function generateCodeVerifier(bytes = 32) {
  return crypto.randomBytes(bytes).toString("base64url");
}

/**
 * Generate PKCE code challenge from verifier (S256 method)
 */
export function generateCodeChallenge(verifier) {
  return crypto.createHash("sha256").update(verifier).digest("base64url");
}

/**
 * Generate random state for CSRF protection
 */
export function generateState() {
  return crypto.randomBytes(32).toString("base64url");
}

/**
 * Generate complete PKCE pair
 */
export function generatePKCE(verifierBytes = 32) {
  c

... (truncated -- full source via MCP)

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

Get a free API key