OAUTH 2.0

Delegated authorization without sharing passwords

L218 MINSECURITY
PREDICT

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.

▶ SIMULATION
ALICE'S BROWSER
pending: —
signed in as: —
tax-return.pdf saved to: —
AUTH SERVER: CODES
—
requires PKCE: no · client checks state: no
ATTACKER
own code: —
token for: —
refused by client or AS: 0
last—
redemptions—
✓ a code is redeemed only by the client session that started its flow✓ alice's browser is never signed in to someone else's account
SCHEDULE
01[CLIENT]client: no state, no PKCE
02[ATTACKER]attacker starts a login in their own browser
03[ATTACKER]attacker consents as mallory and keeps the code instead of using it
04[ATTACKER]attacker's page sends alice's browser to /callback?code=<mallory's code>
05[ALICE]alice uploads tax-return.pdf to the account she is signed in to
06[CLIENT]report
Start of no state: login csrf scenario. 6 steps.
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.

BREAK IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

The code is a bearer ticket that travels through a browser you do not control. Bind both ends of the trip to the client that started it: state checks that the code coming back belongs to this browser’s login, and PKCE checks at the token endpoint that the code is redeemed by whoever started it, which also blocks an injected code when the server enforces PKCE. An access token authorizes; for identity, use the OpenID Connect ID token.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: DIFFIE-HELLMAN →