Security and auth / Intermediate / 6 min / Updated
OAuth 2 and OpenID Connect: Sign in with Google
Step through the authorization code flow, then add an attacker. PKCE stops stolen codes, state stops login CSRF, and the implicit flow leaks tokens.
Start the simulation → Change the load, break a part, watch what happens. Free, no sign-up.
In short
What is OAuth and OIDC?
OAuth 2 lets an app act for a user without their password; OpenID Connect adds an ID token that says who signed in.
Why does it matter?
Users trust one provider with their password. The app gets a short-lived code, swaps it for tokens, and never sees credentials.
What is the trade-off?
More redirects and moving parts than a password form; PKCE and state are mandatory details, and token storage still matters.
How do you explain it in an interview?
"Authorization code flow with PKCE for web and mobile, state against CSRF, validate the ID token, short access tokens, refresh in an httpOnly cookie."
Check yourself
Why does the app swap the code for tokens over a back channel instead of getting tokens in the redirect?
Answer: Tokens in the URL leak through history, logs and Referer headers. The code is short-lived and useless alone; the real tokens never touch the URL. That is why the implicit flow is deprecated.
A malicious app intercepts the authorization code. What makes the code useless to it?
Answer: PKCE: the token request needs the code_verifier only the real app knows. The app sent a hash of a secret verifier at the start; the provider only issues tokens to whoever proves the verifier.
What does OpenID Connect add on top of OAuth 2?
Answer: An ID token that says who the user is. OAuth grants access; OIDC adds identity: a signed ID token with sub, email, audience and expiry.