OAuth2 and OpenID Connect
Authorization code with PKCE, client credentials, the roles of each party, and why a backend rarely needs the password grant.
OAuth 2.0 is an authorisation framework: it lets one application act on a user's behalf at another, without the user handing over their password. OpenID Connect is a thin layer on top that adds authentication — telling you who the user is.
That distinction is the one to hold, because the two are constantly conflated, and the whole of OAuth makes more sense once you notice that plain OAuth 2.0 never actually tells you who anybody is.
Four parties, and naming them correctly
Almost every OAuth confusion is a party confusion:
| Party | Who it is | In a typical Java system |
|---|---|---|
| Resource owner | the human | your user |
| Client | the thing wanting access | your web or mobile front end |
| Authorization server | issues tokens | Keycloak, Auth0, Okta, Entra ID |
| Resource server | holds the data | your Spring Boot API |
Note the last row. In most backend work you are the resource server — you receive and validate tokens, and you do not issue them. The previous lesson was that job in full. Everything below is about how the token got made.
Note also that the "client" is not your backend. It is the thing the user is using.
The authorization code flow, with PKCE
This is the flow. The others are special cases or deprecated.
- The user clicks Sign in. The client redirects the browser to the authorization server, with its
client_id, thescopeit wants, aredirect_uri, a randomstate, and acode_challenge. - The user authenticates at the authorization server — the client never sees the password, and that is the entire point of the exercise.
- The authorization server redirects back to
redirect_uriwith a short-lived authorization code. - The client exchanges that code for tokens, sending the
code_verifierthat matches the challenge from step 1. - The client receives an access token, usually a refresh token, and — with OIDC — an ID token.
Two of those parameters are security mechanisms rather than plumbing:
stateis a random value the client generates and checks on return. It ties the response to the request the client actually started, which is CSRF protection for the login itself.- PKCE (
code_challenge/code_verifier) proves the party redeeming the code is the party that requested it. It was designed for mobile apps that cannot keep a secret, and it is now recommended for every client, including confidential ones. Use it always.
The reason for the two-step dance — a code, then an exchange — is that the code travels through the browser, where it lands in history and logs, while the token is fetched over a direct back-channel call. A code is useless without the verifier and expires in seconds.
The grants, and which to use
| Grant | Use it |
|---|---|
| Authorization code + PKCE | anything with a user: web, mobile, SPA |
| Client credentials | service to service, no user involved |
| deprecated — returned tokens in the URL fragment | |
| deprecated — the client handles the password, defeating the point | |
| Device code | TVs and CLIs, where typing a password is impractical |
| Refresh token | exchanging a refresh token for a new access token |
Client credentials is the one a backend engineer uses most after being a resource server. Your batch job calls another service; there is no user; the job authenticates as itself with a client id and secret and gets a token scoped to what it may do. Least privilege from the authorisation lesson, applied to a machine.
The two deprecated rows are worth recognising in an old codebase. The password grant in particular looks convenient — the app collects a username and password and exchanges them — and it reintroduces exactly the problem OAuth exists to remove: an application handling credentials it has no business seeing.
OIDC: the ID token, and what each token is for
OpenID Connect adds an ID token: a JWT about the user, for the client.
Three tokens, three jobs, and mixing them up is a real class of bug:
| Token | Audience | Says | Send it to your API? |
|---|---|---|---|
| Access token | the resource server | what the bearer may do | yes |
| ID token | the client | who the user is, and how they authenticated | no |
| Refresh token | the authorization server | may be exchanged for a new access token | no |
The ID token is for the client to learn who signed in — a name to show, a subject id to key on. Sending it to an API as a bearer token is a common mistake: it was issued with the client as its audience, so a correctly configured resource server rejects it, and an incorrectly configured one accepts a token that was never meant for it. This is the aud check from the previous lesson, and this is what it is defending against.
An access token may or may not be a JWT, incidentally. It can be an opaque string, in which case the resource server validates it by calling the authorization server's introspection endpoint — a network call per request, with revocation that is immediate. JWT or introspection is the same stateless-versus-stateful trade as sessions and tokens, one layer up.
Scopes are not permissions
A scope says what the user consented to let this client do — orders:read. It is not the same as what the user is allowed to do.
A token with orders:read does not mean the holder may read every order. It means the client is permitted to ask about orders on this user's behalf; which orders is still your authorisation decision, from the object and its owner.
Treating scope as authorisation reproduces exactly the broken-object-level-authorisation bug from the fundamentals lesson, with a token in front of it.
As a resource server, in practice
For most Java backends the work is small, because the previous lesson is the whole job:
spring.security.oauth2.resourceserver.jwt.issuer-uri=https://auth.example.comPlus an audience check, a claims-to-authorities mapping, and ownership checks in your services. Do not build an authorization server. Issuing tokens, storing consent, rotating keys, implementing the flows and keeping up with the specifications is a product, and there are several good ones — Keycloak if you want to run it yourself, a hosted provider if you do not.
Under the hood: what PKCE actually defeats, and introspection versus local validation as a real tradeoff
PKCE (RFC 7636, "pixy") exists to close one specific gap: the authorization code, on its way from the authorization server back to the client, travels through the browser — a redirect URL, which lands in browser history, in a proxy's access log, potentially in a referrer header if the redirect page loads any third-party resource before your code grabs the parameter. A native or mobile app has an additional exposure the web case does not: on Android and older iOS versions, the OS-level redirect back into the app (a custom URL scheme rather than a universal link) could in principle be intercepted by another app registered for the same scheme. Before PKCE, anyone who captured that code in transit could redeem it themselves, at the token endpoint, and receive tokens for a session they never authenticated. The mechanism is a proof of possession spread across the two ends of the flow: at the start, the client generates a random code_verifier (RFC 7636 specifies 43–128 characters from a defined unreserved set) and sends the authorization server only a code_challenge — with the S256 method, BASE64URL-ENCODE(SHA256(code_verifier)), a one-way transform. (S256 is the method to use; if a client sends no code_challenge_method at all, RFC 7636 falls back to plain, where the challenge is the verifier and an observed challenge protects nothing.) The authorization server records that challenge against the authorization code it is about to issue. When the client later redeems the code at the token endpoint, it must also send the original code_verifier in plain text; the authorization server recomputes the SHA-256 and compares it to the code_challenge it stored. An attacker who captured only the authorization code — the thing that actually travelled through the exposed redirect — has no way to produce a code_verifier whose hash matches the challenge, because the verifier itself never appeared anywhere the code did. The code becomes useless without a secret that never travelled the same path. That is also the answer to why PKCE is now recommended for confidential clients too, not just public ones that cannot keep a client secret: it defends the code, which travels through the browser for every client type, and a client secret does nothing to protect that specific leg of the journey.
Introspection versus JWT-local validation is the same stateless-versus-stateful trade the sessions-and-tokens lesson names for cookies versus JWTs, one layer up the stack, and it is worth stating plainly because "just use a JWT" is not free. An opaque access token carries no information the resource server can read on its own; every request means a network call to the authorization server's introspection endpoint (RFC 7662) to ask "is this token still valid, and for what." That call costs latency and makes the authorization server a dependency of every request path in every resource server, but it buys real revocation: the authorization server can mark a token dead and the very next introspection call reflects that immediately, no waiting for an expiry. A JWT access token, validated locally by fetching the issuer's public keys once and checking the signature yourself, costs nothing per request beyond the signature check — no network call, no dependency on the authorization server being reachable — but it inherits exactly the "valid until exp, whatever happens to the account" property the previous lesson already walked through for tokens in general: revoking the underlying grant does not retroactively invalidate a JWT already issued from it. Systems that want both properties commonly keep access tokens short-lived and JWT-local (fast, no per-request dependency, tolerable blast radius because the window is small) while keeping the refresh token introspectable or otherwise revocable server-side — the same access/refresh split the sessions-and-tokens lesson describes, now visible as the reason OAuth's token design looks the way it does rather than as an arbitrary convention.
Try it yourself
Work out what an intercepted authorization code buys an attacker, with and without PKCE
A public SPA starts the authorization code flow. Its code_verifier is generated and held only in the SPA's own memory — never sent anywhere until the token exchange. An attacker manages to read the browser's history and finds the redirect URL, which contains the authorization code and the state value, in plain text.
Before reading on: can the attacker now obtain tokens by sending that code to the token endpoint themselves? What, specifically, stops them (or fails to)?
The code alone is worthless once the token endpoint requires the matching verifier
Without PKCE, yes — the authorization code is a bearer credential at that point: whoever presents it at the token endpoint (with the right client_id and redirect_uri) gets tokens, and the attacker has everything required. With PKCE in place, the token endpoint also requires code_verifier, and the attacker's history capture gave them the code and state — both of which travelled through the same exposed redirect — but not the code_verifier, which by design never appears in any URL, redirect, or browser-visible location at all; it lives only in the SPA's in-memory JavaScript state until the direct back-channel token request. The attacker can present the correct code and still cannot produce the one value the authorization server will check it against, so the exchange fails. This is the concrete version of "PKCE proves the party redeeming the code is the party that requested it": requesting it is what generated the verifier in the first place, and nothing about capturing the code after the fact reproduces that.
Misconceptions
- "PKCE replaces the client secret, so confidential clients don't need it." PKCE and a client secret defend different legs of the flow. The client secret (where one exists) proves the token exchange request itself comes from a registered, trusted client. PKCE proves the party redeeming a specific authorization code is the same party the code was issued to — a property a static secret says nothing about, since a secret doesn't change per-authorization the way a
code_verifierdoes. That is why PKCE is recommended for every client type today, secret or not. - "Introspection is strictly more secure than JWT validation, so prefer it by default." Introspection buys immediate revocation at the cost of a network round trip to the authorization server on every single request — including a dependency: if the authorization server is briefly unreachable, every resource server using introspection is affected, not just the authorization flow. JWT-local validation has no such dependency and no per-request latency cost, at the cost of not being instantly revocable. Neither is unconditionally the safer choice; it is a real tradeoff to make deliberately; per the access/refresh split, and it is common to accept slower revocation on a short-lived access token while keeping the longer-lived refresh token revocable.
Sources
- RFC 6749 — The OAuth 2.0 Authorization Framework.
- RFC 7636 — Proof Key for Code Exchange by OAuth Public Clients (PKCE).
- RFC 7662 — OAuth 2.0 Token Introspection.
- OpenID Connect Core 1.0 specification.