Authentication and authorization
Read first: API design
Authentication names the caller, and authorization decides what that caller may do. OAuth delegates access, OpenID Connect adds an identity token, and a session cookie can be revoked by deleting a row. A JWT is signed, not encrypted, and a long-lived one keeps working after you wish it would not.
Authentication answers who is calling. Authorization answers what that caller may do. A real token with the wrong check still reads someone else’s order. A login box with no check on the resource is a costume. Get the two words apart before you pick a token format, because the format does not answer the second question for you.
Core concepts
The two questions. The API sees a request. Authentication is the step that binds it to a principal: this user, this service, this anonymous client. Authorization is the step that looks at the resource and decides. Ada may read order 123 because the order row says she owns it. A valid token that says “this is Ada” does not by itself say she may read order 456. The service that holds the row does that check. An edge gateway can reject a missing or forged credential. It should not be the only place that asks whether Ada owns the order, because some calls will reach the service another way, and the gateway does not have the row.
OAuth 2.0 is delegation. The user, or a service, grants a client a limited ability to call an API. It is not “login” by itself. Login is how the user proves who they are to the authorization server. The grant is what the client walks away with. Mixing those up is how a design ends at “we use OAuth” and still cannot say what a stolen credential is allowed to do.
Authorization code, with PKCE. This is the grant for a user in a browser, or in a mobile app, where the client is public and cannot keep a secret. The shape is:
- The client generates a random verifier and keeps it. It sends the authorization server a challenge derived from that verifier, plus the scopes it wants. A hash is the usual derivation. Anyone who sees the challenge still cannot produce the verifier.
- The user authenticates at the authorization server and consents to the scopes.
- The server redirects the user back to the client with a one-time code.
- The client sends the code and the verifier. The server checks that the verifier matches the challenge it stored, then returns an access token.
- The client calls your API with that access token.
PKCE exists because the code comes back through the user’s device, where another app may see it. A public client has no client secret to prove itself when it redeems the code. Without the verifier, a stolen code is enough. With PKCE, the stolen code is useless to anyone who does not have the verifier that never left the real client. That is the whole mechanism. You do not need a longer speech about it.
Client credentials. This is the grant for a service, not a user. The payment service proves its own identity to the authorization server and receives an access token whose scope is the call it needs to make, such as posting a ledger entry. There is no browser and no consent screen. The token is the service. A stolen service token is the service, until it expires or you can revoke it. Do not reuse a user-shaped token so that a backend can “act as” whoever is convenient. Scope the grant to the operation. If the ledger accepts a token that means “any user,” you have handed the network a skeleton key and called it delegation.
The identity token. OpenID Connect adds an identity token on top of OAuth. It is how the client learns who authenticated: a signed set of identity claims, produced because the user signed in. It is not the access token. The access token is what the API checks before it does the thing. The identity token tells the client who the person is so it can say hello, or decide which account picker to show. A common bug is to send the identity token to the API and treat “we know who this is” as “they may do this.” Those are the two questions again. Keep the tokens in the jobs they have.
Session cookie versus JWT. A session cookie holds an opaque id. The browser sends it back. The server looks the id up in a store. Logout, or a stolen-device response, deletes the row. The next request presents an id that no longer resolves, so it fails. The cookie is a pointer. It is not a bag of claims. Stealing it is still serious until you delete the row, and you can delete the row. That is the property you are buying. You pay a store read on the authenticated path, and the store has to be there.
A JWT is a signed set of claims. The resource server checks the signature with a key it trusts, checks that the token has not expired, and checks that the token was meant for this API. If the signature verifies, the claims are the ones that were signed. The caller cannot edit the user id and keep a valid signature. Signing is not encryption. The payload is only encoded so it can travel in a header. Anyone who has the token can read every claim. Do not put a secret in the payload: not an API key, not a password, not a card number, not an internal flag you would be embarrassed to see in a log. Encoding is reversible by anyone holding the bytes. Encryption would be a different, additional mechanism. The default JWT is not that mechanism.
Revocation is the sharp edge. You cannot take a JWT back until it expires, unless you check a denylist on the way in. A denylist that you consult on every request, and that you update when you want a token to die, is a session store you just reinvented. You now have the lookup you adopted a JWT to avoid, plus the signature machinery. Keep access tokens short so the window where a stolen one still works is small. Put the long-lived, revocable piece in the store, as an opaque grant the client trades for a fresh short access token. Logout deletes the grant. The access token, if one is already in the wild, dies on its own clock, and you chose that clock to be short enough that you can stand the wait.
Changing the signing key invalidates every outstanding token at once, because none of them verify anymore. That is a consequence of “signed with a key you trust,” and it is a blunt one: you log out the whole fleet to evict one thief. It is not a substitute for a short lifetime.
flowchart LR
user[User]
client[Public client]
authz[Authorization server]
api[Resource server]
user --> client
client --> authz
authz --> client
client --> api
The user proves who they are to the authorization server. The client leaves with an access token. The resource server checks that token and then authorizes against the row. The identity token, if you asked for one, stops at the client. It is not drawn as the thing the API trusts, because it should not be.
A worked example
You issued access tokens as JWTs with a 30-day lifetime. You did it so the mobile client would not have to refresh, which felt like a kindness. The payload carries the user id, a role, and, because it was convenient, an internal support flag and the user’s email. Nothing in the token is encrypted. A client put the token in a query string. Your access log recorded the line. Assume logs are retained 14 days. That retention is an assumption about your logging choice, not a standard.
On day one, someone who can read the log copies the token. They do not need to forge a signature. The token is already valid, and it already says they are the user. They can also read the email and the support flag, because signing never hid the payload. The flag was not a secret you meant to publish. It became one the moment it was a claim.
How long does the copy work?
1 day = 86,400 seconds
30 days * 86,400 = 2,592,000 seconds
a short access token you might have chosen instead
15 minutes * 60 = 900 seconds
2,592,000 / 900 = 2,880
The stolen 30-day token is valid for about 2.9 million seconds. The 15-minute token is 900 seconds. The long one is 2,880 times the exposure. Fifteen minutes is a choice you state, not a magic constant. Thirty days was also a choice, and it was the wrong one. Logout does nothing to the copy. The user can change their password and the JWT still verifies, unless the password change also rewrites something the resource server checks. If the server only checks the signature and the expiry, the password change is theater. The log line keeps working for the rest of the month, and your 14-day log retention means the token is sitting in the log for two weeks of that month even if the thief has not fetched it yet. A bearer token in a query string will also leak into the next site’s referrer and into any intermediate that logs URLs. Put tokens in a header, and do not log that header.
What you wanted instead is a split. The client holds an opaque refresh grant, stored server-side, that you can delete. It trades that grant for an access JWT that lives 15 minutes. You still do not put secrets in the access token. You put the user id and the expiry and the audience, meaning which API it was minted for, and nothing you would mind a log line exposing, because someday a log line will. When the laptop is stolen you delete the grant. The access token, if it was copied too, works for at most the 900 seconds you already decided you could tolerate. You do not build a denylist of every access token unless you are willing to look at that list on every request. That lookup is the session store again. Prefer the short clock.
A session cookie on the same product has a duller story, and the dull story is the point. The browser holds an opaque id. The server holds the row: user id, expiry, the fact that this is a web session. The thief who copies the cookie is also you, until someone deletes the row. The difference is that delete works immediately, including for the copy. You pay the store read on each request. At the QPS of a normal product that read is a cacheable key lookup, not a reason to accept a month-long bearer token. If you cannot be bothered to delete rows, the cookie has the same revocation problem as the JWT and you have bought nothing. The mechanism works only if logout and “lost device” actually remove the row.
The service-to-service version of the same mistake: the payment service calls the ledger with a user access token it stored, so the ledger “sees the user.” A bug in payment, or a log line in payment, now yields a token that can do whatever that user can do, on every API that accepts it, for 30 days. Client credentials avoid that. Payment gets a token for “post this ledger entry,” with a short lifetime, and the ledger checks that scope. Authorization is the scope plus the account id in the request, not a vague sense that the caller is internal.
If you do add a denylist for the long tokens you already shipped, every region that accepts the token has to see the revocation. A list that replicates late is a window where the stolen token still works. That window is a consistency lag, the same family of problem as any other replicated store. You did not escape shared state. You renamed it.
Tradeoffs
| Choice | What you gain | What you spend |
|---|---|---|
| Opaque session cookie | Revocation is a delete of one row | A store read on the authenticated path |
| Short-lived signed JWT | The API can check a signature and an expiry with no session read | A stolen copy works until it expires, so the lifetime is the damage |
| Long-lived JWT | The client refreshes rarely | The stolen-token window is the whole lifetime, and logout does not close it |
| Authorization code with PKCE | A public client can take a user grant without embedding a secret | A redirect and a verifier you must actually check |
| Client credentials | The service acts as itself, with a scope you can name | A stolen service token is the service, so keep it short and narrow |
| Identity token presented as the API credential | One token for the client to stare at | You authorized a login proof, not a grant for this resource |
The honest tradeoff on the web is the cookie. You can delete it. The honest tradeoff on an API client that should not call you on every keystroke for session state is a short JWT, minted from a grant you can delete, with claims you are willing to show the world. A 30-day JWT is neither tradeoff. It is the downside of both.
Pitfalls
- Treating a valid token as authorization. The symptom is Ada reading Bob’s order, because the handler checked the signature and then used the order id from the path without looking at the owner.
- Calling OAuth “login” and stopping there. The symptom is a design that cannot say which grant you used, or what a stolen token is allowed to call.
- A public client with the authorization code and no PKCE. The symptom is a code intercepted on the device, redeemed by someone else, because there was no verifier to demand.
- Sending the identity token to the API. The symptom is an API that knows who signed in and never checks the access grant or the row.
- A secret, a card number, or an internal flag in the JWT payload. The symptom is that value showing up in a log, a trace, or the attacker’s notebook, still “protected” by a signature that does not hide anything.
- A 30-day access token. The symptom is the worked example: logout and password changes do nothing to the copy, for millions of seconds.
- The token in a query string. The symptom is the credential in access logs, in browser history, and in the referrer of the next navigation.
- Logging the Authorization header. Same symptom, different file.
- Client credentials that are really a user’s token stored on the server. The symptom is one service bug becoming that user, on every API, for the token’s whole life.
- A denylist you forget to consult on the internal path. The symptom is a revoked token that still works the moment the caller is another service.
In the interview
In the first five minutes, separate the words and name the grant. “The browser session is an opaque cookie I can delete. The mobile client uses authorization code with PKCE, and it calls the API with a short access token, on the order of minutes, minted from a refresh grant I can also delete. Services call each other with client credentials, scoped to the operation, not with a user’s token. The order service still checks that this principal owns the row.” That is a path. The number is the lifetime. The bottleneck is the session lookup you accepted on the web, or the expiry window you accepted on the JWT. The tradeoff is revocation versus a store read.
Skip cipher suites. Skip a tour of every OAuth grant you have heard of. The two you need are authorization code with PKCE, and client credentials. Mention the identity token only to say you will not accept it as the API credential.
The question that usually follows is the stolen token. Give the 30-day arithmetic if you have a long lifetime in the design, or say you refused that lifetime. 30 times 86,400 is about 2.6 million seconds, and a 15-minute token is 900, which is 2,880 times smaller. Then say what you can actually do: delete the refresh grant, wait out the short access token, and do not pretend a signature is a kill switch. If they ask whether to put roles in the token, say yes only for roles you can stand to be stale for the token’s whole life, and no for anything that is a secret. A role change that must take effect immediately belongs in the store you can write, not in a claim you cannot reach.
Check yourself
What is the difference between authentication and authorization, on one order read?
Authentication says the caller is Ada, because the cookie resolved or the token’s signature and expiry checked out. Authorization says Ada may see this order, because the row’s owner is Ada, or because a scope covers this operation. A valid token for Ada is not permission to read Bob’s order. The service that holds the order is the place that can tell those apart.
Why does a public client use PKCE on the authorization code grant?
The client cannot keep a secret, and the one-time code comes back through the user’s device, where it can be intercepted. The client sent a challenge up front and keeps the verifier. The authorization server will not trade the code for a token unless the verifier matches. A stolen code without the verifier does not redeem. That is the entire reason PKCE is there.
A stolen JWT is valid for 30 days. A short one is valid for 15 minutes. How many times longer is the exposure?
A day is 86,400 seconds, so 30 days is 2,592,000 seconds. Fifteen minutes is 900 seconds. 2,592,000 divided by 900 is 2,880. Logout does not shrink the long window unless you also keep a denylist, and a denylist consulted on every call is a session store. Prefer the short lifetime, and keep the revocable grant in a store you can delete from.
The payload contains an API key, and the token is signed. Is the key protected?
No. The signature stops the caller from changing the claims without detection. It does not hide them. Anyone who copies the token can read the payload. An API key in a claim is now in every log, proxy, and stolen laptop that saw the token. Put nothing in the payload that you would not put on a billboard. Keys belong in a secret store, not in a bearer token’s body.
Based on: OAuth 2.0; OpenID Connect; Designing Data-Intensive Applications, Kleppmann and Riccomini.