Skip to content

OAuth state and OIDC nonce generator

Generate random state and nonce values for OAuth and OpenID Connect authorization requests, made in your browser with Web Crypto.

Runs in your browser. Nothing you paste is sent to us.

Options

16 bytes is plenty for both. Longer values only make the URL longer.

Encoding
How to use them
Store both in the user’s session (or a short-lived, HttpOnly cookie) before you redirect. When the user comes back, the state in the URL must equal the stored one. After the token response, the nonce claim in the ID token must equal the stored nonce. Use each value once.

Values

state
 
nonce
 
randomness
256 bits each
length
0 characters each
source
crypto.getRandomValues in your browser
Query parameters
 

How to use it

  1. Two new values are made when the page opens. Press Generate new for more.
  2. Pick how many random bytes and the encoding, if you need something specific.
  3. Store both in the user’s session, then add them to the authorization URL.

What to know

state protects the callback against cross-site request forgery: your app checks that the state in the response equals the one it stored.

nonce is copied by the server into the ID token. Your app checks the claim equals the stored value, which stops a replayed ID token.

Both must be unguessable and used once. 16 random bytes (128 bits) is plenty.

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.

statenonce
Defined inRFC 6749 (OAuth 2.0)OpenID Connect Core 1.0
Comes back inThe redirect URLThe ID token, as the nonce claim
Checked whenThe callback arrivesThe ID token is validated
StopsCross-site request forgery on the callbackA 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

Sample authorization request (values are made up)
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=n0nceSample4xQ
Sample callback
https://app.example.com/callback?code=SAMPLE_CODE&state=Zq3v8sample7KpW2
Sample ID token claims after the token request
{ "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

  1. Open the page. A new state and nonce are ready.
  2. Choose the size and encoding if your app expects a specific format.
  3. Copy the query parameters into your test authorization URL.
  4. Store both values where your app can find them on the callback.
  5. 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.

  1. Generate a pair and note both values.
  2. Build the authorization URL with them and open it in a private window.
  3. Sign in, then copy the full callback URL from the address bar.
  4. Paste it into the callback parser with the expected state to see if it matches.
  5. Exchange the code with the token request builder and decode the ID token.
  6. 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.

Questions

Frequently asked questions

What is the difference between state and nonce?

state comes back in the redirect URL and protects the callback against cross-site request forgery. nonce comes back inside the ID token and protects against a replayed token. They are checked at different steps, so OpenID Connect apps usually send both.

Is the state parameter required?

RFC 6749 lists it as recommended, not required. RFC 9700 accepts PKCE as an alternative defense against forgery on the callback. Sending state is still the common practice and costs nothing.

Is nonce required in the authorization code flow?

No. OpenID Connect requires it only when the ID token is returned from the authorization endpoint, as in the implicit and hybrid flows. In the code flow it is optional but recommended.

How long should a state value be?

Long enough that no one can guess it. 16 random bytes, 22 characters in Base64url, give 128 bits and are plenty. The spec sets no fixed length.

Can I put a return URL in state?

You can, as long as part of it is random and you check it against your session. A safer pattern is to store the return URL server-side under the random state. Never redirect to a URL taken from state without checking it against a list of your own pages.

What happens if state does not match?

Treat the callback as untrusted. Do not exchange the code, show an error and let the user start again. A mismatch can be an attack, an expired session or a second tab.

Do I need state if I use PKCE?

PKCE already stops a code from another session being redeemed, so it covers the main forgery risk. state remains useful to match a callback to the sign-in that started it and to carry your own context. Sending both is the simplest choice.

Does Keycloak check the nonce?

No. Keycloak puts the nonce into the ID token, and your client must compare it. Most OpenID Connect libraries do this when you pass them the stored nonce.

Are values made on this page safe to use?

They are made in your browser with a cryptographic random source and are never sent anywhere. In production your app should make fresh values for each sign-in itself. Use these for testing and manual requests.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.