Tutorial

RS256 Key Management

Sign JWTs with RS256 using a key pair, rotate keys with a kid header, and publish the public key in a JWKS endpoint for verifiers.

On this page

  1. Introduction
  2. Generate a key pair
  3. Sign with kid
  4. Publish JWKS
  5. Rotate gracefully
  6. Verify by kid
  7. Test the rotation
  8. Sources

Introduction

This guide covers RS256 key management with kid and JWKS, a recurring concern in the auth-jwt scenario. HS256 shares a secret between signer and verifier, which falls apart the moment a second service needs to verify tokens. RS256 gives the signer a private key and every verifier a public key, and rotating the key with a kid header lets you swap keys without a flag day, since verifiers fetch the right key by id.

Generate a key pair

Create an RSA key pair at 2048 bits or higher, keep the private key in a secret store, and treat the public key as non-sensitive material verifiers can fetch.

The trade-off worth naming is that create an rsa key pair at 2048 bits or higher, keep the private key in a secret store, and treat the public key as non-sensitive material verifiers can fetch adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

import { generateKeyPairSync } from 'node:crypto';
const { publicKey, privateKey } = generateKeyPairSync('rsa', { modulusLength: 2048 });

Sign with kid

Put the key id in the JWT header so a verifier knows which public key to use, which is what makes rotation possible without a flag day.

The trade-off worth naming is that put the key id in the jwt header so a verifier knows which public key to use, which is what makes rotation possible without a flag day adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

import { SignJWT } from 'jose';
const jwt = await new SignJWT({ sub: userId }).setProtectedHeader({ alg: 'RS256', kid: '2026-10' }).setExpirationTime('15m').sign(privateKey);

Publish JWKS

Serve the public keys in a JWKS document keyed by kid so verifiers fetch the right key at runtime instead of shipping a hardcoded copy that drifts.

The trade-off worth naming is that serve the public keys in a jwks document keyed by kid so verifiers fetch the right key at runtime instead of shipping a hardcoded copy that drifts adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

app.get('/.well-known/jwks.json', (req, res) => {
  res.json({ keys: [{ kty: 'RSA', kid: '2026-10', n: pubN, e: 'AQAB' }] });
});

Rotate gracefully

Add a new key, start signing with it, and keep the old key valid for one max token lifetime so outstanding tokens still verify, then retire the old key.

The trade-off worth naming is that add a new key, start signing with it, and keep the old key valid for one max token lifetime so outstanding tokens still verify, then retire the old key adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Verify by kid

On verify, read the kid from the header, fetch the matching public key from JWKS, and reject if the kid is unknown, which closes the door on retired keys.

The trade-off worth naming is that on verify, read the kid from the header, fetch the matching public key from jwks, and reject if the kid is unknown, which closes the door on retired keys adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Test the rotation

Sign a token with the old key, rotate, and assert the old token still verifies during the grace period and fails after, so rotation is seamless and bounded.

The trade-off worth naming is that sign a token with the old key, rotate, and assert the old token still verifies during the grace period and fails after, so rotation is seamless and bounded adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: RFC 7517 JWKS, jose library.

FAQ

Why JWKS instead of sharing the public key directly?
JWKS lets verifiers fetch the key by kid at runtime, so a key rotation propagates without redeploying every verifier, which is the point of kid.
How long a grace period on rotation?
At least the max token lifetime, so tokens signed by the old key can still be verified until they expire, then you can drop the old key.

Related Examples