Correlating requests and responses
A code arriving at the printer's callback says nothing about who started the connection or which server answered. Before it redeems anything, the printer matches the response to an attempt it started in this browser 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. and checks that the expected authorization server sent it.
Keeping a pending transaction
The printer's callback is an ordinary address on the web. Anyone can make a browser request it, with any parameters, without ever selecting Connect. Ava might also have two tabs open, each connecting a different photo account. So when a request arrives at https://printer.example/oauth/callback, the printer needs more context than "a code arrived". It has to know which attempt the response belongs to, which browser session started that attempt, and which authorization server it expected to answer.
It gets that context from a record it wrote before sending Ava's browser away. When she selects Connect, the printer's backend creates a pending transaction, one record per attempt, similar to this simplified example:
Pending attempt: demo-attempt-7
Initiating session: Ava's current printer session
Expected issuer: https://auth.photos.example
Redirect URI: https://printer.example/oauth/callback
PKCE verifier: held privately on the backend
Return destination: a checked local photo-book page
Status: pending, with a limited lifetimeThe record's key is the state value the printer put in the authorization request, which the photo service hands back unchanged in the response. A real state is fresh and unpredictable; demo-attempt-7 is readable only so you can follow it here.
When the callback arrives, the printer uses state to look up the pending attempt, then checks that the attempt belongs to the session that is delivering the response. Finding the value in some global list of attempts is not enough. The printer's own session 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 one Ava's browser sends with every request to printer.example, says whose browser this is, and the attempt must have been started by that same session.
That session binding stops cross-site request forgery, or CSRF: another site causing a browser to send a request its user did not intend. Here the forged request is a callback. Someone starts a connection to their own photo account, stops before their browser returns, and gets Ava's browser to deliver their callback, with their code, to the printer. If the printer accepted it, Ava's photo books would quietly fill with their photos, and photos she uploaded through the printer would land in their account. Because the forged state belongs to no pending attempt in Ava's session, the printer stops before any 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. request. Request correlation and CSRF follows that attack in more detail.
A matching record must also still be usable. If it has expired, or was already completed, the response is refused. Two copies of the same callback must never both complete one attempt, so the printer claims the record in a single step that cannot succeed twice, before it redeems the code. Keeping one record per attempt, rather than one per session, also matters: if the second tab's attempt overwrote the first tab's record, the first tab's response would no longer match its state, and its saved verifier would be gone.
If Ava's printer session has expired by the time the callback arrives, there is no session to match. The printer should not guess which local account should receive the connection. It can explain that the attempt expired and let her start again.
Session A
The printer
Pending attempt A
state-A
Bound to session A
User is signed in (session A)
Session B
The printer
Pending attempt B
state-B
Bound to session B
User is signed in (session B)
Private PKCE verifiers stay on the backend.
Wrong session
Lookup finds attempt B
Current A does not match stored B
Session A returns with state-B
Reject: session mismatch
Matching session
Lookup finds attempt A
Session A returns with state-A
Session matches
Also check issuer, expiry and single processing
State locates an attempt. Session binding still matters.
Checking who answered
An application that works with more than one authorization server must also know which one answered. Suppose the printer can also import photos from a second service, Pixel Vault, and uses the same callback address for both. When Ava chooses Pixel Vault, the printer saves https://auth.pixelvault.example as the expected 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. for that attempt and sends her browser to Pixel Vault.
Now suppose Pixel Vault is malicious, or has been compromised. Instead of handling the request itself, it forwards Ava's browser to the genuine photo service with an authorization request for the printer, carrying the printer's own state and callback address. She is already signed in there and has connected the printer before, so a code for her genuine photo account comes straight back to the printer's callback. The state matches her pending attempt, in her session. The printer still believes this attempt is talking to Pixel Vault, so it sends the code, and the verifier with it, to Pixel Vault's token endpointThe authorization server address where clients exchange an authorization code, refresh token, or other grant for tokens, in a direct request rather than through a browser redirect.. Nothing is wrong with the code; the printer has handed it to the wrong server. This is authorization server mix-up. 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. does not prevent it, because the printer sends the verifier to the same wrong place.
Our photo service includes the iss parameter in every authorization response, so the printer can catch this. The response names https://auth.photos.example, but the issuer saved for this attempt is Pixel Vault's. The printer compares the two strings exactly and stops before sending the code anywhere. It also stops if a server that always sends iss leaves it out, and it never uses a returned issuer value to choose a token endpoint: the endpoint comes from the configuration the printer already trusts for the expected issuer.
The printer compares iss
- Saved for this attempt
- auth.pixelvault.example
- Response iss
- auth.photos.example
- state
- Matches Ava's attempt
- Code and verifier sent to
- Nowhere
Stops before the token request
The printer trusts state alone
- Saved for this attempt
- auth.pixelvault.example
- Response iss
- Not compared
- state
- Matches Ava's attempt
- Code and verifier sent to
- Pixel Vault
Ava's code reaches Pixel Vault
PKCE does not prevent it, because the printer sends the verifier to the same wrong place.
Checking iss is one defense against mix-up. Other deployments use a different design, such as a distinct redirect URI for each authorization server, so the address the response arrives at already says who answered. Authorization server mix-up compares those choices.
Keeping the protections distinct
Three protections have now met at the callback, and each answers a different question. state ties the browser response to a pending attempt in this session. The PKCE verifier, kept in that attempt, ties redemption of the code to the attempt that asked for it. The issuer check confirms which authorization server answered.
They overlap in one place. PKCE can also stop a forged callback: the attacker's code is bound to the challenge from their own attempt, so when the printer redeems it with the verifier saved in Ava's session, the token endpoint refuses it. Current guidance therefore lets a client rely on PKCE in place of state for this purpose, but only once it has confirmed that the authorization server enforces PKCE, because the protection depends entirely on the server refusing a verifier that does not match. This lesson keeps an explicit state so that the transaction handling stays visible. The overlap does not make the three interchangeable. PKCE does nothing about a code sent to the wrong server, and state alone cannot tell which server answered.
Finally, keep state opaque. A random reference to the server-side record is easier to control than a value that carries secrets or a URLA web address, such as https://photos.example/api/albums/42?limit=10. It names how to connect (https), the domain, the path to a particular page or resource, and any extra parameters after the question mark.. The page Ava returns to after connecting is saved in the record and checked on its own, so the callback cannot become an open redirect: an address on a trusted site that sends a browser wherever a link tells it to. Attackers use open redirects to make a link to their own page look as if it belongs to the trusted site.
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
- Keep a pending transaction per session and check who answeredTreat each terminal as one printer session, accept a callback only in the session that started it and only once, and stop when the response names an issuer you did not expect.