Beta

Create a tenant

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

State and nonce

The collage app's sign-in request carries two values it generated for this attempt, state and nonce. Both are random, both are stored in the pending sign-in, and both come back. This lesson shows why one would not be enough: each protects a different message.

Two values, two messages

In the sign-in Ava started, the values were state=demo-signin-3 and nonce=demo-nonce-3, readable stand-ins for random values; all values in these examples are fictional.

They come back in different messages, and each protects the message it returns in.

state returns in the redirect to the callback, next to the code. As Correlating requests and responses explained, the collage app uses it to find the pending attempt and to confirm that the response arrived in the same browser session that started it. A code delivered to a different session is not accepted.

nonce returns inside the ID token. The photo service copies the value from the request into the token's nonce claim and then signs the token. Nobody can change the nonce in a genuine ID token without breaking the signature, and the photo service puts demo-nonce-3 only in a token issued in answer to this request. When the collage app finds demo-nonce-3 in a token it has validated, it knows the token belongs to this attempt.

That matters even though the token arrives directly from the token endpoint. Suppose an attacker has stolen the authorization code issued for one of Ava's sign-ins. They start a sign-in of their own at the collage app, so their browser has a pending attempt with its own state and nonce, and then deliver Ava's code to the collage app's callback in their browser, with their state. The state check passes, because the attempt is theirs. If nothing else stopped it, the collage app would exchange Ava's code, receive a perfectly genuine ID token for user-2048, and sign the attacker in to Ava's collage account.

The nonce gives it away. The token carries the nonce from Ava's request, but the attacker's pending attempt expects a different one. The collage app rejects the token and signs no one in.

Creating a nonce

A nonce must be unpredictable and used for one attempt only. The collage app generates it with the same cryptographically secure randomness it uses for state and the PKCE verifier. The readable demo-nonce-3 is a stand-in.

The collage app keeps the expected nonce in the pending sign-in on its server, next to the verifier. A relying party that cannot keep server-side records for each attempt can instead put a random value in a protected cookie and send a hash of it as the nonce, then recompute the hash when the browser returns. Either way, the expected nonce is tied to the browser that started the attempt.

The collage app compares the token's nonce claim with the stored value exactly, and only once. When the pending sign-in is claimed, its nonce goes with it, so a second token carrying the same nonce finds nothing to match. If the request sent a nonce and the ID token has none, that is a failure too, not a token that happened to skip an optional check.

A nonce does not stay secret the way the PKCE verifier does. It travels through the browser in the request and appears in the ID token. Its value comes from being unpredictable beforehand and bound to one attempt.

Alongside PKCE

Return to the stolen code. With PKCE, the exchange would never reach the nonce check. The collage app would send the verifier stored in the attacker's pending attempt, which does not match the challenge in Ava's request, and the token endpoint would refuse to issue anything.

So the two overlap here. Current OAuth security guidance accepts a properly checked nonce in place of PKCE for confidential OpenID Connect clients such as the collage app, as protection against code injection. The collage app uses both anyway. They are checked in different places, by different parties, so a mistake in one does not leave the sign-in unprotected.

What each value protects
ValueTravels inChecked byWhat it establishes
stateThe request and the redirectThe collage app, at the callbackThe response belongs to an attempt started in this browser session.
iss in the responseThe redirectThe collage app, at the callbackThe expected provider answered.
PKCE challenge and verifierThe challenge in the request, the verifier in the token requestThe photo service, at the token endpointWhoever redeems the code started the attempt.
nonceThe request, then the signed ID tokenThe collage app, when validating the ID tokenThe ID token was issued for this attempt.

The nonce carries more weight in older flows that return an ID token directly through the browser, where nothing like the code exchange stands between a captured token and the relying party. OpenID Connect requires a nonce in those flows, and Response types, response modes, and older flows covers them. Expiration and nonce checks shows where the nonce comparison sits among the other checks the collage app makes.

Check your understanding

Try these questions before moving on. If an answer isn't right, use the feedback and try again.

An ID token's signature, issuer, and audience are valid, but its nonce does not match the pending sign-in. What should the collage app do? 1 of 2

Choose one answer.

A relying party cannot keep a server-side record for each sign-in attempt. How can it still check the nonce? 2 of 2

Choose one answer.

Try it in the Lab

Try a Capture the Flag challenge

Apply this lesson against an isolated, intentionally vulnerable application. Capture a flag, explain the mistake, then repair and retest it.

  1. A Sign-in from Another Session Difficulty 4 of 5 · About 40 minutes · Browser application

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