@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
166 lines • 9.19 kB
TypeScript
import type { HttpContext } from '@adonisjs/core/http';
import { type ConsoleAuthorize, type ConsoleAuthorizeDecision } from './console_authorize.js';
import { type ConsoleCapabilityTable } from './console_capabilities.js';
/** The capability {@link authorizeAgoraCaller} asks of a calling app to read object bytes. */
export declare const MEDIA_OBJECTS_READ = "media:objects:read";
/** The capability {@link authorizeAgoraCaller} asks of a calling app to run actions (`actions`). */
export declare const MEDIA_OBJECTS_WRITE = "media:objects:write";
/** Media's console capabilities, each a read or a write (for {@link consoleCapabilityAllows}). */
export declare const MEDIA_CONSOLE_CAPABILITIES: ConsoleCapabilityTable;
/**
* Whether `granted` (a token's or a viewer's capabilities) allows the console capability
* `required`: the shared matcher (`console_capabilities.ts`) with media's table. `*`, `media:*`,
* `media:console:*`, `media:console:write` (reads and writes), `media:console:read` (reads), the
* exact name, or a hierarchical grant like `media:objects:*`. A `*` anywhere else grants nothing.
*/
export declare function capabilitiesAllow(granted: readonly unknown[], required: string): boolean;
/**
* One granted capability against `required`, by the same rules as {@link capabilitiesAllow}.
* @deprecated Use {@link capabilitiesAllow} (a list of grants).
*/
export declare function capabilityAllows(granted: string, required: string): boolean;
/** Who is calling this console from another app, as an {@link IdentifyAppCaller} found it. */
export interface AppCaller {
/** The calling app's name. */
app: string;
/** Whatever the identification carried (a token's capabilities, a client row…), for `permit`. */
claims?: Record<string, unknown> | undefined;
}
/**
* WHO is calling: the app, or `undefined` when the request does not come from one (a `401`). Read a
* verified token, a client certificate, a shared secret, a row of your own `api_keys` table…
*/
export type IdentifyAppCaller = (ctx: HttpContext) => AppCaller | undefined | null | Promise<AppCaller | undefined | null>;
/** CAN that app do this: `permission` is `media:objects:read` or `media:objects:write` (by default). */
export type PermitAppCaller = (caller: AppCaller, permission: string, ctx: HttpContext) => boolean | Promise<boolean>;
/** Options for {@link authorizeAppCaller}. */
export interface AuthorizeAppCallerOptions {
/** Who is calling: {@link identifyAgoraCaller}, {@link identifySharedSecret}, or your own. */
identify: IdentifyAppCaller;
/** What it may do: {@link permitTokenCapabilities}, {@link permitAll}, or your own. */
permit: PermitAppCaller;
/** The calling apps let in, by name. Omit for any `identify` finds. */
apps?: string[] | undefined;
/** The permission asked to read object bytes. Default {@link MEDIA_OBJECTS_READ}. */
read?: string | undefined;
/**
* The permission asked to run actions ({@link MEDIA_ACTION_ROUTES}), or `false` to open no action
* at all. Default {@link MEDIA_OBJECTS_WRITE}.
*/
write?: string | false | undefined;
}
/** The app {@link authorizeAppCaller} let this request in for, or `undefined` (a viewer, or none). */
export declare function appCallerOf(ctx: HttpContext): AppCaller | undefined;
/**
* The console's action routes, by name, that an app caller may run with the write permission: what
* a federated console sends (`proxyActions`). Uploading and the Organization switch are not among
* them.
*/
export declare const MEDIA_ACTION_ROUTES: readonly string[];
/**
* An `authorize` check for another app's federated console calling this console. `identify` says
* who is calling, `permit` whether it may; the routes are fixed:
*
* - `GET <apiBase>/object/raw` (its `proxyObjects`) with the `read` permission;
* - `POST` on the action routes ({@link MEDIA_ACTION_ROUTES}, its `proxyActions`) with `write`;
* - nothing else.
*
* A caller sees and acts on every tenant's media of THIS app, never that of the other apps this
* console federates (`apps`). Nobody identified: `401`; an app not let in, not permitted, or on any
* other route: `403`. Combine it with the viewer check through {@link anyAuthorize}.
*
* ```ts
* authorize: anyAuthorize(
* authorizeAppCaller({ identify: identifySharedSecret({ apps: { hub: env.get('MEDIA_HUB_SECRET') } }), permit: permitAll() }),
* adminsOnly,
* ),
* ```
*/
export declare function authorizeAppCaller(options: AuthorizeAppCallerOptions): ConsoleAuthorize;
/**
* {@link IdentifyAppCaller} for Agora apps signed in through `@adonis-agora/authkit`: the caller is
* `ctx.agora.caller`, which `@adonis-agora/authkit-client` (1.0.0-rc.9 or later) sets from a
* verified service token (`service.name` in config/authkit_client.ts). Its `claims.capabilities` are
* the capabilities granted to it on this app (`authkit:client grant-app`).
*/
export declare function identifyAgoraCaller(): IdentifyAppCaller;
/** Options for {@link identifySharedSecret}. */
export interface IdentifySharedSecretOptions {
/** The calling apps, by name, each with the secret it sends. Long and random; one per app. */
apps: Record<string, string | undefined>;
/**
* The request header carrying the secret. Default `authorization`, as `Bearer <secret>`; any
* other header carries the secret as is.
*/
header?: string | undefined;
}
/**
* {@link IdentifyAppCaller} without an identity provider: each calling app sends its own secret
* (`Authorization: Bearer <secret>` by default), compared in constant time. A missing or wrong
* secret identifies nobody (`401`). Throws at config time on an app with an empty secret.
*/
export declare function identifySharedSecret(options: IdentifySharedSecretOptions): IdentifyAppCaller;
/**
* {@link PermitAppCaller} reading the capabilities the caller's token carries
* (`claims.capabilities`, set by {@link identifyAgoraCaller}), with the Agora wildcard rule
* ({@link capabilityAllows}).
*/
export declare function permitTokenCapabilities(): PermitAppCaller;
/** {@link PermitAppCaller} for trusted callers: every identified app may read and act. */
export declare function permitAll(): PermitAppCaller;
/** Options for {@link authorizeAgoraCaller}. */
export interface AuthorizeAgoraCallerOptions {
/** The calling apps let in, by name (the token's partition, e.g. `agora-hub`). Omit for any. */
apps?: string[] | undefined;
/** What the caller must hold to read object bytes. Default {@link MEDIA_OBJECTS_READ}. */
capability?: string | undefined;
/**
* Also let a caller run the console's actions ({@link MEDIA_ACTION_ROUTES}): another app's
* federated console with `proxyActions`. Default `false`: object bytes only.
*/
actions?: boolean | undefined;
/** What the caller must hold to run actions. Default {@link MEDIA_OBJECTS_WRITE}. */
actionsCapability?: string | undefined;
}
/**
* {@link authorizeAppCaller} with {@link identifyAgoraCaller} and {@link permitTokenCapabilities}:
* an Agora app calling with an authkit service token may read object bytes with
* `media:objects:read` and, with `actions: true`, run actions with `media:objects:write`.
*
* ```ts
* authorize: anyAuthorize(authorizeAgoraCaller({ apps: ['agora-hub'], actions: true }), adminsOnly),
* ```
*/
export declare function authorizeAgoraCaller(options?: AuthorizeAgoraCallerOptions): ConsoleAuthorize;
/** Options for {@link mediaCallerCsrfExceptions}. */
export interface MediaCallerCsrfOptions {
/** Where the console's API is mounted (its `apiBasePath`). Default `/media/dashboard/api`. */
apiBase?: string | undefined;
/**
* The header the calling app proves itself with. Default `authorization` (a `Bearer` token); the
* `header` of {@link identifySharedSecret} when it uses another one.
*/
header?: string | undefined;
}
/**
* For `@adonisjs/shield`'s `csrf.exceptRoutes`: `true` for a request another app's federated
* console sends this console's API (`proxyActions`), so the CSRF check does not refuse it. Such a
* request carries `Authorization: Bearer` (or the `header` you name) and no cookie: a browser cannot
* send it from another site without a CORS preflight, and without a cookie it carries no viewer
* session, so `authorize` can only let it in through the app's credential. Everything else keeps
* its check.
*
* ```ts
* csrf: { enabled: true, exceptRoutes: (ctx) => mediaCallerCsrfExceptions(ctx, { apiBase: '/media/api' }) },
* ```
*/
export declare function mediaCallerCsrfExceptions(ctx: HttpContext, options?: MediaCallerCsrfOptions): boolean;
/**
* The capabilities a viewer's `authorize` decision grants on media's console (§4b): only the ones
* that reach media's console set (`media:objects:read`, `media:objects:write`), all of them when
* the decision says nothing, and no write with `access: 'read'`.
*/
export declare function viewerConsoleCapabilities(decision: Pick<Extract<ConsoleAuthorizeDecision, {
allowed: true;
}>, 'capabilities' | 'access'> | undefined): string[];
//# sourceMappingURL=console_apps.d.ts.map