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 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. that started it. A code delivered to a different session is not accepted.
nonce returns inside the ID tokenIn OpenID Connect, a signed JWT from the provider, addressed to one client, saying which account authenticated, when, and in answer to which request. It reports a sign-in and is not used to call APIs.. The photo service copies the value from the request into the 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.'s nonce claim and then signs the token. Nobody can change the nonce in a genuine ID token without breaking 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., 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 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.. Suppose an attacker has stolen the authorization codeA short-lived, single-use value that the authorization server sends back to a client through the browser. The client swaps it for tokens in a direct request to the authorization server; APIs never accept it. 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 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. 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 partyAn application that accepts and relies on a sign-in from another service, the name used in OpenID Connect and for passkeys. SAML calls the same role a service provider. that cannot keep server-side records for each attempt can instead put a random value in a protected 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. and send a hashTurning data into a short, fixed-size value called a hash, in a way that cannot be reversed and where changing one character changes the result completely. Services store password hashes instead of passwords, and signatures are made over 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 OAuthA framework that lets a person give an application limited access to their resources at another service, using tokens instead of sharing their password. OAuth is about access; OpenID Connect adds sign-in. security guidance accepts a properly checked nonce in place of PKCE for confidential 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. 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.
| Value | Travels in | Checked by | What it establishes |
|---|---|---|---|
state | The request and the redirect | The collage app, at the callback | The response belongs to an attempt started in this browser session. |
iss in the response | The redirect | The collage app, at the callback | The expected provider answered. |
| PKCE challenge and verifier | The challenge in the request, the verifier in the token request | The photo service, at the token endpoint | Whoever redeems the code started the attempt. |
nonce | The request, then the signed ID token | The collage app, when validating the ID token | The 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.
Try it in the Lab
- Deliver a code to the wrong attempt and see PKCE, then the nonce, catch itHand a real code from Ava's attempt to a second attempt of your own, watch the token endpoint refuse it, then remove PKCE briefly and let the nonce check refuse the token.
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.