Key rotation
For: an operator who must change a key of an API with authtempo on a schedule, or after a leak.
What it does: this guide rotates each key that authtempo uses, and says what each rotation ends.
Before you start
Section titled “Before you start”- A deployed API. Read Deploy first.
- For the signing key,
jwtMinton. It is on by default. JWT minting lists its keys.
- Call
auth.api.jwt.rotate()from a scheduled handler, such as once a month.
import type { Auth } from "@authtempo/core";
/** * The scheduled rotation of the signing key. Run it from a cron trigger, for * example once a month. The old key verifies for 24 hours and signs nothing, * so a token that it signed stays valid until its own expiry. */export async function rotateSigningKey(auth: Auth): Promise<string | null> { // `api.jwt` is absent when the config sets `jwtMint: false`. if (!auth.api.jwt) return null; const { kid } = await auth.api.jwt.rotate(); return kid;}Core can also rotate on a timer: set jwtMint.rotateEvery to a number of days. An operator can rotate from a shell with authtempo keys rotate, through the operator route. authtempo keys rotate tells how.
- After a leak of a private key, call the rotation at once. Then wait 24 hours, and run the sweep.
- For Apple, make a new key in the Apple developer portal.
- Set the new key in
social.apple.privateKey, and its id insocial.apple.keyId. Deploy. - Revoke the old key in the Apple developer portal.
- For Google, GitHub, LinkedIn, and each list provider, make a second client secret at the provider.
- Set the new value in the provider’s
clientSecret, and deploy. - Delete the old client secret at the provider.
The test below rotates the key and checks the key set.
import { decodeProtectedHeader } from "jose";import { afterEach, describe, expect, it, vi } from "vitest";import { createJwtAuth } from "../guides/jwt.ts";import { call, harness, signIn } from "../support/harness.ts";import { rotateSigningKey } from "./key-rotation.ts";
// The retired key verifies for this long (RETIRED_KEY_MS of core).const DAY_MS = 86_400_000;
describe("key rotation", () => { afterEach(() => { vi.useRealTimers(); });
it("signs with the new key, and keeps the old key in the key set for its tokens", async () => { const h = harness(); const auth = createJwtAuth(h); const ada = await signIn(auth, h, "ada@example.com"); const mint = async () => ( (await (await call(auth, "GET", "/auth/token", undefined, { cookie: ada })).json()) as { token: string; } ).token; const keys = async () => ( (await (await call(auth, "GET", "/auth/jwks")).json()) as { keys: { kid: string }[] } ).keys.map((k) => k.kid);
const before = await mint(); const oldKid = decodeProtectedHeader(before).kid; const newKid = await rotateSigningKey(auth); expect(newKid).not.toBe(oldKid); expect(decodeProtectedHeader(await mint()).kid).toBe(newKid); expect(await keys()).toEqual([newKid, oldKid]); expect((await auth.api.jwt?.verify(before))?.email).toBe("ada@example.com"); });
it("drops the old key from the key set after 24 hours, and the sweep deletes it", async () => { // Only Date is fake: core's default clock reads it, and the crypto keeps its real timers. vi.useFakeTimers({ toFake: ["Date"] }); const h = harness(); const auth = createJwtAuth(h); const ada = await signIn(auth, h, "ada@example.com"); await call(auth, "GET", "/auth/token", undefined, { cookie: ada }); const keys = async () => ( (await (await call(auth, "GET", "/auth/jwks")).json()) as { keys: { kid: string }[] } ).keys.map((k) => k.kid); const newKid = await rotateSigningKey(auth); const rotatedAt = Date.now();
vi.setSystemTime(rotatedAt + DAY_MS - 60_000); expect(await keys()).toHaveLength(2); expect((await auth.api.sweepExpired()).jwks).toBe(0);
vi.setSystemTime(rotatedAt + DAY_MS + 60_000); expect(await keys()).toEqual([newKid]); expect((await auth.api.sweepExpired()).jwks).toBe(1); });});How it behaves
Section titled “How it behaves”- A rotation retires the signing key and writes a new one. The retired key verifies for 24 hours and signs nothing.
- With
jwtMint.rotateEvery, the first signature with a key of that many days makes the new key. A read of the key set never rotates. GET /auth/jwkslists the signing key first, then at most 100 keys retired less than 24 hours ago.- A server that verifies session JWTs reads the key set again for a new
kid. With a cache of the key set, the cache must be shorter than 24 hours. - The sweep deletes a key 24 hours after it retired.
- With
jwtMint.alg: "RS256", one key generation took 20 to 129 ms of wall time in local workerd. One signature took less than 1 ms. These are wall times on a loaded machine, measured on 2026-10-19. - On Workers, every RS256 key generation runs inside a Worker invocation.
authtempo keys rotatecalls the operator route, which is a Worker request. A scheduledapi.jwt.rotate(),rotateEvery, an alg change, and a new appsecretmake a key inside an invocation too. Workers Free allows 10 ms of CPU per invocation, so it can refuse each of these. - Core signs the Apple client secret again at each token exchange, with a life of five minutes. No stored Apple secret expires.
- The app
secretis not a key that rotates. Secrets tells what a change of it breaks.
If it fails
Section titled “If it fails”oauth_failedat an Apple sign-in: the key id and the private key do not match.oauth_failedat another provider: the client secret is wrong or deleted.- A JWT check fails on another server: the server keeps the old key set past 24 hours.