OAuth Authorization Code Grant: protect authorization codes with PKCE
Understand the OAuth authorization-code flow, how PKCE protects an intercepted code, and the redirect, polling, and token-handling checks to apply.
OAuth Authorization Code Grant: protect authorization codes with PKCE
OAuth 2.0 Authorization Code Grant is a delegated-authorization flow in which a user approves a request through a browser, the client receives a short-lived authorization code, and the client exchanges that code for an access token at the token endpoint. If the user is not already authenticated, the authorization server may first ask them to sign in, but the authorization code itself is not proof of login. Tokens are issued through the token-endpoint exchange instead of being returned directly to the browser redirect. The current OAuth security BCP requires PKCE for public clients and recommends it for confidential clients as well.
OAuth lets a client access protected resources on a user's behalf. The authorization server may handle the user's actual sign-in, but an authorization code is not an ID token and does not prove who the user is.
The flow has four steps
**The client sends an authorization request.** It redirects the browser to the authorization endpoint with `response_type=code`, `client_id`, the registered `redirect_uri`, the requested `scope`, and `state` to correlate the transaction. With PKCE, the client creates a random `code_verifier` and also sends the derived `code_challenge`.
**The user reviews the requested access.** If the user is not already authenticated, the authorization server performs the required authentication, then displays the requesting app and access scope for approval or denial.
**The server redirects the browser back to the client.** After approval, the authorization server sends a short-lived, one-time `code` to the registered redirect URI. The client checks `state` to make sure the response matches the transaction it started.
**The client exchanges the code for tokens.** The client sends the authorization code to the token endpoint. A confidential client authenticates itself as appropriate for its type. With PKCE, the client also sends the original `code_verifier`, which the server checks against the challenge it received earlier. If validation succeeds, the server issues an access token and possibly other tokens.
How PKCE protects an intercepted code
PKCE (Proof Key for Code Exchange) creates a random secret code_verifier for each authorization request and sends a value derived from it, the code_challenge, in the initial request. If an attacker intercepts the authorization code and tries to exchange it at the token endpoint, the attacker cannot obtain tokens without the original verifier. The verifier itself is not included in the initial request or redirect.
RFC 9700 requires public clients to use PKCE to prevent authorization-code injection and misuse, and recommends PKCE for confidential clients too. S256 is the recommended current PKCE challenge method because it does not expose the verifier in the request; authorization servers must support PKCE. Clients should create a different challenge for each request and confirm that the server enforces its use.
Security checks before implementation
**Register exact redirect URIs.** The authorization server must not send codes to unregistered or mismatched addresses. Do not use wildcards or open redirectors.
**Use PKCE with `S256`.** Generate the verifier securely for each request, store it with that transaction, and send it to the token endpoint only once from the expected callback after approval.
**Bind and validate `state` to the transaction.** Check that the request and callback correspond. If using OIDC, follow the relevant OIDC guidance for nonce and ID-token validation separately from OAuth scope.
**Treat authorization codes as short-lived and single-use.** RFC 6749 recommends a maximum lifetime of 10 minutes. Prevent code reuse and, if a code is reused, revoke previously issued tokens when possible.
**Protect sensitive data in redirect responses.** Avoid unnecessary third-party scripts on the callback, and check that codes do not leak into logs, analytics records, or external referrers.
**Store tokens according to the client type.** Browser, native, and server apps have different ways to protect secrets. Do not treat a fixed client secret embedded in a public client as a secure secret.
Authorization Code Grant is an OAuth delegation flow; by itself, it does not mean an application login has completed or that the user has been authenticated. If OpenID Connect is used for login, add the OIDC request and ID-token validation to the OAuth authorization-code procedure. Check the provider's documentation for its specific settings and supported features.
References
[RFC 6749: The OAuth 2.0 Authorization Framework](https://www.rfc-editor.org/rfc/rfc6749.html)
[RFC 7636: Proof Key for Code Exchange by OAuth Public Clients](https://www.rfc-editor.org/rfc/rfc7636.html)
[RFC 9700: Best Current Practice for OAuth 2.0 Security](https://www.rfc-editor.org/rfc/rfc9700.html)