UNPKG

@adonis-agora/media

Version:

Media-library for AdonisJS — owner collections (MIME whitelist, single-file replace, ordering), image conversions (eager/lazy), pluggable content transformers (HLS video via mediabunny, metadata probe, image blur-up placeholder), and column attachments, a

312 lines • 15.1 kB
import type { TenantTokenSource, TenantVerifierInput } from '../topology/auth.js'; import { type ChannelSecret, type ConsoleChannelAuthOptions, type ConsoleCipher, type ConsoleCipherSource, type ConsoleCommandKeys, type ConsoleModeChooser, RedisConsoleChannel } from './redis_channel.js'; /** Where the console's authentication is documented. */ export declare const CONSOLE_AUTH_DOCS = "https://davidecarvalho.github.io/agora/docs/media/dashboard/federated-console#authenticating-the-console"; /** The container binding `@adonis-agora/crypto` publishes its runtime under. */ export declare const CRYPTO_BINDING = "agora.crypto"; /** The container binding `@adonis-agora/authkit-client` publishes its manager under. */ export declare const AUTHKIT_BINDING = "authkit.client"; /** A crypto principal, as `@adonis-agora/crypto`'s service takes it. */ interface CryptoPrincipalLike { partition: string; capabilities: string[]; actor: string; } /** What this package reads of `@adonis-agora/crypto`'s runtime (`agora.crypto`). */ export interface CryptoRuntimeLike { topology: 'standalone' | 'control-plane' | 'tenant'; /** The app's crypto partition: its name on a standalone/control plane, its partition on a tenant. */ partition: string; /** Crypto embedded in this app (standalone, control plane): any partition's keys. */ service?: { encrypt(principal: CryptoPrincipalLike, input: { key: string; plaintext: string; aad?: string; }): Promise<{ ciphertext: string; }>; decrypt(principal: CryptoPrincipalLike, input: { key: string; ciphertext: string; aad?: string; context?: string; }): Promise<{ plaintext: string; }>; createKey(principal: CryptoPrincipalLike, input: { name: string; type: 'aes256-gcm'; description?: string; }): Promise<unknown>; store: { getKey(partition: string, name: string): Promise<unknown>; }; } | undefined; /** * This app's own operations (in-process on a server, over its configured channel on a tenant). * `encrypt` takes `partition` to seal for another app (a tenant console, `crypto:seal:<key>`). */ operations: { encrypt(input: { key: string; plaintext: string; aad?: string; context?: string; partition?: string; }): Promise<{ ciphertext: string; }>; decrypt(input: { key: string; ciphertext: string; aad?: string; context?: string; }): Promise<{ plaintext: string; }>; createKey?(input: { name: string; type: 'aes256-gcm'; description?: string; }): Promise<unknown>; /** * Whether this app's key exists (`false` when missing), without a decrypt that the crypto audit * would record as an error (§13f). Optional: older crypto versions lack it. */ hasKey?(input: { key: string; }): Promise<boolean>; }; } /** What this package reads of `@adonis-agora/authkit-client`'s manager (`authkit.client`). */ export interface AuthkitClientLike { appCallTokens?(): { token(app: string): Promise<string>; reset?(): void; } | null | undefined; appCallsConfig?(): unknown; /** Publishes one of this app's public keys in authkit's key directory (background, retried). */ publishAppKey?(input: { purpose: string; jwk: OkpJwk; }): Promise<void>; /** Another app's public key for `purpose` from authkit's key directory (cached), or `null`. */ appKey?(input: { app: string; purpose: string; }): Promise<OkpJwk | null>; appCallerVerifier?(): { verifyToken?(token: string): Promise<{ app: string; capabilities: string[]; clientId?: string; } | null>; } | null; } /** An X25519 public JWK (never `d`). */ export interface OkpJwk { kty: 'OKP'; crv: 'X25519'; x: string; kid?: string; } /** The purpose media's command key is published under in authkit's key directory. */ export declare const CONSOLE_KEY_PURPOSE = "console:media"; /** The public JWK of an app's command key, as published in authkit's key directory. */ export declare function commandKeyJwk(keys: { publicKey: string; kid: string; }): OkpJwk; /** Whether authkit's key directory can hand this console an app's command key. */ export declare function hasKeyDirectory(authkit: AuthkitClientLike | undefined): boolean; /** * §13a: token mode when the app published its key in authkit's directory, else crypto. Cached; * decided again after 5 minutes, or when the channel says the app did not take a command. */ export declare function directoryChooser(authkit: AuthkitClientLike, app: string, now?: () => number): ConsoleModeChooser; /** The siblings this app has, as the container says. */ export interface ConsoleSiblings { crypto?: CryptoRuntimeLike | undefined; authkit?: AuthkitClientLike | undefined; } interface ContainerLike { hasBinding?(binding: string): boolean; make(binding: string): Promise<unknown>; } /** * `@adonis-agora/crypto`'s runtime when it is booted in this app, else `undefined`. Throws when it * is registered but does not start (its own error, with this package's context). */ export declare function cryptoRuntimeOf(container: ContainerLike): Promise<CryptoRuntimeLike | undefined>; /** `@adonis-agora/authkit-client`'s manager when it is registered here, else `undefined`. */ export declare function authkitClientOf(container: ContainerLike): Promise<AuthkitClientLike | undefined>; /** Both siblings, as far as this app has them. */ export declare function consoleSiblingsOf(container: ContainerLike): Promise<ConsoleSiblings>; /** The exact command that creates the console key in `partition`, on the crypto server. */ export declare function createKeyCommand(partition: string, key?: string): string; /** * Make sure the console key (`aes256-gcm`) exists in `partition`, with crypto embedded in this app * (`runtime.service`): created when missing (idempotent: another instance creating it meanwhile is * fine). A failure throws with the exact command to run. Without `service` (a crypto tenant), see * {@link checkTenantConsoleKey}: this throws its warning. */ export declare function ensureConsoleKey(runtime: CryptoRuntimeLike, partition?: string, key?: string): Promise<'created' | 'exists'>; /** What the key-check block calls on this app's crypto. */ type CryptoOperationsLike = CryptoRuntimeLike['operations']; /** What one decrypt failure says about the key. */ export type ConsoleKeyOutcome = 'missing' | 'usable' | 'refused' | 'unreachable'; /** * Classifies a decrypt error from @adonis-agora/crypto (its `CryptoError` / client error carry a * `code`; the message is the fallback for other clients). `usable`: the key exists and this app * may decrypt with it (only the dummy ciphertext was rejected). */ export declare function classifyConsoleKeyError(error: unknown): ConsoleKeyOutcome; /** A `forbidden` refusal from crypto (its error code, or the message for other clients). */ export declare function isForbidden(error: unknown): boolean; /** The one line to log for `outcome`, or `undefined` when all is well. */ export declare function consoleKeyNote(runtime: Pick<CryptoRuntimeLike, 'partition'>, outcome: ConsoleKeyOutcome | 'created', key?: string, detail?: string): { level: 'info' | 'warn'; line: string; } | undefined; /** * Checks the console key once (a dummy decrypt) and, when it is missing, tries to create it * through the channel crypto already configured (works with `crypto:keys:manage:console-*`). * Never throws: returns the line to log, or `undefined` when the key is there and usable. */ export declare function checkTenantConsoleKey(runtime: CryptoRuntimeLike, key?: string): Promise<{ level: 'info' | 'warn'; line: string; } | undefined>; /** * The tenant's console key, watched: `atBoot()` runs {@link checkTenantConsoleKey} once; * `decrypt` wraps the app's own decrypt so that, while the last check warned, a command's failure * is classified and logged (one line, only when it changes) and its success clears the warning. * No extra crypto call per command. */ export declare function watchTenantConsoleKey(runtime: CryptoRuntimeLike, log: (note: { level: 'info' | 'warn'; line: string; }) => void, key?: string): { atBoot(): Promise<void>; decrypt: CryptoOperationsLike['decrypt']; }; /** * The console key at boot, as one log line (or none when it is simply there): created, or missing * with the exact command to run, or not checkable. Embedded crypto creates it; a crypto tenant is * checked with {@link checkTenantConsoleKey}. Never throws: commands fail until it is fixed. */ export declare function consoleKeyReport(runtime: CryptoRuntimeLike): Promise<{ level: 'info' | 'warn'; line: string; } | undefined>; /** * The owning app's cipher in automatic crypto mode: its own operations (it decrypts with the key in * its own partition). Where crypto is embedded here, the key is created on first use. */ export declare function appConsoleCipher(runtime: CryptoRuntimeLike, /** A crypto tenant's watched decrypt ({@link watchTenantConsoleKey}). */ decrypt?: CryptoRuntimeLike['operations']['decrypt']): ConsoleCipher; /** * The console's cipher for the federated app whose crypto partition is `partition`. Encrypt-only: * the console never opens a command. * * - Crypto embedded in the console's app (the Agora Hub): as `media:console`, with nothing but * `crypto:encrypt:console-media` on that partition; the key is created there on first use. * - Crypto in another app (the console is a crypto tenant): through the console's own crypto channel, * asking the server to seal with `partition`'s key (its client needs `crypto:seal:console-*`). */ export declare function consoleSideCipher(runtime: CryptoRuntimeLike, partition: string, key?: string): ConsoleCipher; /** An explicit crypto cipher (a crypto client, a KMS of your own). */ export interface ExplicitConsoleCrypto { cipher: ConsoleCipherSource; key?: string | undefined; /** Whether a ciphertext is this cipher's. Default: `agora:ct1:<key>:` (crypto's format). */ owns?: ((ciphertext: string) => boolean) | undefined; } /** `consoleCommands`, as far as authentication goes. */ export interface AppConsoleAuthConfig { prefix: string; verifyTenant?: TenantVerifierInput | false | undefined; partition?: string | undefined; crypto?: ExplicitConsoleCrypto | false | undefined; secret?: ChannelSecret | undefined; } /** How the owning app answers, once resolved. */ export interface ResolvedAppConsoleAuth { /** The channel's authentication options (without `client` and `prefix`). */ channel: ConsoleChannelAuthOptions; /** This app's name as a classic (service-tenant) token must name it. */ partition: string; /** Each way commands are accepted, for the boot log and the doctor. */ modes: string[]; /** Token mode without crypto: the key pair the console seals to (its public half is printed). */ keys?: ConsoleCommandKeys | undefined; /** The crypto in use: automatic (this app's runtime) or the configured cipher. */ crypto?: { auto: boolean; key: string; cipher: ConsoleCipherSource; } | undefined; /** Automatic crypto on a crypto tenant: checks the key once at boot (see watchTenantConsoleKey). */ checkKey?: (() => Promise<void>) | undefined; } /** `consoleCommands` is on, but no authentication resolves: the boot error says what to add. */ export declare class ConsoleAuthConfigError extends Error { constructor(message: string); } /** * Resolve how this app authenticates the commands of other apps' consoles. Explicit options win * (`crypto`, `secret`, `verifyTenant`) and nothing automatic is added to them. With none: * `@adonis-agora/crypto` booted here seals commands (key `console-media` in this app's partition), * authkit's calls between apps (or a control plane's own `verifyTenant`) checks the token they carry, * and, with an `APP_KEY`, tokens sealed to the APP_KEY-derived key are accepted too. * Throws a {@link ConsoleAuthConfigError} when nothing resolves. */ export declare function resolveAppConsoleAuth(input: { options: AppConsoleAuthConfig; /** A control plane's own `verifyTenant` (used when `consoleCommands.verifyTenant` is not set). */ controlPlaneVerifyTenant?: TenantVerifierInput | undefined; appKey: string | undefined; siblings: ConsoleSiblings; /** Logs the console key's state on a crypto tenant (automatic mode); see watchTenantConsoleKey. */ logKey?: ((note: { level: 'info' | 'warn'; line: string; }) => void) | undefined; }): ResolvedAppConsoleAuth; /** A federated app's `controlPlane`, as far as authentication goes. */ export interface ConsoleSideAuthConfig { secret?: ChannelSecret | undefined; token?: TenantTokenSource | false | undefined; publicKey?: string | (() => string | Promise<string>) | undefined; crypto?: ExplicitConsoleCrypto | false | undefined; } /** How the console signs its commands to one federated app, once resolved. */ export interface ResolvedConsoleSideAuth { /** The channel's authentication options (without `client` and `prefix`), or none. */ channel: ConsoleChannelAuthOptions; /** How, for the boot log and the doctor; `undefined` when nothing resolves. */ mode: string | undefined; /** Automatic crypto: the partition the key lives in (the app's crypto partition). */ autoPartition?: string | undefined; } /** * Resolve how this console authenticates its commands to the federated app whose name (and crypto * partition) is `partition`. Explicit options win; with none, the siblings decide: crypto seals * with `console-media` in `partition` (embedded here, or through this app's crypto channel), and * authkit adds this app's app-call token for that app. */ export declare function resolveConsoleSideAuth(input: { plane: ConsoleSideAuthConfig | RedisConsoleChannel; partition: string; siblings: ConsoleSiblings; /** The entry's key in `apps`, for the messages. Default: `partition`. */ app?: string | undefined; /** Where the console's warnings go (a token skipped in crypto mode, §13b). */ log?: ((level: 'info' | 'warn', line: string) => void) | undefined; }): ResolvedConsoleSideAuth; /** The boot error (or warning) for a federated app the console cannot authenticate to. */ export declare function consoleSideAuthMessage(key: string, siblings?: ConsoleSiblings, plane?: ConsoleSideAuthConfig | RedisConsoleChannel | undefined): string; export {}; //# sourceMappingURL=console_auth.d.ts.map