OAUTH 2.0
Delegated authorization without sharing passwords
In the authorization code flow, the provider redirects back to the app with a one-time code, and the app trades that code for a token at the token endpoint. A mobile app does this with no client secret and no PKCE (a per-login secret that proves who started the flow). Another app on the phone registers the same redirect scheme and receives the code. What happens when it posts that code to the token endpoint?
OAuth lets an app act for a user without ever seeing their password. The app sends the browser to the authorization server. The user signs in there and consents. The server redirects back with a one-time code, and the app redeems that code for an access token.
The code passes through the browser, so two things can go wrong. Someone can deliver the wrong code to your callback, and someone can redeem a copy of the right code. A state value tied to the browser session stops the first. PKCE stops the second: only the client that started the flow holds the secret needed to redeem the code. PKCE also defeats the first, since an injected code is bound to the attacker’s verifier, so RFC 9700 lets a client rely on PKCE instead of state once it knows the server enforces PKCE.
OAuth 2.0 authorization code flow + PKCE (RFC 6749, RFC 7636):
Browser Client Auth Server
|--"Sign in"------>| |
| | state = random, kept in this session
| | verifier = random, kept in this session
| | challenge = BASE64URL(SHA256(verifier))
|<--302 /authorize?client_id&redirect_uri&scope&state&code_challenge&code_challenge_method=S256
|----------------------------------------------->| user signs in, consents
|<--302 redirect_uri?code=…&state=… --------------|
|--/callback?code&state->| |
| | state == session.state ? (else: login CSRF)
| |--POST /token---------------->|
| | code, redirect_uri, |
| | code_verifier | SHA256(verifier) == challenge ?
| | (+ client_secret only for | code unused, same client and redirect_uri ?
| | confidential clients) |
| |<--access_token (+ refresh)---|
The challenge travels through the browser and can be seen; the verifier never does.
Access tokens authorize; for "who is this user", use OpenID Connect's ID token. Watch alice’s browser and the ATTACKER box. In NO STATE, alice ends up signed in as mallory and her upload goes to mallory. In NO PKCE, the malicious app redeems alice’s code first and alice’s own redemption fails. In STATE + PKCE, both attacks are refused and both invariants stay green.
A public client with no state and no PKCE. Insert one attacker step, at the right moment, so the attacker ends up holding a token for alice.
GOAL: the attacker holds a token for alice
Write both halves. start(session) runs in the client and returns the state and code_challenge for /authorize. callback(session, query) returns the code_verifier to redeem with, or null to refuse. verify(challenge, verifier) is the AS check at the token endpoint. Remember that the challenge is visible in the browser URL.
Every “Sign in with Google/GitHub/Apple”, mobile and single-page apps (the OAuth security best current practice, RFC 9700, requires PKCE for public clients and recommends it for confidential ones; the OAuth 2.1 draft builds it into the code flow), and libraries such as AppAuth that generate the state and verifier for you. A missing state check is a common bug report for login CSRF. The tokens the flow returns are often JWTs, and checking them is the JWT pitfalls lesson.
“Walk me through the authorization code flow” or “What is PKCE for?” Draw the redirects, say what state binds (the callback to the browser session) and what PKCE binds (the code to the client that asked for it), explain why public clients cannot keep a secret, and say why the implicit flow is deprecated.
USED IN
Chapters that build on this lesson.
- Open banking: the CBN framework, with UK and Brazil comparedPayments
- Platform, API and technical PMProduct Manager
- Authentication and authorisation for a multi-tenant SaaS and its public APISecurity
- Identity, access and secretsSecurity
- Pentests, bug bounties and vendor riskSecurity
- Security philosophies: defence in depth, least privilege, assume breach, risk not fearSecurity
- Threat modellingSecurity
- Triaging researcher reports: running a disclosure programme day to daySecurity