UNPKG

@emeraldpay/api

Version:
112 lines (111 loc) 3.22 kB
import { CredentialsClient, SecretToken } from "./typesAuth"; /** * A general interface to modify request headers */ export interface Headers { /** * Add a header to the request * * @param key header name (ex `Authorization`) * @param value header value (ex `Bearer <token>`) */ add(key: string, value: string): void; } /** * Current authentication details */ export interface AuthDetails { /** * Add the authentication details to the request headers * @param meta */ applyAuth(meta: Headers): void; /** * Check if it's expired and needs to be refreshed from server before applying */ isExpired(): boolean; } /** * No authentication, just pass the requests as is */ export declare class NoAuth implements AuthDetails { applyAuth(_meta: Headers): void; isExpired(): boolean; } /** * Interface to an authentication provider. This provider get an actual auth details, such as JWT, from the server. */ export interface EmeraldAuthenticator { authenticate(): Promise<AuthDetails>; refresh(): Promise<AuthDetails>; } export type AuthenticationListener = (status: AuthenticationStatus, tokenStatus: TokenStatus) => void; export declare enum AuthenticationStatus { AUTHENTICATING = 0, AUTHENTICATED = 1, ERROR = 2 } /** * JWT based authentication */ export declare class JwtSignature implements AuthDetails { token: string; expire: Date; constructor(token: string, expire: Date); applyAuth(meta: Headers): void; update(token: string, expire: Date): void; isExpired(): boolean; } export declare enum TokenStatus { REQUIRED = 0, REQUESTED = 1, SUCCESS = 2, ERROR = 3 } /** * Interface to access the current auth provide per API client */ export interface Signer { /** * Get current authentication details */ getAuth(): Promise<AuthDetails>; /** * Listen for authentication status changes * * @param listener */ setListener(listener: AuthenticationListener): void; /** * Set a new authentication provider. * Usually, it's created automatically by the signer, as it knows what kind of provider it needs. * * @param provider */ setAuthentication(provider: EmeraldAuthenticator): void; } export declare class NoSigner implements Signer { getAuth(): Promise<AuthDetails>; setListener(listener: AuthenticationListener): void; setAuthentication(_authentication: EmeraldAuthenticator): void; } /** * Standard signer based on JWT authentication provider (initiated automatically) * * @see JwtAuthProvider */ export declare class StandardSigner implements Signer { private readonly client; private readonly secretToken; private readonly agents; private tokenStatus; private token; private provider; private listener; private authenticationStatus; constructor(client: CredentialsClient, secretToken: SecretToken, agents: string[]); getAuth(): Promise<AuthDetails>; protected notify(status: AuthenticationStatus): void; setListener(listener: AuthenticationListener): void; setAuthentication(authentication: EmeraldAuthenticator): void; }