Explain the OAuth 2.0 authorization code flow with PKCE.
It is the recommended flow for web, mobile and single-page applications.
- The client generates a random
code_verifier, hashes it to acode_challenge, and redirects the user to the authorization endpoint with the client id, redirect URI, scope, state and challenge. - The user authenticates and consents.
- The authorization server redirects back with an authorization code and the original
state. - The client exchanges the code at the token endpoint, sending the
code_verifier. - The server verifies the verifier against the stored challenge and returns tokens.
GET /authorize?response_type=code&client_id=app
&code_challenge=...&code_challenge_method=S256&state=xyz
PKCE prevents an attacker who intercepts the authorization code from redeeming it, because they lack the verifier. Always validate state for CSRF, use exact registered redirect URIs, and avoid the deprecated implicit flow that exposes tokens in the URL.