Skip to main content

Keep signing keys in your own KMS

Sign tokens with a key that stays in AWS KMS, Google Cloud KMS, Azure Key Vault or an HSM, and rotate it.

RecipeDraftView source
This page is a draft. The code exists, and not all of it has been run for this page yet. The page says what was checked.
Works with
JavaScript / TypeScriptPython
Standards
RFC 7517

What it shows​

  1. You create a signing key in your key service, here AWS KMS. The private key never leaves the service: AWS signs each token.
  2. The new key is published in the key set first, and it signs nothing yet.
  3. You make it the key that signs new tokens.
  4. A program reads the key set from the discovery document, and verifies a token with it.
  5. During a rotation both keys are in the key set, so tokens that the old key signed stay valid.

LumoAuth stores only the public key and the ID of the key in the service. The key set and the check of signatures therefore keep working when the key service is unreachable for a moment. Signing does not.

Before you start​

  • Check that your plan includes Bring-your-own signing keys in the plans matrix.
  • An AWS access key that may use these KMS actions: CreateKey, CreateAlias, GetPublicKey, Sign, Verify and ScheduleKeyDeletion.
  • An access token of your organization, to verify. The programs need no API key: the key set is public.

Create the key in the portal​

The signing keys have no Admin API: you create them in the portal.

  1. Open Security, Signing keys, and click Generate new key.
  2. Under Algorithm, choose RSA with RS256, the algorithm that every JWT library supports.
  3. Under Key storage, choose AWS KMS. Enter the Region, the Access key ID and the Secret access key.
  4. Under Activation and rotation, switch off Sign new tokens with this key. The key is then published first, and applications can read it before it signs.
  5. Click Generate key. The key list shows the new key with the provider AWS KMS.
  6. Run a program below: the key set now holds two keys.
  7. Open the new key and click Set as primary. New tokens carry its kid.

These steps were written from the docs. Nobody has followed them for this page yet. The programs ran against an organization whose keys LumoAuth stores, with one key and with a second published key. No key service was available, so the portal steps with AWS KMS were not followed.

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. Every version below was run against a LumoAuth organization. The programs ran against an organization whose keys LumoAuth stores, with one key and with a second published key. No key service was available, so the portal steps with AWS KMS were not followed.

b2b/keys-in-your-kms/js/keys.mjs
// Reads the public keys that sign the tokens of your organization, and
// verifies a token with them. The private keys stay in your key service.
//
// node b2b/keys-in-your-kms/js/keys.mjs
import { createRemoteJWKSet, decodeProtectedHeader, jwtVerify } from 'jose';

const issuer = `${process.env.LUMOAUTH_URL.replace(/\/+$/, '')}/orgs/${process.env.LUMOAUTH_ORG_ID}/api/v1`;

// The discovery document names the address of the key set.
const discovery = await (await fetch(`${issuer}/.well-known/openid-configuration`)).json();
console.log(`key set: ${discovery.jwks_uri}`);

// During a rotation the set holds the old key and the new key.
const { keys } = await (await fetch(discovery.jwks_uri)).json();
for (const key of keys) console.log(`key: ${key.kid} (${key.alg}, ${key.kty})`);

// jose picks the key by the `kid` of the token. When a token names a key that
// it has not seen, it reads the key set again, as after a rotation.
const token = process.env.ACCESS_TOKEN;
const { kid, alg } = decodeProtectedHeader(token);
// An API also checks `aud`. This program accepts a token for any audience.
const { payload } = await jwtVerify(token, createRemoteJWKSet(new URL(discovery.jwks_uri)), { issuer });
console.log(`token: signed with ${kid} (${alg}), valid, subject ${payload.sub}`);
node b2b/keys-in-your-kms/js/keys.mjs

Check the result​

Output
key set: https://app.lumoauth.dev/orgs/acme-corp/api/v1/.well-known/jwks.json
key: acme-corp-rs256-20261128010900-4d38b7b0 (RS256, RSA)
key: acme-corp-rs256-20261128020428-b32572e1 (RS256, RSA)
token: signed with acme-corp-rs256-20261128010900-4d38b7b0 (RS256), valid, subject 01a21fe5-5dd1-7837-b49e-f636d1b8073a
  • Run a program with a token from before step 7, and with a new one. Both are valid, and each names the key that signed it.
  • To rotate, click Rotate on the row of the primary key. The replacement uses the same key storage. The old key stays in the key set for the grace period, 24 hours by default.
  • The audit log records signing_key.rotated.

Limits​

  • No provider accepts a private key that you already have. The key is created in the service.
  • Disabling or deleting the key in AWS stops LumoAuth from signing with it at once. Keep a second key if you want a fallback.
  • Delete on the page of a key also deletes the key in the service, or schedules the deletion where the service has a waiting period.
  • Google Cloud KMS cannot sign with RS384, PS384 or ES512. Akeyless cannot sign with ES512 or the PS algorithms.
  • SAML responses are signed with the SAML certificate of the organization, not with these keys.