What problem does PKCE address?
The IETF’s Proof Key for Code Exchange standard addresses authorization code interception attacks against public clients that use the Authorization Code Grant. PKCE mitigates this threat by linking the authorization request to the subsequent token request.
In the attack described by the standard, a malicious app can register itself as a handler for the same custom URI scheme as the legitimate app. The malicious app can intercept the authorization code during the redirect. The attacker can then use the intercepted code to request an access token.
This scenario depends on conditions such as the operating system allowing multiple apps to register for the same custom URI scheme. The standard also notes that a client secret embedded in the application binary, as in its native app examples, cannot be considered confidential.
In the PKCE flow, the client generates a fresh code_verifier for each authorization request. It derives a code_challenge from that value and sends it to the authorization server with the authorization request. The client later sends the original code_verifier to the token endpoint along with the authorization code.
The authorization server applies the specified transformation to the received code_verifier and compares the result with the code_challenge it previously associated with the authorization code. If the values do not match, the server denies access. In the flow described by the standard, intercepting the authorization code alone leaves the attacker without the code_verifier.
Preparing the verifier and challenge
The IETF standard defines the code_verifier as a high-entropy, cryptographically random string. It links the authorization request to the token request. The client generates and stores one for each authorization request.
The code_verifier can contain uppercase and lowercase ASCII letters, digits, and the characters -, ., _, and ~. The standard requires enough entropy to make guessing the value impractical for an attacker.
The client generates the code_challenge by applying a transformation to the code_verifier. The standard defines two methods. With plain, the code_challenge has the same value as the code_verifier. With S256, the client applies SHA-256 to the ASCII representation of the code_verifier and base64url-encodes the result.
The IETF’s definition of base64url omits trailing = characters. The encoded value contains no line breaks, spaces, or other added characters. This definition governs the format of the code_challenge produced by the S256 transformation.
The standard requires clients that can use S256 to do so. A client may use plain only if it cannot support S256 for technical reasons and knows from its configuration that the server supports plain. The plain method is defined for compatibility with existing deployments and for constrained environments that cannot use S256.
The authorization request includes the required code_challenge field. The code_challenge_method field is optional and defaults to plain if omitted. When the client supplies a method, the server uses it during verification.
How is the authorization code verified?
Under the IETF standard, when the authorization server issues a code in the authorization response, it associates the code_challenge and code_challenge_method with that code. The token endpoint uses that association to verify the verifier in the subsequent request.
The server can embed these values in encrypted form within the code or store them server-side and associate them with the code. The code_challenge must not be included in client requests in a form that other parties can extract.
After receiving the authorization code, the client sends an access token request to the token endpoint. This request includes the authorization code and the code_verifier. The token endpoint uses the code_challenge_method associated with the authorization code when it was issued.
If the method is S256, the server applies SHA-256 to the received code_verifier, base64url-encodes the result, and compares it with the stored code_challenge. If the method is plain, the server compares the values directly. If they match, the token endpoint continues processing the request as usual. Otherwise, it returns invalid_grant.
PKCE errors can also occur at the authorization endpoint. If the server requires PKCE for a public client and the request omits code_challenge, the server must return invalid_request. A server that supports PKCE returns the same error if it does not support the requested transformation. The response should explain the error using error_description or error_uri.