A valid token is refused in one deployment
When two deployments of one 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. reach different answers for the same tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret., they are applying different rules. This lesson follows the photo API after a new deployment went live with another environment's settings, compares its issuer and audience with the token's claims, and repairs them one check at a time while tokens from elsewhere stay refused.
The same token, two answers
All names, domains and identifiers in these examples are fictional. On Thursday morning, an engineer at the printing company opens a ticket with the photo service's API team: "Since about 10:00 UTC, roughly half our album requests get 401. If we retry the same request with the same token, it usually works. Request IDs attached." You are on the API team, and the timing lines up with a release. At 09:55, the photo API gained a second deployment, green, alongside the existing blue. Both run the same code and serve api.photos.example, and a load balancer in front of them sends each request to one or the other.
The request IDs tell the story quickly. Every refused request was served by green, and every success by blue. Here is one of the refusals:
GET /albums HTTP/1.1
Host: api.photos.example
Authorization: Bearer <redacted>
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="photos", error="invalid_token", error_description="The token was signed by an unknown key"
X-Request-ID: 2c84e1f7-6a90-4b3d-a5e2-9f17d0c3b648
X-Served-By: api-green-2The same token, sent again a second later, reached blue and came back 200. So the token is not expired, not malformed and not revoked, and the user and client behind it are fine. Two deployments of the same code looked at the same token and reached different decisions, which means they are not applying the same rules. Your job is to find which rule differs.
An unknown key that is not a rollover
"Unknown key" sounds like a signing key rollover, where the identity providerA service that authenticates users and tells other applications about the sign-in, so those applications never handle the users' passwords. In OpenID Connect it is called an OpenID Provider. starts signing with a new key and a verifier has not picked it up yet, as Signing keys and rotation described. That would be a reasonable first guess if every deployment were failing, or if the token's key were new. Neither is true here: blue accepts the token, and its key has been in use for weeks. green's request log says where it actually looked:
2026-10-15T10:04:12.551Z token.verify result=rejected stage=key_lookup kid=photos-2026-09
jwks_source=https://auth.staging.photos.example/.well-known/jwks.json
request_id=2c84e1f7-6a90-4b3d-a5e2-9f17d0c3b648green fetched its keys from the staging identity provider. A verifier finds the key by the token's kid in the issuer's published 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., the lookup JWKs, key sets, and key IDs explained. It has to find the key before it can check the signatureA value made with a private key that lets anyone holding the matching public key check that a message came from the key's holder and has not been changed. It proves origin and integrity; it does not hide the message., and it has to check the signature before any claim inside the token is worth comparing. So green failed at key lookup and never reached the point where it would compare the token's issuer with its own configuration. Refetching keys would not help, because the staging key set will never contain a production key. Nor would copying the production key into green's key list, which would leave a deployment that trusts staging's issuer while accepting production's signatures.
Had green held the right keys but the wrong issuer setting, the same token would have passed the signature check and failed one step later, still as a 401:
WWW-Authenticate: Bearer realm="photos", error="invalid_token", error_description="The token issuer is not trusted by this API"Which of those two messages you see depends on how the verifier finds its keys, not on what is wrong. Both point at the same question: which issuer does this deployment trust?
Compare the configuration with the claims
Decode one of the refused tokens on your own machine. Decoding reads the header and claims without verifying anything, which is all you need for a comparison. The at+jwt type marks it as a 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. 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., the profile Token formats and validation described:
Header:
{ "alg": "ES256", "kid": "photos-2026-09", "typ": "at+jwt" }
Claims:
{
"iss": "https://auth.photos.example",
"aud": "https://api.photos.example",
"sub": "user-5521",
"client_id": "printer-web",
"scope": "photos.read",
"iat": 1792058400,
"exp": 1792059000
}Two claims decide whether an API should accept a token at all, and they answer different questions. The issuer, iss, says which authority made the statement. The audience, aud, says which recipient the statement was made for. An API checks each against a value it already knows, and the token must pass both. Here are the two deployments' settings next to the token:
| Setting | Token claims | blue | green |
|---|---|---|---|
| Issuer | https://auth.photos.example | https://auth.photos.example | https://auth.staging.photos.example |
| Key set | kid photos-2026-09 | From the production issuer's discovery document | From the staging issuer's discovery document |
| Audience | https://api.photos.example | https://api.photos.example | https://api.staging.photos.example |
green's settings were generated from the staging template, and both its issuer and its audience came along. The API's expected values must come from its own configuration like this, never from the token being checked. A verifier that read iss from each incoming token and fetched that issuer's keys would accept tokens from anyone willing to run an identity provider. That is also why the decoded claims above are only evidence for a comparison. They show what the token asserts. Only the API's validation, signature first, says whether to believe it.
Staging and production are separate identity providers. They have their own users, clients and signing keys, so a perfectly genuine staging token says nothing a production API should believe, even if its sub or email happens to match a production user.
A second 401, and why neither is a 403
The team corrects green's issuer and redeploys. The key lookup now finds photos-2026-09, the signature verifies and the issuer matches, and the request is refused again, one check further along:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="photos", error="invalid_token", error_description="The token is not intended for this API"
X-Request-ID: 91d6a3b0-4e2f-4c78-8b19-d5e0f2a7c364
X-Served-By: api-green-1The audience was always wrong. It was hidden behind the issuer failure, because a refused key lookup stops validation before the audience is ever compared. This is common after a configuration copy: each repair reveals the next mismatch, and a new error after a fix is progress. The team sets green's audience to https://api.photos.example, and the refusals stop.
Staging template
- Key set
- Staging
- Key
- Not found
- Signature
- Not checked
- Issuer
- Not compared
- Audience
- Not compared
401 unknown key
Production issuer
- Key set
- Production
- Key
- photos-2026-09
- Signature
- Valid
- Issuer
- Matches
- Audience
- api.staging.photos.example
401 not for this API
Production audience
- Key set
- Production
- Key
- photos-2026-09
- Signature
- Valid
- Issuer
- Matches
- Audience
- Matches
200
The audience was always wrong. It was hidden behind the issuer failure.
Both answers were 401 with error="invalid_token", and that is correct. RFC 6750, section 3.1, uses invalid_token for a token that is expired, revoked, malformed or otherwise invalid, with status 401, the challenges Using access tokens described. A token from an untrusted issuer is not valid at this API, and RFC 9068, section 4, says a resource serverIn OAuth, the API that holds protected resources and decides, for each request, whether the presented access token allows it. must reject a JWT access token whose aud does not name it. A 403 means something different:
HTTP/1.1 403 Forbidden
WWW-Authenticate: Bearer realm="photos", error="insufficient_scope", scope="photos.write"insufficient_scope tells the client that the token is valid here but does not carry the scope this operation needs. Sending it for a wrong issuer or audience would tell the printing site to ask for more scope, which would never fix anything. If your API answers issuer or audience failures with 403, that is a bug worth fixing separately, because clients and their engineers will chase the wrong cause.
Verify both environments stay separate
Repeat the original request against each deployment directly, using the X-Served-By header to confirm which one answered. A fresh production token for the photo API should get 200 from blue and green alike.
Then check two requests that must still be refused, using genuine tokens rather than edited ones. First, a token issued by the staging identity provider to a staging test client: green must answer 401. Second, a token from the production issuer for a different API, such as one whose aud is https://storage.photos.example: right issuer, wrong recipient, so green must answer 401 again. Do not hand-edit a token's iss or aud to produce these. An edited token fails at the signature, before either claim is compared, so it proves nothing about the checks you repaired.
The repair changed only green's configuration. The token, the identity provider and blue were never wrong. To keep this from happening again, the team adds a release step that compares each deployment's issuer and audience with the values expected for its environment before it takes traffic.
Worked example: the handoff note
The note records the two repairs in the order they were needed, so nobody reading it later wonders why the audience changed in a second release:
Symptom: About half of printer-web's album requests got 401 from 09:55 UTC (production).
First failure: Only deployment green refused. 401 invalid_token, "signed by an unknown key"
(request 2c84e1f7-6a90-4b3d-a5e2-9f17d0c3b648). Log: stage=key_lookup,
keys fetched from the staging issuer.
Compared: Token iss https://auth.photos.example, aud https://api.photos.example.
green issuer https://auth.staging.photos.example,
green audience https://api.staging.photos.example. blue matched the token.
Cause: green's settings were generated from the staging template (release 2026.10.15-1).
Not a key rollover: the token's key is published by the production issuer and
blue accepted it.
Repair: Set green's issuer, then its audience, to the production values. The audience
mismatch appeared only after the issuer repair (request 91d6a3b0-4e2f-4c78-8b19-d5e0f2a7c364).
Verification: Production photo API token: 200 from blue and green.
Genuine staging token at green: 401. Production token for storage.photos.example
at green: 401.
Not changed: Identity provider keys and settings, blue's configuration, printer-web's client.
Follow-up: Release check comparing issuer and audience per environment.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
- Restore the deployment's accepted requestsRun a local API with a drifted issuer setting, compare its configuration with real tokens' claims, repair it, and keep other-tenant and other-resource tokens refused.