Skip to Content
🎉 BSH Engine v0.0.7 is out with new features and improvements. See what's new →

Signing Keys

The engine signs every access and refresh token with a JWT signing key. Keys are generated by the engine, stored encrypted in the database and rotated automatically.

How keys work

  • On first startup, the engine generates a key and stores it.
  • Each key has its own kid and holds two key pairs: one signs access tokens, the other signs refresh tokens.
  • Each pair: the private key signs, the public key verifies. Tokens using any other algorithm, including none, are rejected.
  • Every token carries the kid of the key that signed it, in its header.
  • The engine checks the token with that key. A token with a missing or unknown kid is rejected.
  • The public keys that verify access tokens are published at /.well-known/jwks.json, so other services can verify access tokens without calling the engine.

Encryption at rest

The private keys are encrypted before they are stored, using a key derived from auth.masterKey. The master key is required.

Keep auth.masterKey stable and identical on every instance. If it changes, the engine will create new Signing Keys and all users will logout.

Lifecycle

StatusSigns new tokensVerifies tokensWhen
ActiveyesyesThe current key. There is exactly one.
RetirednoyesIt keeps verifying tokens it signed until retiredAt + refreshTtl + 1h, so users stay logged in through a rotation.
ExpirednonoNever used again. Tokens it signed are rejected and their users have to log in again.

Automatic rotation

The active key is replaced after auth.config.secretTtl days (30 by default). The new key signs all new tokens and the previous one becomes Retired.

Like the token TTLs, the rotation and grace dates follow the configuration currently set, so changing secretTtl or refreshTtl also applies to existing keys.

Keep secretTtl at least as long as refreshTtl. With a shorter secretTtl, more than two keys are alive at the same time.

Manual and Emergency rotation

A BSH_ADMIN can rotate the key at any time, this will be useful when tokens where leaked:

OptionPrevious keysEffect on usersUse it when
none: Keep previous keys validStay RetiredNone. Sessions keep working until their tokens expire.Routine rotation
previous: Expire the previous keyThe key that was active becomes ExpiredEveryone holding tokens signed by that key is logged out.The current key leaked
all: Expire all previous keysEvery retired key becomes ExpiredEveryone is logged out.You don’t know which key leaked

Verifying tokens in other services

The engine publishes the public keys of its access tokens as a standard JSON Web Key Set (JWKS), with public access:

GET /.well-known/jwks.json
{ "keys": [ { "kty": "EC", "crv": "P-256", "x": "h7I0BWOoM31TIzIHRKgU4kzKGMSWofcwz1KU3_Az2as", "y": "Owiaoq-WMdzojUajSxx4Z1ZgW6snsgbUZ_9uN5CRd3c", "kid": "a82791af-3c4d-4e5f-8a9b-0c1d2e3f4a5b", "alg": "ES256", "use": "sig", "key_ops": ["verify"] } ] }
  • Only public access token keys are published. Refresh tokens are only read by the engine.
  • Any standard JWT library can use it: give it the URL, it picks the key matching the token’s kid.
import { createRemoteJWKSet, jwtVerify } from 'jose'; const jwks = createRemoteJWKSet(new URL('https://<engine-host>/.well-known/jwks.json')); const { payload } = await jwtVerify(accessToken, jwks, { algorithms: ['ES256'] });

The response is cached for 5 minutes (Cache-Control: max-age=300). After a rotation, new tokens carry a kid that is not in a cached set yet: JWT libraries fetch the set again when they meet an unknown kid, as jose does.

A valid signature only proves the engine issued the token. It does not tell whether the session was revoked or the user logged out since. For those checks, call the engine (for example Get Current User).

See JWKS in the API reference and bshEngine.jwks() in the SDK.

Managing keys

BSH_ADMIN users manage keys under Security → Signing Keys. The list shows each key’s status, how it became active (Scheduled or Manual), when it was activated and retired, how long it stays valid and who rotated it. The key that signed your own session is tagged Current.

Signing Keys

Rotate now opens the rotation dialog with the three options above. The reason is optional for a routine rotation and required when expiring keys.

Rotate Signing Key

Retired keys have an Expire action that ends them immediately. A reason is required.

Expire Signing Key

Same operations from the API/SDK:

Last updated on