Signing keys
Asymmetric keys per owner in Supabase Vault, with rotation that keeps old keys published until their tokens expire, a JWKS endpoint and a signer for JWTs and Ed25519 webhooks.
signing-keys keeps Ed25519 and ES256 keys per owner: an organization, an
app, a stream of security events. The private key lives in
Supabase Vault, the public
key in a signing_keys row, and signing runs in TypeScript with WebCrypto.
Receivers check signatures against a JWK Set you publish, so they never hold
a secret.
better-supabase add signing-keysimport {
createSigningKeys,
sqlTransport,
} from "better-supabase/blocks/signing-keys";Every function is service-role only. Clients can't read the table.
Create and rotate
create(owner, alg) generates a key (Ed25519 by default, or ES256),
stores the private key in Vault and makes it the owner's active key. The
kid is the RFC 7638 thumbprint of the public key. rotate(owner) does the
same with the active key's type, and the key it replaces is retired:
Server code calls the signing-keys instance on db, in the caller's organization. With gen.hooks set, better-supabase gen writes the hooks:
export const { useList: useSigningKeys } = defineDataHooks(
"signingKeys",
(db: AppDb) => db.signingKeys,
);const rows = await db.signingKeys.list({ limit: 50 }).orThrow();"use client";
import { useSigningKeys } from "@/lib/supabase/hooks";
const rows = useSigningKeys({ limit: 50 });| Method | What it does |
|---|---|
create(owner, alg?) | Generates a key and makes it active; the previous key is retired |
rotate(owner, alg?) | create with the active key's type |
retire(kid) | Retires a key without a replacement |
noteExp(kid, exp) | Keeps a key published until at least exp |
jwks(owner, opts?) | The owner's public keys still in use, as a JWK Set |
jwksResponse(owner) | jwks as an application/jwk-set+json response with Cache-Control |
signer(owner, opts?) | Signs with the owner's active key |
remove(owner) | Deletes every key of the owner and its Vault secrets |
purge(opts?) | Deletes retired keys that no token can still need |
Owners
An owner is any { type, id } pair. Use one owner per tenant that needs its
own JWKS, such as { type: "environment", id: environment.id }, so rotating
one tenant's key never touches another's. A platform with one authorization
server for every tenant uses the exported PLATFORM_OWNER
({ type: "platform", id: "default" }).
An owner has at most one active key. create_signing_key takes an advisory
lock per owner, so two rotations at once leave one active key.
Sign
signer(owner) imports the active private key once and keeps it for
cacheSeconds (300). sign(bytes) returns the raw signature with its kid
and alg; signJwt(claims, header?) returns a compact JWS with alg, kid
and typ: "JWT" in the header:
const signer = signingKeys.signer(owner, { create: "Ed25519" });
const token = await signer
.signJwt({
iss: "https://app.example.com",
sub: userId,
exp: nowSeconds + 600,
})
.orThrow();Without an active key, signing fails with not_found and the hint
SIGNING_KEY_MISSING, unless create names the type to generate. An
Ed25519 key signs with alg: "EdDSA" (RFC 8037), which most verifiers read;
set ed25519Alg: "Ed25519" for the fully specified name from RFC 9864.
Token types
Name each kind of token you sign with typ, so a token of one kind can't be
replayed as another (RFC 8725 section 3.11). jwtType("invite") returns
vnd.invite+jwt and throws for a name outside lower-case letters, digits and
-. signJwt accepts JWT or a type ending in +jwt and fails with the
hint SIGNING_TYP_INVALID for any other value. Verify with the same type:
import { jwtType, verifyCompact } from "better-supabase/blocks/signing-keys";
const typ = jwtType("invite");
const token = await signer.signJwt({ sub: userId }, { typ }).orThrow();
await verifyCompact(token, jwks, { typ }); // throws JWS_TYP for another kindA numeric exp claim is noted on the key, rounded up to the hour, so a
retired key stays in the JWKS until the last token it signed has expired.
Keys in Vercel KMS
Signers get their keys from the keys option, a KeyProvider. The default
is vaultKeyProvider, which keeps the private keys in Vault and signs with
WebCrypto as described above (ADR 0033). To keep the private key out of your
app entirely, sign with a Vercel KMS issuer
instead. Install the SDK, an optional peer (>=0.3.12 <0.4):
pnpm add @vercel/kmsimport * as kms from "@vercel/kms";
import {
createSigningKeys,
sqlTransport,
vercelKmsKeyProvider,
} from "better-supabase/blocks/signing-keys";
export const signingKeys = createSigningKeys({
transport: sqlTransport(postgres.admin),
keys: vercelKmsKeyProvider(kms, { issuer: "iss_abc123" }),
});signer(owner) then signs with the issuer's managed key: sign calls
signMessage and signJwt calls signJWT, and neither ever sees a private
key. Pass a function as issuer to pick one issuer per owner, and token,
region or baseUrl when the function's own OIDC token and region don't
fit. create is ignored, because the issuer holds its keys.
signJwt passes ttl: null, so the exp in your claims decides the
lifetime and KMS adds none; set exp yourself. The typ check
(SIGNING_TYP_INVALID) still runs before KMS is called. Failures come back
as results: a missing issuer key is not_found with the hint
SIGNING_KEY_MISSING, 401 and 403 are forbidden, 429 is rate_limited,
and anything else is network. A KMS key whose algorithm is not a JWS alg
this entry knows fails with SIGNING_ALG_UNSUPPORTED.
Verifiers fetch the issuer's JWK Set, which Vercel KMS publishes, not the
set from jwksResponse; the kid in each token names a key in that set.
create, rotate, retire, noteExp, jwks, jwksResponse, remove
and purge keep managing the Vault keys only, so with KMS you don't need
them and Vault holds no keys for the owner.
Your own provider
A KeyProvider has apiVersion: 1, a name, sign and signJwt, and
optionally noteExp when its published key set has to keep a key until a
token's exp. It sets alg and kid in the JWT header and takes the rest
of the header (typ and your members) from the signer. Run
testKeyProvider from better-supabase/testing against it: it checks the
API version, the kid and alg of a signature, the JWT header, and, given
the owner's JWK Set, that both signatures verify with verifyCompact.
Publish the JWKS
import { signingKeys } from "@/lib/signing-keys";
export const GET = () =>
signingKeys.jwksResponse({
owner: { type: "app", id: "default" },
maxAge: 300,
});The Next.js example publishes one set per organization at
/.well-known/jwks/<organization id> and excludes that path from the proxy
matcher, so receivers fetch it without a session.
The set has the active key and every retired key whose last exp plus the
grace period is still ahead. The grace defaults to the jwksGrace option
(5 minutes), which covers clock skew and cached JWKS responses; pass
grace to jwks or purge to change it for one call:
blocks: {
"signing-keys": { options: { jwksGrace: "15 minutes" } },
},Receivers verify with any JOSE library, or with verifyCompact from this
entry, which checks the alg against the key and refuses none:
import { verifyCompact } from "better-supabase/blocks/signing-keys";
const { header, payload } = await verifyCompact(token, jwks, { typ: "JWT" });Run purge() from a daily job to delete the retired keys that dropped out
of the set. Deleting a row deletes its Vault secret through a trigger.
Ed25519 webhooks
Outgoing webhooks can sign with an owner's key
instead of shared secrets (Standard Webhooks v1a). Pass the signer to
standardWebhooksAsymmetric and give receivers the whpk_ public key:
import {
standardWebhooksAsymmetric,
webhookPublicKey,
} from "better-supabase/blocks/webhooks";
const asymmetricSigner = standardWebhooksAsymmetric({
signer: signingKeys.signer({ type: "webhooks", id: "default" }),
});
const publicKey = webhookPublicKey(created.publicKey); // "whpk_…"Events
| Type | When |
|---|---|
signing_key.created | A key was created; data has kid and alg |
signing_key.retired | A key was retired, by rotation or retire |
Both carry ownerType, ownerId and kid, and write a security audit
entry when the audit block is installed.
Without the client
db.signingKeys is this client. Code that does not go through bsb.db() builds it with a transport and an organization id:
import {
createSigningKeys,
sqlTransport,
} from "better-supabase/blocks/signing-keys";
export const signingKeys = createSigningKeys({
transport: sqlTransport(postgres.admin),
});
const owner = { type: "organization", id: organizationId };
const created = await signingKeys.create(owner).orThrow();
// { kid: "kPrK_qmx…", alg: "Ed25519", publicKey: { kty: "OKP", … }, retired: null }Last updated on
Service accounts
Non-human organization members that can't sign in, hold a role through a membership, and run organization-owned workflows and service webhook endpoints.
Security events
An OpenID Shared Signals transmitter and receiver, with CAEP and RISC events as signed Security Event Tokens, pushed through outgoing webhooks or polled.