# Rotate signing keys

> Move your realm to a new signing key whenever you choose, without any app or API refusing a token.

Your security policy says signing keys are replaced every year, or you've stopped trusting one. Keys never rotate on
their own, so each rotation is a call you make. A rotation moves the
realm to a new [signing key](/platform/keys/signing-keys/) by publishing it first and signing with it later, so every
app and API already holds the new key by the time it meets a token signed with it.

**Before you start:** you need the `MODIFY` right on the realm's `keySettings`, which a group holding `RealmOwner`
has, and your session in `$CVYUH_SESSION`: [Call the API](/develop/api/) shows how to get it. It's done through the
API. Check that the apps and APIs that verify your tokens pick keys by `kid` and refetch on an unknown one: see
[What your apps should do](/platform/keys/signing-keys/#what-your-apps-should-do).

## Rotate

<Steps>

1. Check the publish lead. Read `GET https://foobar.cvyuh.com/api/v0/am/objects/v1/keySettings/keySettings`: `rotation.publishLeadSecs` (3,600
   by default) is the soonest the new key may start signing. To give apps longer, raise it:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/keySettings/keySettings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "rotation": { "publishLeadSecs": 7200 } }'
   ```

2. Rotate one algorithm, naming when the new key starts signing:

   ```sh
   curl -X PUT https://foobar.cvyuh.com/api/v0/am/keys/v1/signing \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "algorithm": "ES256", "validFrom": "<RFC 3339 time, at least the publish lead from now>" }'
   ```

   The realm answers `204`, and the new key is in the published key set at once, beside the current one.

3. Repeat for `RS256` and `PS256` if you're rotating every key. Each algorithm rotates on its own.

4. Wait. Apps, gateways and APIs pick the new key up as their caches of the key set refresh, within five minutes by
   default. At `validFrom` the new key starts signing.

</Steps>

## Check it worked

List the keys with `GET https://foobar.cvyuh.com/api/v0/am/keys/v1`. The new `oauth2/signing` version shows its `validFrom` and `state:
"valid"`. Once its `validFrom` has passed, the version it replaced shows `state: "retained"`: still published, so
the tokens it signed verify until they expire.

You can also open `https://foobar.cvyuh.com/oauth2/jwks` in a browser: the new key's `kid` is there from step 2.

<Aside type="caution" title="Apps that pin one key">
  Apps that follow the key set's cache headers, and fetch it again when they meet an unknown `kid`, need nothing
  from you. An app that copied one key into its configuration does: give it the new key before `validFrom`.
</Aside>

## If it fails

- **`400`, "a rotated key signs too soon".** `validFrom` is sooner than the publish lead. The detail names the earliest
  time allowed; send that or later.
- **`400` once the realm holds 100 versions.** Revoke an old version first: see
  [Revoke a signing key](/platform/guides/revoke-a-signing-key/).
- **An API refuses tokens after `validFrom`.** It didn't refetch the key set. Restart it or clear its key cache, and
  give it the rules in [What your apps should do](/platform/keys/signing-keys/#what-your-apps-should-do).

Each rotation is recorded on the realm's audit trail under your name, never the key: see
[Signing keys](/platform/keys/signing-keys/#audit-record).

## Undo it

Until a minute before its `validFrom`, you can revoke the new version (`DELETE https://foobar.cvyuh.com/api/v0/am/keys/v1/signing/<version>`)
and the current key keeps signing. After that, rotate again to a fresh key.

## Related

- [Revoke a signing key](/platform/guides/revoke-a-signing-key/): retire the old key, or one you believe is exposed.
- [Signing keys](/platform/keys/signing-keys/): the settings and limits a rotation follows.
- [Check what clients discover](/platform/guides/what-clients-discover/): how apps find the new key.
