What is PKCE?

In short
PKCE, pronounced "pixy", is a security extension for OAuth 2.0 that protects the authorization code flow in public clients such as mobile and desktop apps. The app creates a one-time secret and proves possession of it when exchanging the authorization code for tokens, so an attacker who intercepts the code cannot use it.
The problem PKCE solves
OAuth 2.0 lets an app obtain access to a user's data without handling the user's password. In the authorization code flow, the app receives a short-lived authorization code through a redirect, then exchanges that code for an access token at the token endpoint. This works well for servers that can keep secrets. Public clients, such as native mobile apps and desktop apps, cannot. Any secret shipped inside an app binary can be extracted, so it cannot be treated as confidential.
The RFC describes a concrete threat, the authorization code interception attack. On a phone, a malicious app can register the same custom URI scheme as the legitimate app. If the operating system lets both apps claim the scheme, the malicious app may receive the redirect and read the authorization code. If it then gets hold of the client identifier, it can exchange the code for tokens itself. The RFC notes that this attack has been seen in practice.
How it works
PKCE adds a per-request secret that only the original client knows. The flow has four steps:
- The client creates a high-entropy random string called the code verifier. The RFC asks for enough randomness that it cannot be guessed.
- The client transforms the verifier into a code challenge. With the method named S256, the challenge is the SHA-256 hash of the verifier, encoded with base64url and without padding.
- The client sends the code challenge and the challenge method with the authorization request. The authorization server stores the challenge alongside the code it issues.
- When the client redeems the authorization code at the token endpoint, it sends the original code verifier. The server hashes it, compares the result with the stored challenge, and issues tokens only if they match.
An attacker who intercepts the code does not know the verifier. The verifier travels only over TLS, on the back channel to the token endpoint, so the attacker cannot complete the exchange. Each authorization request gets a new verifier, so a captured value cannot be reused for another login.
Choosing a challenge method
The specification defines two methods. The plain method sends the verifier unchanged as the challenge. The RFC says this only mitigates the attack in the case where the attacker can observe responses but not requests to the authorization endpoint. The S256 method hashes the verifier first. The RFC says that when the attacker might also observe requests, the method must be S256 or a cryptographically secure method defined by an extension. In practice, you should always use S256 when your server supports it.
A simple example
The appendix of the RFC gives a worked S256 example. The client builds a challenge like this in pseudocode:
code_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier)))
The authorization request carries code_challenge and code_challenge_method=S256. The token request carries code_verifier. The client does not send the hash at that point, only the original value. The server repeats the hash and compares the two results.
Common mistakes
A frequent error is using a weak random source for the verifier. The RFC requires the verifier to have sufficient entropy, so a predictable value defeats the purpose. Another mistake is falling back to the plain method when S256 is available. Developers also sometimes store the verifier in a place that other apps can read, which exposes the secret the scheme depends on. Finally, base64url encoding must omit the trailing padding characters, as the appendix explains.
Alternatives and related standards
PKCE is not the only defense. The RFC mentions that the OAuth threat model describes mitigations based on per-instance client secrets or per-instance redirect URIs, but these are not always usable on native apps. PKCE was designed to work with the standard flow without them. Confidential clients, such as server-side web apps that can keep a client secret, can use the same flow, and the RFC treats PKCE as a useful addition for them as well. Newer OAuth guidance commonly recommends PKCE for all authorization code flows, so it is a sensible default for new applications.
To learn the flow it extends, read about the authorization code grant first. Then review the RFC's security considerations section, which covers entropy, eavesdroppers and TLS in more detail.
FAQ
Is PKCE only for mobile apps?
No. The RFC was written for public clients such as native apps, but the same protection applies to any authorization code flow. Using it everywhere keeps your setup consistent.
Why is the plain method considered weaker?
With plain, the challenge equals the verifier, so anyone who sees the authorization request learns the secret. The RFC says plain only mitigates the attack when requests cannot be observed, so S256 is the safer choice.
Does PKCE replace the client secret?
Not necessarily. PKCE proves that the client that started the request is the one redeeming the code. A client secret, where used, authenticates the client to the server. They solve different problems and can be used together.
What happens if the verifier does not match?
The authorization server rejects the token request and returns an error, so no tokens are issued. The RFC describes the server comparing the verifier with the earlier challenge before returning tokens.
Sources
This page is based on the official documentation below. Read it for full detail.
PKCE — official documentation ↗