JWT Signing Keys
Why one shared JWT secret stops being enough, how key pairs fix it, and how to check tokens signed by a login provider like Auth0, Clerk or Cognito.

The examples in JWT Explained sign tokens with HS256
and one shared secret. That's the simplest setup, and it's fine while one
server both makes and checks tokens. This post covers when that stops being
enough, why key pairs fix it, and how to let a login provider look after the
keys for you.
The problem with one shared secret
HS256 uses one secret to both sign and check tokens. Every service that
checks tokens needs the secret, and every service with the secret can also
make tokens.
Say your login service signs tokens, and your orders service checks them. To check them, the orders service needs the secret. Now a bug in the orders service that leaks the secret lets an attacker sign a token for any user. The more services share the secret, the more places it can leak from.
Key pairs: one key signs, the other only checks
With a key pair, a private key signs and a public key can only check. Only the login service has the private key. Every other service gets the public key, and leaking it gives an attacker nothing: it can check a signature, but it can't make one.
The common key-pair algorithms are RS256 (RSA, supported everywhere),
ES256 (elliptic curves, with much smaller keys and signatures) and EdDSA
(newer and faster still). You'll see the first two most often.
The login service publishes its public keys at a URL, as a JWKS (JSON Web
Key Set). Each key has an id, its kid, and every token's header names the
key that signed it:
{ "alg": "RS256", "typ": "JWT", "kid": "a1b2c3" }A service checking the token looks up that kid in the set and checks the
signature with the matching public key.
Let a login provider hold the keys
If you build the login yourself, the private key is yours to look after: you have to store it safely, change it now and then, publish the JWKS, and replace it fast if it ever leaks. A hosted login provider like Auth0, Clerk, Cognito, Firebase Auth or Supabase Auth does all of that for you:
- It makes the key pair and keeps the private key. You never see it, so you can't leak it.
- It signs every token at login.
- It publishes the public keys as a JWKS.
- It rotates the keys, publishing the new one before signing with it.
Your servers only check tokens. That's a much smaller job, and it's the same for every provider.
Checking a provider's tokens
You need three values from your provider's dashboard:
# .envJWKS_URL=https://login.example.com/.well-known/jwks.jsonJWT_ISSUER=https://login.example.com/JWT_AUDIENCE=https://api.example.comJWKS_URL is where the provider publishes its public keys. The other two
match claims inside every token it makes. Here's the payload of a typical
provider token:
{ "iss": "https://login.example.com/", "aud": "https://api.example.com", "sub": "user_64f1c2", "exp": 1763540100}iss(issuer) says who made the token: your account at the provider. Checking it means a token from someone else's account at the same provider is turned away. The check compares the two strings exactly, so a missing trailing slash is enough to reject every real token.aud(audience) says which API the token is for. When you add your API in the provider's dashboard, you give it an identifier, and the provider puts it inaud. Checking it means a token meant for one of your APIs doesn't work on another. It often looks like a URL, but it's only a name; nothing ever visits it.
The surest way to get both right is to log in once, decode the token you get
(with decodeJwt from jose), and copy iss and aud exactly. None of
these three values are secret. Then check every request with jose:
import { createRemoteJWKSet, jwtVerify } from "jose";
const keys = createRemoteJWKSet(new URL(process.env.JWKS_URL!));
async function requireAuth(req: Request, res: Response, next: NextFunction) { const header = req.headers.authorization; if (!header?.startsWith("Bearer ")) return res.sendStatus(401);
try { const { payload } = await jwtVerify(header.slice("Bearer ".length), keys, { algorithms: ["RS256"], // whatever your provider signs with issuer: process.env.JWT_ISSUER, audience: process.env.JWT_AUDIENCE, requiredClaims: ["sub", "exp"], });
req.user = { id: payload.sub! }; next(); } catch { res.sendStatus(401); }}This is the same requireAuth as in JWT Explained,
with one change: keys takes the place of the shared secret.
createRemoteJWKSet fetches the JWKS the first time it's needed and keeps it
in memory. When a token arrives with a kid it hasn't seen, it fetches the
set again. So when the provider rotates its keys, your servers pick up the new
key on their own, and nobody is logged out.
Where each provider publishes its keys
| Provider | JWKS URL | Signs with |
|---|---|---|
| Auth0 | https://<your-domain>/.well-known/jwks.json | RS256 |
| Clerk | https://<your-frontend-api>/.well-known/jwks.json | RS256 |
| Cognito | https://cognito-idp.<region>.amazonaws.com/<user-pool-id>/.well-known/jwks.json | RS256 |
| Firebase Auth | https://www.googleapis.com/service_accounts/v1/jwk/securetoken@system.gserviceaccount.com | RS256 |
The issuer and audience differ more between providers, and a few have quirks.
Cognito's access tokens, for example, have no aud at all; they put the app
id in a client_id claim, which you check yourself. Your provider's docs on
"verifying access tokens" list the exact values.
What's still your job
The provider looks after the keys, but checking tokens properly is still on you:
- Pin
algorithms. The token's header says which algorithm it used, and an attacker can write anything there. The server should decide, not the token. - Check
issuerandaudience. Without them, a token the same provider issued for a different app would be accepted by yours. - Only trust your provider's JWKS URL. Tokens can carry header fields that
point at keys, like
jku(a URL to fetch keys from) andx5c(a certificate). An attacker can fill these in to point at a key they made themselves. Always use the URL from your own config, never one from the token. - Keep the client secret out of git. If your provider gives your backend a client secret, it belongs in an environment variable, like any password.
- Set token lifetimes in the provider. How long access and refresh tokens last is a setting there. Refresh Tokens Explained covers what to choose.
The short version
HS256is fine while one server both makes and checks tokens.- Once a second service checks tokens, use a key pair: the private key signs, the public key only checks.
- Let a login provider make, hold and rotate the keys. It publishes the public keys as a JWKS.
- Check its tokens with
createRemoteJWKSetandjwtVerify, pinningalgorithms,issuerandaudience. - Never trust a key the token points to. Only use your provider's JWKS URL.