Token formats and validation
Before the photo APIAn application programming interface: a defined way for software to interact with a service. A web API offers endpoints where other programs send HTTP requests. can decide whether the printer may see album 42, it has more basic questions to answer about the token in the request. Did the photo service's authorization serverIn OAuth, the service that handles a person's sign-in and approval and decides which tokens to issue to a client. It may belong to the same company as the API, but its job is separate. issue it? Was it issued for this API? Is it still valid? How the API answers depends on what the token contains, and this lesson compares the two formats and follows the checks for the self-contained one.
Reference and self-contained tokens
The printer's request reaches the photo API with a token in its Authorization header. Some tokens carry nothing but a reference. Others carry their own description, signed by the authorizationDeciding whether a requested action is permitted, such as whether a signed-in account may delete a particular photo. Authentication says which account is asking; authorization decides what it can do. server.
A reference token, often called an opaque token, is a random string that points to a record kept by the authorization server. demo-access-token-7 could be one, and only the authorization server knows what it stands for. An API that receives one has to ask the authorization server what it means, which the Token introspection lesson covers.
A self-contained token carries that information itself. The common format is a signed JWTA JSON Web Token: a compact token made of Base64url-encoded parts that carries claims and is usually signed. Anyone holding it can decode and read the claims, so the signature shows who issued it but hides nothing.. The authorization server writes the claims and signs them with its private keyThe half of a key pair that its owner keeps to themselves and uses to create signatures or decrypt data. Anyone who obtains it can act as its owner., and any API holding the matching public keyThe half of a key pair that can be shared freely. It checks signatures made with the matching private key and can encrypt data that only that private key can decrypt. can check the token without contacting anyone.
Neither format is better everywhere:
| Consideration | Reference token | Self-contained JWT |
|---|---|---|
| Size | A short random string. | Hundreds of characters or more, sent with every request. The example below is 423. |
| Who can read it | Only the authorization server knows what it means. | Anyone holding it can decode the claims, including the client and anything that logs it. |
| Revocation | Takes effect at the API's next check with the authorization server. | An API that validates locally accepts the token until it expires. |
| Dependence on the authorization server | Every check needs the authorization server to answer, unless the API caches the result. | The API needs only the issuer's public keys, so it keeps working through a short outage. |
The revocationCancelling a credential, token, certificate, or authenticator before it would expire, so that it is no longer accepted. Whether every copy stops working at once depends on how services check it. row is the one people discover in production. If Ava disconnects the printer at 09:02 and its JWT expires at 09:10, an API that validates locally keeps accepting that token for eight more minutes, because nothing in the token has changed. Short lifetimes keep that window small.
The privacy row is easy to overlook too. A JWT carrying Ava's email address would hand it to every client that received the token, so the authorization server chooses what goes into a self-contained token knowing who can read it.
The JWT access token profile
Inside a JWT took a token apart into its header, payload and signature and listed the registered claims. JWT access tokens add a standard profile on top, so APIs and authorization servers agree on what each claim means in an access tokenA token that a client presents to an API to use access it has been granted, usually limited in what it allows and how long it lasts. It authorizes API calls; it does not tell the client who signed in.. If the photo service issued JWTs, the printer's token, shown in Exchanging a code for tokens as the stand-in demo-access-token-7, would look like this, shortened for display. All domains, keys and tokens in these examples are fictional.
eyJhbGciOiJFUzI1NiIsImtpZCI6InBob3Rvcy0yMDI2LTA5IiwidHlwIjoiYXQrand0In0
.eyJpc3MiOiJodHRwczovL2F1dGgucGhvdG9zLmV4YW1wbGUiLCJzdWIiOiJ1c2VyLTIwNDgi...
.(signature)The line breaks are for display. Decoded, its header and claims read:
Header:
{
"alg": "ES256",
"kid": "photos-2026-09",
"typ": "at+jwt"
}
Claims:
{
"iss": "https://auth.photos.example",
"sub": "user-2048",
"aud": "https://api.photos.example",
"client_id": "photo-printer",
"scope": "photos.read",
"iat": 1790845200,
"exp": 1790845800,
"jti": "demo-token-id-7"
}The header's typ, at+jwt, is the profile's own value: it declares that this JWT is an access token, not some other kind of token from the same issuer.
The claims describe the access. iss is the authorization server's issuer identifier. sub is the subject, here Ava's account at the photo service, user-2048; in a token a client obtained for itself with the client credentials grant, it normally identifies the client instead. aud, the audience, names the API the token is for, and client_id the client it was issued to, so a token carries both whose access it is and which application is using it. scope is the space-separated list of granted scopes. iat and exp are 09:00 and 09:10 UTC on 1 October 2026, and jti gives this token a unique identifier.
The profile requires every one of those claims except scope, which a server should include whenever the client requested one.
Validating step by step
Reading a token is not validating it, as Protecting credentials and messages first put it. The JOSE lessons set out how a receiver gets from a decoded JWT to an accepted one, and the photo API applies those checks with its own expected values, in this order, before it trusts any claim:
- Accept only the expected algorithm. The API's configuration says
ES256for this issuer, and a token naming anything else,noneincluded, is refused. Choosing and enforcing algorithms explains why the header never chooses. - Check the type.
typmust beat+jwt, or its long formapplication/at+jwt. The photo service also signs the ID tokens OpenID ConnectAn identity layer on top of OAuth 2.0 that lets an application sign people in with an account at another service. It adds the ID token, standard claims, and discovery to the OAuth exchange. gives clients after sign-in, and publishes their keys in the same key setA JSON document, published at a fixed HTTPS address, that lists an issuer's current public keys so others can verify its signatures. It never contains private keys.. It happens to sign them with a different algorithm, but nothing requires that, so only this check reliably keeps another kind of JWT from passing as an access token when its other claims happen to fit. - Find the key. The API looks up the token's
kidin the photo service's key set athttps://auth.photos.example/jwks, an address from its own configuration or the issuer's metadata, and never uses a key the token offers itself, as JWKs, key sets, and key IDs explained. - Verify the signature with that key and the expected algorithm.
- Check the issuer.
issmust equalhttps://auth.photos.exampleexactly, with no adjustments for case or a trailing slash. An API that trusts several issuers must also confirm that the key it used belongs to the issuer the token names. - Check the audience.
audmay be a single value or a list, and one of its values must be the photo API's own identifier,https://api.photos.example. A genuine token issued for another API is still rejected: its signature proves who issued it, not who it was for. - Check the time. The current time must be before
exp, and not beforenbf("not before") when the token has one. A small tolerance absorbs differences between the two servers' clocks, which the profile describes as usually no more than a few minutes.
The values are what make these checks the photo API's own. The algorithm, the issuer, the key set address and the audience all come from its configuration, never from the token, which is why each needs setting explicitly: these checks are settings for a well-maintained JWT library rather than code to write by hand, and a library left on its defaults may skip some.
If any check fails, the API answers 401 with invalid_token, and the request goes no further. A token that passes has proved who issued it, which API it was for, and that it is current. It has not yet been compared with what the printer is asking to do: first the scope, then the album.
Keeping up with keys
Fetching the key set for every request would make the authorization server part of every API call, so the photo API caches it and follows the rules from Caching the key set: fetch the set again when a token names an unfamiliar kid, and limit how often that can happen. If the identifier is still unknown after a fresh fetch, the token is rejected.
Those rules let the photo service rotate its access token signing key without coordinating with each API. Before it signs anything with its next key, it publishes that key beside the current one:
GET /jwks HTTP/1.1
Host: auth.photos.example
HTTP/1.1 200 OK
Content-Type: application/json
{
"keys": [
{ "kid": "photos-2026-09", "kty": "EC", "crv": "P-256", "alg": "ES256", "use": "sig", "x": "...", "y": "..." },
{ "kid": "photos-2027-01", "kty": "EC", "crv": "P-256", "alg": "ES256", "use": "sig", "x": "...", "y": "..." },
{ "kid": "photos-rs-2026-09", "kty": "RSA", "alg": "RS256", "use": "sig", "n": "...", "e": "AQAB" }
]
}The key values are shortened here. The third key, photos-rs-2026-09, signs ID tokens and is not part of this rotation. An API that refreshed its cache after the new key appeared already knows photos-2027-01. One that has not sees an unfamiliar identifier on the first new token, fetches the set again, and finds it. The old key stays published until the last token it signed has expired, ten minutes after the switch with these tokens.
The cache has a cost in the other direction. If a signing key leaks, the photo service removes it from the set immediately, but an API keeps trusting its cached copy until its next scheduled refresh. The unfamiliar-identifier rule never fires, because tokens signed with the leaked key name a key the API already knows. The cache lifetime therefore decides how long a withdrawn key keeps working at that API, and a refresh measured in minutes or an hour, rather than days, keeps the window short.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Validate a JWT access token in order and follow a key rotationRead a real at+jwt against the access token profile, let a local photo API validate it, see the audience check refuse it elsewhere, then rotate the signing key and watch the API keep up.