Identity and access PKCE 3 min read
What is PKCE?
Your app sent the browser to an authorization server and got an authorization code back. What stops someone who copied that code from exchanging it for a token?
-
The client keeps a verifier
Before sending the user to authorize access, the app creates a fresh random string called the
code_verifier. It keeps this value for the current authorization attempt.The app derives a
code_challengefrom the verifier and includes that challenge in the authorization request. With the recommendedS256method, the challenge is a SHA-256 digest encoded for transport.The client creates a secret code verifier, derives a challenge, and sends only the challenge in the OAuth authorization request. The authorization server stores the challenge with the authorization code.
The verifier stays with the client; the authorization server sees its challenge. In short Send the derived challenge now; keep the verifier for later.
-
The verifier unlocks the code exchange
After the user approves, the authorization server returns a short-lived authorization code through the browser redirect. The app sends that code and its original verifier directly to the token endpoint.
The server derives a challenge from the verifier and compares it with the one stored for that code. If they match, it can issue tokens; a different client holding only the intercepted code cannot complete the exchange.
The authorization server returns a code through the browser. The app sends the code and its original verifier to the token endpoint. The server checks the verifier against the saved challenge and returns an access token only when they match.
The code can travel through the browser; redemption requires the verifier. In short A stolen code alone is not enough to redeem it.
-
Use a fresh challenge for each attempt
The verifier must be unpredictable and tied to this authorization attempt. Reusing one value across logins would weaken the binding PKCE is meant to provide.
Use the
S256challenge method where supported. PKCE is part of the Authorization Code flow; it doesn’t replace checking redirect URIs or safely handling the authorization response.In short One flow, one fresh verifier; prefer S256.
What PKCE doesn’t do
PKCE doesn’t authenticate the user, encrypt the authorization code, or make every OAuth implementation safe. The authorization server still needs to validate the client, redirect URI, code, and request correctly.
It protects code redemption by requiring proof of the verifier. Other threats, such as a compromised client or a stolen access token, need their own controls.
Keep this
The client must bring the verifier that matches its earlier challenge to redeem the code.
PKCE binds authorization-code redemption to the client instance that started the flow. Use it with Authorization Code for public clients, and follow current OAuth security guidance for other clients too.- Start
- The client creates a fresh, hard-to-guess verifier.
- Challenge
- It sends a derived challenge with the authorization request.
- Redeem
- It sends the original verifier with the authorization code.
- Check
- The server verifies the match before issuing tokens.
Questions worth carrying
Is the verifier sent through the browser?
No. The challenge goes in the authorization request; the verifier stays with the client until it exchanges the code at the token endpoint.
Does PKCE replace OAuth state?
Not in every design. PKCE protects authorization-code redemption. Clients still need appropriate protections for redirect responses and transaction binding; follow the OAuth security best current practice for your flow.