What it is
OAuth 2.0 (RFC 6749) is a framework for delegated authorization: a user (the resource owner) lets an application (the client) access their data at an API (the resource server) with limited permissions, through tokens issued by an authorization server. The client never sees the user’s password, and the user can revoke the grant later.
OAuth 2.1 is the IETF’s consolidation of OAuth 2.0 with the extensions and security lessons learned since 2012: PKCE (RFC 7636), exact redirect URI matching, no implicit or password grants, and the practices in the OAuth 2.0 Security Best Current Practice (RFC 9700, published January 2025).
Why it exists
Before OAuth, an app that wanted your photos asked for your password at the photo service. It then had full access, forever, and the only way to revoke it was to change your password. OAuth replaces the password with a token that is scoped (only photos, only read), short-lived, tied to one app, and revocable from the user’s account settings.
How it works
Authorization code flow with PKCE, the flow for any client acting for a user:
- Prepare. The client generates a high-entropy
code_verifier, derivescode_challenge = BASE64URL(SHA-256(code_verifier)), and astatevalue bound to the user’s session. - Authorization request (front channel). The client redirects the browser to the authorization endpoint with
response_type=code,client_id,redirect_uri,scope,state,code_challengeandcode_challenge_method=S256. The server checks the redirect URI exactly against the registered ones. - Authenticate and consent. The user logs in at the authorization server (passkey, password and MFA, SSO) and approves the requested scopes.
- Authorization response (front channel). The server redirects back to
redirect_uriwith a short-lived, single-usecodeand thestate. The client checksstateagainst the session. Servers may also returniss(RFC 9207) so a client that talks to several servers can detect mix-up attacks. - Token request (back channel). The client posts
grant_type=authorization_code, the code, the redirect URI and thecode_verifierto the token endpoint, plus client authentication if it is confidential (private-key JWT or mTLS preferred over shared secrets). The server checks the verifier against the stored challenge and returns an access token, usually a refresh token, the granted scope andexpires_in. - API calls. The client sends
Authorization: Bearer <token>. The resource server validates it (locally if it is a JWT access token per RFC 9068, otherwise via introspection, RFC 7662), checks audience and scope, and applies its own authorization for the user. - Refresh. When the access token expires, the client uses
grant_type=refresh_token. Public clients must have refresh tokens rotated or sender-constrained (RFC 9700).
Other grants. Client credentials for machine-to-machine calls with no user. Device authorization grant (RFC 8628) for TVs and CLIs: the device shows a code and the user approves on a phone. Token exchange (RFC 8693) for services that need a downscoped token for a downstream call.
Client types. Confidential clients (server-side apps) can keep a credential secret. Public clients (native and browser apps) cannot, which is why PKCE was invented for them; OAuth 2.1 makes PKCE required for public clients and RFC 9700 recommends it for confidential clients too.
Errors on the API. RFC 6750: 401 with WWW-Authenticate: Bearer error="invalid_token" for missing, expired or bad tokens; 403 with error="insufficient_scope" when the token lacks the scope.
When to use it
- Third-party apps that access users’ data at your API (the original use case).
- Your own apps (web, mobile, CLI) authenticating against a central identity provider, combined with OpenID Connect for login.
- Service-to-service authorization with client credentials and short-lived tokens.
When not to
- A single web app with its own users and no API consumers: a server-side session is simpler.
- As a login protocol on its own: an access token says the client may call an API, not who the user is or that they just authenticated. Use OpenID Connect’s ID token for login.
- Rolling your own authorization server for production: use a mature product or service and certified libraries.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Authorization server separate from apps | One place for login, MFA, consent and revocation | Another critical service; redirects add round trips to login |
| JWT access tokens vs opaque | Local validation, authorization server off the hot path | Revocation waits for expiry |
| Short access tokens | Faster revocation | More refresh traffic |
| BFF (confidential client) vs browser public client | Tokens never reach JavaScript; real client authentication | A backend per frontend; cookie session and CSRF defences |
| Fine-grained scopes | Least privilege per client | Consent screens and scope management get complex |
Alternatives
- Server-side sessions when there is no delegation and only your own web app.
- API keys for simple server-to-server access: easy, but long-lived, unscoped by user and hard to rotate.
- Mutual TLS between services, often combined with OAuth (RFC 8705 binds tokens to the client certificate).
- SAML in older enterprise SSO deployments; OpenID Connect is its common modern replacement.
Failure modes
- Code interception or injection without PKCE.
- Open redirectors and loose redirect URI matching (wildcards, prefix matching) leaking codes to attacker URLs.
- Missing or unchecked state, letting an attacker log a victim into the attacker’s account.
- Tokens in URLs (implicit grant, query parameters) leaking through logs, history and referrers.
- Over-broad scopes or long-lived tokens turning a client compromise into a full account compromise.
- Mix-up attacks in clients that use several authorization servers.
- Using OAuth for login without OpenID Connect, accepting any access token as proof of identity.
- Authorization server outage: no new logins, and failures spread as access tokens expire.
In production
Use authorization code + PKCE everywhere users are involved, and client credentials for machines. Register exact redirect URIs. Keep access tokens short and audience-restricted, rotate refresh tokens, and revoke grants when users disconnect apps. For browser apps, prefer a backend-for-frontend so tokens stay server-side. Consider sender-constrained tokens (DPoP, RFC 9449, or mTLS) for high-value APIs. Monitor the authorization server like the critical dependency it is, and choose access-token lifetimes knowing they set how long an outage is invisible and how long revocation takes.
Common misconceptions
- “OAuth is an authentication protocol.” It is delegated authorization. OpenID Connect adds authentication.
- “PKCE is only for mobile apps.” It began there; current guidance applies it to all clients.
- “Scopes are permissions on data.” They limit the client. A token with
photos:readstill must not read someone else’s photos. - “The access token is for the client to read.” To the client it is opaque; only the resource server should interpret it. Use the ID token for user information.
Interview angle
Expect to draw the authorization code flow with PKCE, labelling front channel (redirects carrying the challenge, code and state) and back channel (code and verifier for tokens). Explain what PKCE and state each stop, why implicit and password grants are gone, how scopes and audiences limit tokens, refresh token rotation, and what happens when the authorization server is down. Bonus points for BFF, DPoP and the difference between OAuth and OpenID Connect.
Model assumptions
The flow is a scripted illustration, not a simulation: redirects, consent, the code exchange, PKCE checks, refresh and the implicit-grant leak are hand-placed packets with illustrative one-way durations (8 ms browser to client, 14 ms browser to authorization server, 6 ms client to authorization server, 5 ms client to API). The engine has no model of browsers, redirects or cryptography. The outage step is simulated: the client app sends Poisson-distributed API calls at 200 per second to a resource server with 32 workers and 3 ms mean service time (lognormal, cv 0.3), which validates JWT access tokens locally, so the authorization server is not on the path; its failure is modelled as the node going offline. Token expiry during the outage is described, not simulated. Numbers are illustrative.