SecurityProof Key for Code Exchangecode_verifiercode_challenge

PKCE

Definition

PKCE (RFC 7636) has the client send code_challenge = BASE64URL(SHA-256(code_verifier)) with the authorization request and prove possession of the secret code_verifier when exchanging the code for tokens.

In practice

The authorization code travels through the browser, where it can leak via redirects, logs or a malicious app registered for the same URL scheme. With PKCE, only the client that generated the verifier can redeem the code. It also defends against code injection, where an attacker swaps their own code into a victim's session.

PKCE was created for public clients (mobile and single-page apps) that cannot hold a secret, but OAuth 2.1 requires it for public clients and RFC 9700 recommends it for confidential ones too. Use the S256 method, never plain. PKCE does not replace exact redirect URI matching or the state check.

Example

A mobile app generates a 43-character random code_verifier, sends its SHA-256 hash as code_challenge with code_challenge_method=S256, and later posts the original verifier to the token endpoint.