Crypto 1.x is a release candidate. npm's latest tag still installs 0.x, so install these docs' version from the rc tag:
npm i @adonis-agora/crypto@rc
node ace configure @adonis-agora/cryptoThe same goes for every @adonis-agora/* package these docs install: add @rc, and where a page says node ace add, install with @rc first and then run node ace configure. On 0.x? Read the 0.x docs.
KMS
Use the crypto server as the root of another Agora lib's envelope encryption, and as a remote signer.
The Agora crypto kit (copied into every lib) can run in envelope mode: each sealed value carries a
data key wrapped by a KmsAdapter. Point that adapter at this server and no lib holds key material
any more — the server is the root.
import { createCryptoClient, cryptoServerKms } from '@adonis-agora/crypto'
import { createKeyring } from './crypto-kit/index.js'
const client = createCryptoClient({
url: env.get('CRYPTO_URL'), // https://crypto.example.com/_crypto
partition: 'streaming',
token: authkitTenantToken({ issuer, clientId, clientSecret, resource: 'urn:agora:crypto' }),
})
const keyring = createKeyring({
purpose: 'config:secrets',
encryption: appEncryption, // the lib's local layer stays as it is
kms: cryptoServerKms({ client, key: 'fleet-root' }),
})The server needs one aes256-gcm key for the fleet (fleet-root above) and the app's token needs
crypto:encrypt:fleet-root and crypto:decrypt:fleet-root.
What the adapter does
wrap(dataKey, { purpose })encrypts the 32-byte data key with the named key, binding the keyring'spurposeas AAD.unwrap(wrapped, { purpose })decrypts it. A data key wrapped forconfig:secretscannot be unwrapped for another purpose, so a value cannot be moved between libs.
Rotating the fleet root is one command, and old wrapped keys keep working because each ciphertext names the version that sealed it:
node ace crypto:keys:rotate fleet-rootWho can use it
Any lib that copies the crypto kit accepts an adapter with exactly this shape:
@adonis-agora/config crypto: { kms: cryptoServerKms({ client, key: 'fleet-root' }) }
authkit-server keystore / account secrets
@adonis-agora/agent poppy connectionsThe kit's KmsAdapter is { wrap, unwrap } returning Uint8Array; cryptoServerKms implements it
structurally, so the client package never imports @adonisjs/core.
Remote signer
When a signature has to come from a key the app must not hold (a webhook signing key, a licence token), use the remote signer:
import { cryptoServerSigner } from '@adonis-agora/crypto'
const signer = cryptoServerSigner({ client, key: 'webhooks' })
const signature = await signer.sign(body) // agora:sig1:<kid>:<signature>
await signer.verify(body, signature)
const jwk = await signer.publicKey()The signature format is the crypto kit's, so a receiver verifies it with the kit's verify() and the
published JWK — no shared secret, and the private key never leaves the server.
Choosing a root
| Root | When |
|---|---|
the app's encryption service | default; one key for the app, rotation in config/encryption.ts |
crypto.keys | this package owns the root; rotate by prepending a key |
a cloud KMS via KmsAdapter | the root must live in AWS/GCP/Azure; key material is wrapped by the KMS |
| this server | several apps must share a root, and none of them should hold it |
The two compose: a crypto server can itself use a cloud KmsAdapter as its root, so the fleet's
material is protected by the cloud KMS while apps only ever talk to the crypto server.