Why PKCE is Mandatory for Public Clients
In traditional OAuth 2.0 Authorization Code flow, a confidential client_secret is required to exchange the authorization code for an access token. Single Page Applications (SPAs) and mobile apps cannot safely hold a client secret without exposing it to decompilation or browser inspection. Proof Key for Code Exchange (PKCE - RFC 7636) eliminates the need for client secrets on public clients.
The PKCE Handshake Steps
- Create Verifier: The client creates a cryptographically random string (
code_verifier). - Create Challenge: The client hashes the verifier using SHA-256 and base64url encodes it to produce the
code_challenge. - Authorization Request: The client sends the
code_challengeandcode_challenge_method=S256to the authorization server. - Token Exchange: Upon receiving the authorization code callback, the client sends the original unhashed
code_verifierin the POST token exchange request. The auth server computesSHA-256(code_verifier)and verifies it matches the original challenge before issuing the token.