@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
96 lines • 4.13 kB
TypeScript
import type { HttpContext } from '@adonisjs/core/http';
/**
* What a console `authorize` check may answer. The same shape is accepted by every
* `@adonis-agora` console (durable, telescope, media, payments, agent, the authkit admin), so one
* authorizer works for all of them.
*
* - `true` / `{ allowed: true }` let the request through.
* - `false` / `{ allowed: false }` deny it with `403`; `reason: 'unauthenticated'` makes it a `401`.
* - `{ redirectTo }` sends a page navigation to that path (`302`); an API call gets `401` JSON.
*/
export type ConsoleAuthorizeResult = boolean | {
allowed: true;
/**
* With tenancy on: the tenants (organizations) this viewer may see in the console, or `'*'`
* for every tenant plus files with no tenant. Omit for the viewer's own active tenant.
*/
tenants?: '*' | string[];
} | {
allowed: false;
reason?: 'unauthenticated' | 'forbidden';
} | {
redirectTo: string;
};
/** A class form, for an authorizer in its own file (`@inject()` works). */
export interface ConsoleAuthorizer {
authorize(ctx: HttpContext): ConsoleAuthorizeResult | Promise<ConsoleAuthorizeResult>;
}
export type ConsoleAuthorizerClass = new (...args: any[]) => ConsoleAuthorizer;
/**
* The console `authorize` option, in three forms:
*
* ```ts
* authorize: (ctx) => ctx.auth.user?.isAdmin === true, // inline function
* authorize: ConsoleAccess, // a ConsoleAuthorizer class
* authorize: () => import('#policies/console_access'), // lazy import of one
* ```
*
* Classes are built through the AdonisJS container, so `@inject()` constructor dependencies work.
* A check that throws denies the request (`403`).
*/
export type ConsoleAuthorize = ((ctx: HttpContext) => ConsoleAuthorizeResult | Promise<ConsoleAuthorizeResult>) | ConsoleAuthorizerClass | (() => Promise<{
default: ConsoleAuthorizerClass;
}>);
/** The outcome of a console `authorize` check, normalized. */
export type ConsoleAuthorizeDecision = {
allowed: true;
tenants?: '*' | string[];
} | {
allowed: false;
status: 401 | 403;
reason: 'unauthenticated' | 'forbidden';
} | {
allowed: false;
status: 401;
reason: 'unauthenticated';
redirectTo: string;
};
/** Normalize an `authorize` answer. Anything that is not an explicit allow denies. */
export declare function normalizeConsoleAuthorizeResult(result: unknown): ConsoleAuthorizeDecision;
/**
* Turn a {@link ConsoleAuthorize} option into a check that runs per request. A class is built
* through `ctx.containerResolver`; a lazy import is loaded once and remembered. A throw denies.
*/
export declare function resolveConsoleAuthorize(authorize: ConsoleAuthorize): (ctx: HttpContext) => Promise<ConsoleAuthorizeDecision>;
/** Options for {@link authorizeByToken}. */
export interface AuthorizeByTokenOptions {
/** The shared secret callers send as `Authorization: Bearer <token>`. */
token: string | undefined;
}
/**
* An `authorize` check for machine access: allows a request whose `Authorization` header is
* `Bearer <token>`, and answers `{ allowed: false, reason: 'unauthenticated' }` (`401`) otherwise.
*
* ```ts
* authorize: authorizeByToken({ token: env.get('CONSOLE_TOKEN') }),
* ```
*
* Throws at config time when `token` is empty or undefined.
*/
export declare function authorizeByToken(options: AuthorizeByTokenOptions): ConsoleAuthorize;
/**
* An `authorize` that allows a request when ANY of `authorizers` allows it, tried in order — e.g. a
* machine token for MCP clients OR a signed-in admin for the browser:
*
* ```ts
* authorize: anyAuthorize(
* authorizeByToken({ token: env.get('CONSOLE_TOKEN') }),
* (ctx) => ctx.auth.user?.isAdmin === true,
* ),
* ```
*
* When none allows: a redirect if one asked for it, `401` if any said "unauthenticated", `403`
* otherwise. A throwing authorizer counts as a `403` and the next one is still tried.
*/
export declare function anyAuthorize(...authorizers: ConsoleAuthorize[]): (ctx: HttpContext) => Promise<ConsoleAuthorizeResult>;
//# sourceMappingURL=console_authorize.d.ts.map