Notes
The four roles
OAuth has four roles, and naming them clears up most confusion. The resource owner is the user who owns the data. The client is the app that wants access. The authorization server issues tokens after the user consents. The resource server is the API that holds the data. Example: you are the resource owner, a photo-printing app is the client, Google's login is the authorization server, and Google Photos is the resource server. The app reaches your photos without seeing your password.
- resource owner: the user who owns the data
- client: the app that wants access
- authorization server: issues tokens after consent
- resource server: the API holding the data
- example: you, a print app, Google auth, Google Photos
The code flow
The flow you should use is the authorization code flow. The client redirects the user to the authorization server. The user logs in there and consents. The server redirects back to the client with a short-lived, one-time authorization code in the URL. The client then exchanges that code, from its backend, for the actual tokens. The clever part is that the code seen in the browser is useless on its own: an attacker who captures it cannot complete the exchange, which needs a second step.
- 1. client redirects user to the authorization server
- 2. user logs in and consents
- 3. server redirects back with a one-time code
- 4. client exchanges the code (on its backend) for tokens
- the code is useless to an eavesdropper without the exchange step
PKCE
PKCE, proof key for code exchange, makes the code flow safe everywhere. The client generates a random secret, the verifier, and sends its hash, the challenge, when it starts the flow. When it exchanges the code, it sends the original verifier, and the server checks they match. So a stolen code is useless to anyone without the matching verifier. Originally for mobile and single-page apps, PKCE is now recommended for all clients.
- client makes a random secret (verifier) and its hash (challenge)
- sends the challenge when starting, the verifier when exchanging the code
- server checks they match
- stops a stolen code being used by anyone else
- now recommended for ALL clients, not just mobile
Tokens and scopes
Two kinds of token flow out. The access token is what the client sends to the API; it is short-lived and carries scopes, the specific permissions it grants, such as read-only photos. The refresh token is longer-lived and gets new access tokens without a fresh login, and it can be revoked. Scopes apply least privilege: request only what you need, so a leaked token does little. An API should always check the token's scopes.
- access token: sent to the API, short-lived, carries scopes
- scopes: the specific permissions ("photos.read")
- refresh token: longer-lived, gets new access tokens, can be revoked
- principle of least privilege: request only the scopes you need
OpenID Connect
OpenID Connect is a thin layer on top of OAuth 2.0 that adds the missing piece: authentication. It introduces the ID token, a signed JWT that states who the user is, with claims like a stable subject identifier, email and name. You request it with the openid scope, and there is a userinfo endpoint for additional claims. This, not a raw access token, is the correct way to implement sign in with Google or any federated login. If you need to know who the user is, you want OIDC and the ID token, verified properly.
- a thin layer on OAuth 2.0
- adds the ID token: a signed JWT that says WHO the user is (sub, email, name)
- the "openid" scope requests it
- a userinfo endpoint for more claims
- this, not a raw access token, is how you do "log in with..." correctly
The mistakes
Now the mistakes, which cause real breaches. The old implicit flow returns the token in the URL where it leaks; it is deprecated in favour of code plus PKCE. Skipping PKCE. Not validating state, which protects the flow against cross-site request forgery. Loose redirect URI matching enables token theft; URIs must be exact-matched against an allowlist. Using an access token as login proof instead of the ID token. And not fully verifying the ID token.
- implicit flow (token in the URL): deprecated
- no PKCE
- not validating state (CSRF on the flow)
- open redirect_uri: exact- match, allowlist only access token used as login proof (use the ID token)
- ID token not verified: sig, iss, aud, exp, nonce
Using them safely
A short checklist for using these safely. Always use the authorization code flow with PKCE. Exact-match and allowlist your redirect URIs. Validate the state and nonce parameters. Request minimal scopes. Keep access tokens short-lived and make refresh tokens revocable. Verify ID tokens fully, and do it with a vetted OpenID Connect library rather than parsing JWTs by hand, which is where many mistakes happen. And run all of this through a real identity provider rather than implementing the protocol yourself.
- code flow + PKCE, always
- exact-match redirect URIs, allowlisted
- validate state and nonce
- request minimal scopes
- short access tokens; revocable refresh tokens
- verify ID tokens fully with a vetted library
- use a real identity provider
Common mistakes
The recurring findings, together. The implicit flow, or the code flow with no PKCE. An access token treated as proof of login. Missing state or nonce validation. Loose redirect URI matching that allows token redirection to an attacker. Over-broad scopes that grant far more than needed. An ID token whose signature or claims are not verified. And refresh tokens that never expire or cannot be revoked, so a single theft is permanent. Most of these are avoided by using a good library and provider correctly.
- implicit flow or no PKCE
- access token treated as login proof
- missing state / nonce validation
- loose redirect_uri matching
- over-broad scopes
- ID token signature or claims not verified
- refresh tokens that never expire or cannot be revoked
Say it like this
In the interview: OAuth 2.0 is delegated authorization, granting an app scoped access to resources through tokens, without sharing the password. OpenID Connect adds authentication on top, a signed ID token that tells the client who the user is. For login I use OIDC and the ID token, not a raw access token. I use the code flow with PKCE everywhere, exact-match redirect URIs, validate state and nonce, request minimal scopes, and fully verify the ID token with a vetted library.
- "OAuth 2.0 is delegated
- authorization: access to resources via scoped tokens.
- OIDC adds authentication on top, with a signed ID token
- saying who the user is. I use
- the authorization code flow
- with PKCE, exact redirect
- URIs, state and nonce, and fully verify the ID token."