It fails on one server or in one tab
Fresh tokens fail on 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. instance out of three, and Tari's sign-in fails in a tab she left open overnight. This lesson finds the local state that differs in each place, a verifier's clock, a pending sign-in attempt that was replaced, or a cookieA small value that a website asks the browser to store and send back with later matching requests. Cookies commonly carry a session identifier, so the site can recognize an ongoing sign-in without your password. the browser held back, and repairs that layer without loosening the checks that caught it.
The failure depends on where you try it
Two reports land in the same week. The first comes from the photo service's monitoring: about one in three calls from the printing company to api.photos.example is failing with 401, and the tokens being refused were issued seconds earlier. The second is a ticket from Tari: "Sign-in failed this morning in the tab I had open since last night. I opened a new tab and it worked straight away. Is something wrong with my account?"
The two have nothing to do with each other, except for a shape you learn to recognise. The same request works in one place and fails in another, and the place is the variable: one server out of three, one browser tab out of two. When that happens, 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. is usually fine. It issued one 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., or ran one sign-in, and something local to the failing place disagreed with it. Your job is to find which piece of local state differs, prove it, and fix that piece without touching the checks that caught it.
Both incidents follow the method from Finding the failing stage. What changes is where the evidence lives: in a server's clock for the first, in the browser's pending sign-in for the second.
One instance says a fresh token has expired
The photo API runs as three instances behind a load balancer. Its request log already tells you most of the story, because each line names the instance that handled it:
2026-10-13T14:20:06.114Z api-1 GET /albums/2210 200 client=printer-web request_id=a71c...
2026-10-13T14:20:06.402Z api-3 GET /albums/2210 401 stage=token_validation reason=expired client=printer-web request_id=c09e...
2026-10-13T14:20:06.885Z api-2 GET /albums/2210 200 client=printer-web request_id=4be2...
2026-10-13T14:20:07.311Z api-3 GET /albums/2210 401 stage=token_validation reason=expired client=printer-web request_id=d5f0...The same client, the same album, a second apart, and only api-3 refuses. Here is what api-3 sends back:
HTTP/1.1 401 Unauthorized
Date: Tue, 13 Oct 2026 16:20:07 GMT
WWW-Authenticate: Bearer error="invalid_token", error_description="The token has expired"
X-Request-ID: d5f0a2c4-61e9-4b7a-9d13-7c0e5f2a8b19The challenge follows RFC 6750: invalid_token covers a token that is expired, revoked, malformed or invalid for some other reason, and the description narrows it to expiry. Before you believe it, look at the token. You need the claims, not 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., so decode the payload of one refused token on the printing site's side, where the team can see what it sent:
{"alg": "ES256", "typ": "at+jwt", "kid": "photos-2026-09"}
{
"iss": "https://auth.photos.example",
"sub": "user-5521",
"aud": "https://api.photos.example",
"client_id": "printer-web",
"scope": "photos.read",
"iat": 1791901205,
"nbf": 1791901205,
"exp": 1791901805,
"jti": "<redacted>"
}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. times are epoch seconds: seconds since the start of 1 January 1970, UTC. iat (issued at) and nbf (not before) are both 1791901205, which is 14:20:05 UTC. exp (expiry) is 1791901805, 14:30:05 UTC, so this token is good for ten minutes. A verifier accepts it only while its own clock reads between nbf and exp.
Now look at the Date header in the 401. Servers stamp every response with their own idea of the current time, and api-3 thinks it is 16:20:07, two hours later than everyone else. By that clock, a token that expires at 14:30:05 expired nearly two hours ago. The token is fine. The issuerThe party that creates and signs a token, assertion, or certificate, named in a token's iss claim. A recipient must check that the issuer is exactly the one it expected and trusts.'s clock is fine. One verifier's clock is wrong, which is why the error appears on one instance only. A clock running behind fails the other way: a fresh token looks as if it comes from the future, so an instance that checks nbf or iat refuses it as not yet valid, and the same instance keeps accepting tokens after they should have expired.
The instance itself confirms it. On a Linux host, timedatectl shows whether the clock is being kept in step with a time server:
Local time: Tue 2026-10-13 16:20:41 UTC
Universal time: Tue 2026-10-13 16:20:41 UTC
RTC time: Tue 2026-10-13 16:20:40
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: no
NTP service: inactive
RTC in local TZ: noTime synchronization is off on api-3. The instance was rebuilt the previous week from an image that left the time service disabled, and its clock has been wandering since.
Most token libraries accept a small leeway, often 30 to 60 seconds, applied to exp and nbf. RFC 7519 allows for it because two well-run servers never agree to the millisecond. Leeway handles ordinary skew. It is the wrong tool for drift. Raising it to two hours would make api-3 accept the fresh token, and it would also make every instance accept tokens for two hours after they expire, while api-3's clock stays wrong for everything else it does. The fault is on the host, so the repair is on the host.
api-1, clock 14:20:06
Before exp: 200
api-3, clock 16:20:07
Past exp: 401 expired
Turn time sync back on
Fresh tokens 200, expired tokens 401
Leeway stays at 60 seconds
Sign-in fails in an old tab
Tari's ticket has the same shape in a browser. To read it, you need to know what the printing site keeps while a sign-in is in progress. When she chooses Sign in, the site creates a pending attempt: a random state value, a PKCEProof Key for Code Exchange, pronounced pixie: the client creates a secret for each authorization attempt, sends only a hash of it at the start, and proves it holds the secret when redeeming the code, so a stolen authorization code is useless to anyone else. code verifier and, because this is an 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. sign-in, a nonce. It sends her to auth.photos.example with the state, the PKCE challenge and the nonce, and keeps the attempt where her browser can bring it back: here, a server-side record referenced by a short-lived cookie. When the provider redirects her back, the site matches the returned state to that record before it redeems the code. A response that matches no attempt the site holds is refused, because the site cannot tell it apart from a response planted by someone else.
Her callback trace from that morning:
2026-10-14T21:47:12Z attempt.start attempt=att-31 expires=21:57:12
2026-10-15T08:02:40Z attempt.start attempt=att-58 expires=08:12:40
2026-10-15T08:03:05Z callback state=unknown txn_cookie=present(att-58) code=not_redeemed result=rejected
2026-10-15T08:04:19Z callback state=match(att-58) txn_cookie=present(att-58) code=redeemed result=signed_inThe trace records whether the cookie arrived and which attempt it pointed to, never its value. Read it in order. Last night at 21:47 Tari chose Sign in, which created att-31 with a ten-minute life, and she left the provider's page open. This morning she opened a new tab and chose Sign in there too, which created att-58. Then she went back to the old tab and finished signing in. The provider did its job and redirected her with a code and the state of att-31. By then the printing site held only att-58: att-31 had expired at 21:57 and, in this design, the new attempt had also replaced it in the single cookie the site uses. The site refused the callback and, correctly, never redeemed the code. In the new tab, the returned state matched att-58 and she was signed in a minute later.
A callback like this fails for one of three reasons: a second attempt replaced the first, the attempt expired, or the cookie that carries it was not sent with the callback. Tari hit the first two at once, and the trace rules out the third: txn_cookie=present means the browser sent the cookie, it just pointed at a different attempt. Her account, her password and the provider's sessionThe ongoing connection between later requests and an earlier sign-in, so you are not asked for your password on every click. It is often tracked with a cookie holding an unpredictable session identifier. were never involved.
Repair and verify the actual layer
For api-3, turn time synchronization back on and fix the image it was built from, so the next rebuild does not repeat the fault. Add an alert on clock offset, because the 401s were the first sign anyone saw. Then verify in both directions. Fresh tokens are accepted on all three instances, with the leeway still at its usual 60 seconds. A token whose exp has genuinely passed is still refused, by every instance.
For Tari, the site was right to refuse a callback for an attempt it no longer held. The repair is the experience around it: instead of a generic error, the callback page says the sign-in attempt has expired and offers a button that discards the stale attempt and starts a fresh one. Storing each pending attempt under its own state, rather than in one cookie slot, also lets two tabs sign in without one replacing the other. Verify that a fresh attempt still signs in, and that replaying the old callback is still refused with its code unredeemed. Do not loosen state, nonce or PKCE checks to make an old response succeed. Those checks are what keep someone else's callback out of Tari's session.
Worked example: two handoff notes
Each incident closes with a note that names the local state that differed and the evidence that proved it:
Symptom: api-3 refused fresh tokens with invalid_token "The token has expired"; api-1 and api-2 accepted them.
Evidence: Refused token iat and nbf 1791901205 (14:20:05Z), exp 1791901805 (14:30:05Z). api-3 Date header
16:20:07 GMT (request d5f0a2c4-61e9-4b7a-9d13-7c0e5f2a8b19). timedatectl on api-3:
synchronized no, NTP service inactive.
Cause: Time service disabled in the image used to rebuild api-3. Issuer clock and tokens correct.
Repair: Enabled time sync on api-3, fixed the image, added a clock-offset alert. Leeway unchanged (60 s).
Verification: Fresh tokens 200 on all three instances. Expired token refused on all three.
Symptom: Sign-in failed in a tab open since the previous evening; a new tab worked.
Evidence: Callback state matched no held attempt; transaction cookie present, pointing at att-58.
att-31 started 21:47, expired 21:57, replaced by att-58 at 08:02. Code not redeemed.
Cause: Stale pending attempt. Not a cookie attribute fault (the cookie was sent). Account not involved.
Repair: Expired-attempt page with "Start again"; pending attempts stored per state value.
Verification: Fresh sign-in succeeds. Replayed old callback still refused, code still unredeemed.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
- Explain why the same request fails on one instanceShift a verifier's clock to refuse a valid token, then separate that from real expiry and from a callback that reaches the wrong browser's pending sign-in.