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.
Central server
One crypto server many apps connect to — same package on both sides, same idempotent config, `topology` picks the role.
With several apps, run crypto in one place. The control plane owns the keys, the audit and the
console. Each app is a tenant: it calls the same operations, and the package sends them over HTTP
with its token. There is no second package to install — @adonis-agora/crypto is the server and
the tenant side, exactly like @adonis-agora/flags and @adonis-agora/durable.
topology takes the same three values in every Agora lib:
topology: 'standalone' | 'control-plane' | 'tenant'The control plane
import { authkitTenantVerifier } from '@adonis-agora/authkit-client'
import { defineConfig } from '@adonis-agora/crypto'
export default defineConfig({
topology: 'control-plane',
// TOP LEVEL, the same place in every lib. Or sharedSecretVerifier({ partitions }).
verifyTenant: authkitTenantVerifier({
issuer: env.get('AUTHKIT_ISSUER'),
resource: 'urn:agora:crypto',
}),
})Tenants authenticate with tokens, not cookies: exempt the tenant API from @adonisjs/shield's CSRF
check, or it refuses their calls.
import { cryptoCsrfExceptions } from '@adonis-agora/crypto'
csrf: { exceptRoutes: (ctx) => cryptoCsrfExceptions(ctx.request.url()) },It serves, under /_crypto:
POST /v1/encrypt,/v1/decrypt,/v1/rewrap,/v1/datakeys,/v1/sign,/v1/verify,/v1/hmac(and the batch variants);GET/POST /v1/keys,POST /v1/keys/:name/rotate,PATCH/DELETE /v1/keys/:name,GET /v1/keys/:name/public|jwks|export;GET /v1/auditandGET /v1/whoami;- the console at
/_crypto(API/_crypto/api), gated byconsole.authorize.
A tenant
import { authkitTenantToken } from '@adonis-agora/authkit-client'
import { defineConfig } from '@adonis-agora/crypto'
export default defineConfig({
topology: 'tenant',
tenant: {
url: env.get('CRYPTO_URL'), // https://crypto.example.com/_crypto
partition: 'streaming', // this app's name on the server
token: authkitTenantToken({
issuer: env.get('AUTHKIT_ISSUER'),
clientId: env.get('CRYPTO_CLIENT_ID'),
clientSecret: env.get('CRYPTO_CLIENT_SECRET'),
resource: 'urn:agora:crypto',
}),
},
})import crypto from '@adonis-agora/crypto/services/main'
// Identical calls in every topology: in process on a server, over HTTP on a tenant.
const { ciphertext } = await crypto.encrypt({ key: 'media', plaintext: url })url can also be a function, resolved before the first call. With authkit, let the issuer tell
the tenant where the server is:
import { controlPlaneUrl } from '@adonis-agora/authkit-client'
tenant: {
url: controlPlaneUrl({ issuer: env.get('AUTHKIT_ISSUER'), resource: 'urn:agora:crypto' }),
partition: 'streaming',
token,
},After a connection error or a 404 from a route that is not there, the URL is looked up again
(with the function's refresh() when it has one) and the call is retried once on the new URL.
transports.http({ url }) takes the same function.
The tenant keeps no key material: the server holds the keys, enforces the token's capabilities
(crypto:encrypt:<key>, crypto:decrypt:<key>, …) and audits every call. On boot it asks whoami
and logs the answer; a wrong token or URL is a warning, not a boot failure.
Moving from standalone to central
Create the key names on the server, then change only config/crypto.ts:
export default defineConfig({ topology: 'standalone', app: 'streaming' }) // before
export default defineConfig({ topology: 'tenant', tenant: { … } }) // afterCalls do not change. Ciphertext made locally with standalone stays readable only by the local key,
so rewrap (or start fresh) when the data is expected to be served by the central server.
One end-to-end example
authkit-server crypto-server (_crypto) app A (tenant)
(issuer, JWKS) ─────► control-plane ─────► crypto.encrypt(...)
keys: media, webhooks no key material
console: /_crypto token: urn:agora:crypto
app B (tenant)
partition: billing
caps: crypto:encrypt:billing- authkit holds one service tenant per app, each allowed
urn:agora:cryptowith the scopes it needs. - The crypto server verifies tokens through discovery, keeps the keys, seals the material with its
own root (the app's
encryptionservice,crypto.keysor a cloud KMS), and audits every call. - App A and app B call the server. A
decryptwith app B's token can never touch app A's keys: the partition comes from the token.
Scaling
- Several replicas share the same database; rotation is a transaction, so two replicas rotating at once produce two ordered versions instead of losing one.
- Each replica needs the same root: the same
APP_KEY/config/encryption.ts, the samecrypto.keys, or the same cloud KMS. A missing root makes sealed material unreadable, which the doctor reports before it hurts.