Skip to content

Interview question · Identity, OAuth & Access Control

How does the OAuth 2.0 authorization code flow with PKCE work, and what attacks does each part stop?

Drawing the arrows is half the answer. The other half is pinning an attack to every parameter: state, redirect URI, code, verifier, audience.

mediumEssentialNewPreparation tags: Google, Microsoft, SalesforceUpdated Nov 2026

TL;DR: The client sends the user to the authorization server with scopes, a state value and a PKCE code_challenge (hash of a secret code_verifier). The user logs in and approves; the server redirects to the exactly registered redirect URI with a short-lived, single-use code. The client exchanges code plus verifier for tokens; the server issues them only if the verifier matches the challenge. Each part stops something: state stops CSRF, exact redirect matching stops code theft via open redirects, PKCE stops intercepted or injected codes being redeemed, and audience and scope checks stop tokens being misused elsewhere.

How to approach it

Draw the roles (user, client, authorization server, resource server), walk the flow in six steps, then do a second pass where each element gets the attack it prevents. Close with current best practice: RFC 9700 (January 2025).

A strong answer

FIG · OAUTH 2.0 · AUTHORIZATION CODE WITH PKCERFC 6749 · RFC 7636
A code in the browser, tokens only for whoever holds the verifier

Client app

holds code_verifier

Browser

user signs in

Auth server

issues code, tokens

  1. Front channel
  2. Client app to Browser

    1Redirect: state, code_challenge
1/5

1. Redirect: state, code_challenge

Also carries the requested scope. challenge = SHA-256(code_verifier); state ties the response to this browser session.

The flow.

  1. Client generates a random code_verifier, computes code_challenge = SHA-256(verifier) (method S256), and generates state.
  2. Browser is redirected to the authorization endpoint with client_id, redirect_uri, scope, state, code_challenge.
  3. User authenticates at the authorization server and consents to scopes.
  4. Server redirects to the registered redirect_uri with code and state; the client checks state matches what it stored for this browser session.
  5. Client posts code and code_verifier (plus client authentication if confidential) to the token endpoint.
  6. Server verifies the verifier hashes to the challenge, the code is unused and unexpired, and the redirect URI matches; it returns an access token (and possibly a refresh token).

What each part stops.

ElementAttack it stops
User authenticates at the authorization serverClient ever seeing the password
Scopes and consentOver-broad access the user did not approve
state bound to the client sessionCSRF: attacker's code delivered into the victim's session
Exact redirect URI matchingCodes leaked to attacker URLs via loose matching or open redirects
Short-lived, single-use codeReplay of a captured code
PKCE verifier and challengeRedemption of an intercepted code (mobile scheme hijack, logs, referrers) and code injection
Client authentication at the token endpointImpersonation of confidential clients
Audience and scope validation at the APITokens for one API being accepted by another

Current best practice (RFC 9700). Use the authorization code flow with PKCE for all clients, including server-side ones; match redirect URIs exactly; do not use the implicit grant or the resource owner password grant; prefer sender-constrained tokens (DPoP or mTLS) where token theft is a concern; and keep refresh tokens rotated or sender-constrained for public clients.

Agents. An agent is just another OAuth client. Request the minimum scopes per feature, store refresh tokens like any other secret, and make every downstream API check the token's audience so a token cannot be replayed elsewhere.

What interviewers probe next

  • "Why does a confidential server-side client need PKCE if it has a client secret?" PKCE also defeats authorization code injection, where an attacker plants a stolen code into a victim's session; the secret does not.
  • "What is a mix-up attack?" With multiple authorization servers, an attacker tricks the client into sending a code to the wrong server; mitigate with the iss parameter in responses and per-server redirect URIs.
  • "Where should the SPA store tokens?" Ideally not in JavaScript-accessible storage; a backend-for-frontend holding tokens server-side with an HttpOnly session cookie is the safer pattern.

What if?

What if · interview follow-ups

Hold your answer as the constraints change

A single-page app and a mobile app let users sign in through an OAuth authorisation server and call your API with the resulting tokens.

  1. Start here

    How does the authorisation code flow with PKCE work?

How it's scored

Weak: Describes OAuth as login, or walks through the implicit flow.

Hire: Gives the code flow with PKCE correctly and explains state and PKCE.

Strong Hire: Maps each element to an attack, cites current best practice, and covers mix-up, code injection and token storage for browser clients.

Common mistakes

  • Using the plain PKCE method instead of S256.
  • Prefix or wildcard matching of redirect URIs.
  • Not binding state to the user's session.
  • APIs that accept any token from the provider without checking audience.
You just read a free answer. Every topic opens its first 5 answers to anyone. Sign in and the count per topic goes to 10, and we keep track of how far you get in every topic.Google sign-in · free · no payment details
HOW DID IT GO?
0
READING SIGNED OUT

Signing in doubles your free answers, from 5 to 10 per topic, and the site starts remembering you: mastery per topic, bookmarks, and a next-focus recommendation. Free, no card.

Sign in free
UP NEXT ON YOUR JOURNEY
DISCUSSION · 0

No comments yet. Share a correction or a different way to answer it.