UNPKG

koas-security

Version:

Koas security checks if a request matches the security requirement of an operation. For example, given the following partial OpenAPI document:

31 lines (30 loc) 1.08 kB
import { Plugin } from 'koas-core'; import { GetApiKeyUser } from './apiKey'; import { GetHttpUser } from './http'; import { GetOAuth2User } from './oauth2'; import { OAuth2Client } from './types'; /** * Known clients that may be assigned to `ctx.client` and `ctx.clients`. */ export interface Clients { } /** * Known clients that may be assigned to `ctx.user` and `ctx.users`. */ export interface Users { } /** * A function for getting a user. */ export declare type GetUser<User = unknown, Client = unknown> = GetApiKeyUser<User, Client> | GetHttpUser<User, Client> | GetOAuth2User<User, Client>; export { GetApiKeyUser, GetHttpUser, GetOAuth2User, OAuth2Client }; export declare type SecurityOptions = { [K in keyof Users]: GetUser<Users[K], Clients[K]>; }; /** * Authenticate users based on OpenAPI security schemes and security requirements. * * @param options - A mapping of OpenAPI security definition keys to user getter functions. * @returns A Koas plugn for authenticating users. */ export declare function security(options: SecurityOptions): Plugin;