Beta

Create a tenant

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

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 session 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 lifetime

The 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 cookie, 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 token 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

Browser session A

The printer

Starts authorizationBackend server

Pending attempt A

state-A

Bound to session A

User is signed in (session A)

Session B

Browser session B

The printer

Starts authorizationBackend server

Pending attempt B

state-B

Bound to session B

User is signed in (session B)

Private PKCE verifiers stay on the backend.

Wrong session

Callback to session ABackend checks state

Lookup finds attempt B

Current A does not match stored B

Session A returns with state-B

Reject: session mismatch

Matching session

Callback to session ABackend checks state

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.

Each browser session keeps its own pending attempt. A callback carrying another session's state finds that attempt but fails the session check; a callback in its own session still has the issuer, expiry and single-use checks ahead of it.

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 issuer 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 endpoint. Nothing is wrong with the code; the printer has handed it to the wrong server. This is authorization server mix-up. PKCE 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.

The same forwarded response, with and without the issuer check. Only the comparison with the issuer saved for the attempt keeps Ava's code away from Pixel Vault.

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

A callback reaches the printer in Ava's browser session. Its state matches a pending attempt, but someone else's session started that attempt. What should the printer do? 1 of 3

Choose one answer.

Ava chose Pixel Vault. The callback's state matches her attempt, but its iss names https://auth.photos.example. What should the printer do? 2 of 3

Choose one answer.

The printer has confirmed that the photo service enforces PKCE, and it stops sending state. Which problem does PKCE still leave it unable to catch? 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