Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

A valid token is refused in one deployment

When two deployments of one API reach different answers for the same token, 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-2

The 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 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-9f17d0c3b648

green fetched its keys from the staging identity provider. A verifier finds the key by the token's kid in the issuer's published key set, the lookup JWKs, key sets, and key IDs explained. It has to find the key before it can check the signature, 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 JWT access token, 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:

Configured issuer and audience compared with the token's claims
SettingToken claimsbluegreen
Issuerhttps://auth.photos.examplehttps://auth.photos.examplehttps://auth.staging.photos.example
Key setkid photos-2026-09From the production issuer's discovery documentFrom the staging issuer's discovery document
Audiencehttps://api.photos.examplehttps://api.photos.examplehttps://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-1

The 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.

The same production token at green after each repair. Each fix lets validation run one check further, until nothing is left to refuse.

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 server 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.

After a release, one API deployment answers 401 "signed by an unknown key" for production tokens that another deployment accepts. Its log shows keys fetched from the staging issuer. What should you fix first? 1 of 3

Choose one answer.

Two APIs trust https://auth.photos.example. A genuine token with aud https://api.photos.example is sent to the storage API at https://storage.photos.example. What should the storage API answer? 2 of 3

Choose one answer.

To test the repaired deployment, a teammate edits a production token's iss to the staging issuer and sends it. The API answers 401. What does that show? 3 of 3

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity