What is PKCE (Proof Key for Code Exchange), and why is it now recommended for OAuth flows even in traditional server-side Node.js applications?
Short answer
PKCE adds a dynamically generated secret (a 'code verifier' and its hashed 'code challenge') to the Authorization Code flow, binding the initial authorization request to the specific client that later exchanges the code for a token -- originally designed for public clients (like…
Advertisement
AModel answer
PKCE adds a dynamically generated secret (a 'code verifier' and its hashed 'code challenge') to the Authorization Code flow, binding the initial authorization request to the specific client that later exchanges the code for a token -- originally designed for public clients (like mobile and single-page apps) that can't securely store a static client secret, but now recommended universally by OAuth security best practices as defense-in-depth, protecting against authorization code interception even for confidential server-side clients.
const crypto = require('node:crypto');
const codeVerifier = crypto.randomBytes(32).toString('base64url');
const codeChallenge = crypto.createHash('sha256').update(codeVerifier).digest('base64url');
// codeChallenge sent in the initial authorization request
// codeVerifier sent later, during the token exchange, to prove it's the same clientReal-world example
A company's server-side Node.js application adds PKCE to its existing Authorization Code flow with Okta, following updated OAuth 2.1 security recommendations, providing an additional layer of protection against authorization code interception even though the server already keeps its client secret confidential.
Likely follow-ups
- How does PKCE protect against an intercepted authorization code even without needing a client secret at all?
- What's the difference between the 'plain' and 'S256' PKCE code challenge methods?
Related topics