What state and nonce are
Both are random values your app puts in the authorization request. Both come back to the app, and the app checks that they are the ones it sent. They protect different steps of the sign-in.
| state | nonce | |
|---|---|---|
| Defined in | RFC 6749 (OAuth 2.0) | OpenID Connect Core 1.0 |
| Comes back in | The redirect URL | The ID token, as the nonce claim |
| Checked when | The callback arrives | The ID token is validated |
| Stops | Cross-site request forgery on the callback | A replayed or swapped ID token |
Why state exists
The callback URL of your app is public. Anyone can send a browser to it with a code in the query. Without a check, an attacker could start a sign-in with their own account, stop before the callback, and trick a victim’s browser into finishing it. The victim would then be signed in to the attacker’s account. This is cross-site request forgery against the redirect.
RFC 6749 section 10.12 describes the attack and recommends state as the defense. The app ties a random value to the user’s session before the redirect, and drops any callback whose state does not match.
RFC 9700 notes that PKCE also blocks this attack, because a code from another session cannot be redeemed without that session’s verifier. Most apps still send state, since it is simple and also tells the app which sign-in a callback belongs to.
Why nonce exists
An ID token is a signed statement that a user signed in. If an attacker gets hold of one, they might try to present it to your app again. OpenID Connect Core section 3.1.2.1 defines nonce to prevent that.
The server copies the nonce from the authorization request into the ID token. Because the token is signed, the nonce cannot be changed. Your app checks that the claim equals the value it stored for this sign-in. A token minted for any other request fails the check.
The nonce is required when an ID token comes straight from the authorization endpoint, as in the implicit and hybrid flows. In the code flow it is optional but recommended.
How they travel, with an example
GET /realms/acme/protocol/openid-connect/auth
?response_type=code&client_id=web-app
&redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback
&scope=openid&state=Zq3v8sample7KpW2&nonce=n0nceSample4xQhttps://app.example.com/callback?code=SAMPLE_CODE&state=Zq3v8sample7KpW2{ "iss": "https://auth.example.com/realms/acme", "aud": "web-app",
"nonce": "n0nceSample4xQ", "sub": "1f8f4b3a-sample", ... }What this tool does
- Makes a state and a nonce in your browser with
crypto.getRandomValues. - Lets you pick 16, 32 or 64 random bytes.
- Encodes them as Base64url or hex, both safe in a URL without escaping.
- Shows the bits of randomness and the length of each value.
- Prints the pair as query parameters, ready to paste into a test URL.
It does not store the values or check a callback. To compare a returned state with the one you expect, use the callback URL parser. To build the whole request, use the authorization URL builder, which makes its own state and nonce.
How to use the generator
- Open the page. A new state and nonce are ready.
- Choose the size and encoding if your app expects a specific format.
- Copy the query parameters into your test authorization URL.
- Store both values where your app can find them on the callback.
- Press Generate new before the next test. Never reuse a pair.
Where to store them
The values must survive the round trip through the authorization server. Where to keep them depends on the app.
Server-side web apps
Keep them in the user’s server session, or in a short-lived cookie that is HttpOnly, Secure and SameSite=Lax. Lax lets the cookie come back on the top-level redirect from the server. Delete the values once the callback is handled.
Single-page apps
Session storage works for the length of the redirect, because the page reloads in the same tab. Most browser libraries do this for you, together with the PKCE verifier.
Mobile and desktop apps
Keep them in memory for the duration of the sign-in. The app process normally stays alive while the system browser is open. If it may be killed, persist them in app-private storage.
How much randomness is enough
The values must be unguessable. 16 random bytes give 128 bits, which no one can guess. More bytes only make the URL longer. The tool offers 32 and 64 bytes for teams with a fixed policy.
Never build state or nonce from a timestamp, a counter, the user id or Math.random. Those are predictable. Use the platform’s cryptographic random source.
Base64url and hex carry the same randomness. Base64url is shorter: 16 bytes become 22 characters instead of 32. Pick whichever your logs and tests handle more easily.
Common mistakes
- Sending state but never checking it on the callback.
- Using one fixed state for every user, which protects nothing.
- Storing state in a cookie with SameSite=Strict, so it is missing when the server redirects back.
- Comparing the nonce from the authorization response instead of the one inside the validated ID token.
- Reusing values after a failed sign-in instead of starting fresh.
If a callback fails the state check, show a short error and start a new sign-in. Do not try to redeem the code.
Testing a sign-in by hand
When you debug a client, it helps to run the flow without the app. Paste the generated pair into a test URL, sign in, and read the redirect.
- Generate a pair and note both values.
- Build the authorization URL with them and open it in a private window.
- Sign in, then copy the full callback URL from the address bar.
- Paste it into the callback parser with the expected state to see if it matches.
- Exchange the code with the token request builder and decode the ID token.
- Check that the nonce claim equals the nonce you noted.
If the state comes back changed or missing, something between the app and the server is rewriting the URL. Proxies that drop or re-encode query parameters are a common cause.
Best practices
- Make a fresh state and nonce for every authorization request.
- Bind both to the browser session that started the sign-in, not to the user account.
- Check state before doing anything else with the callback.
- Check the nonce only after the ID token signature, issuer, audience and expiry pass.
- Delete both values once the sign-in completes or fails.
- Send PKCE as well. The three values protect different things.
How Keycloak handles state and nonce
Keycloak returns state unchanged in the redirect, along with code, session_state and, by default, iss. It copies nonce into the ID token. It does not check either value for you; that is the client’s job.
Keycloak’s own JavaScript adapter makes and checks both values. If you use another OpenID Connect library, check that it validates the nonce claim, since some only do it when asked.