UNPKG

@targetprocess/tokiny

Version:

Client for easy retrieving access_tokens from Targetprocess Auth service.

74 lines (73 loc) 3.06 kB
/// <reference types="node" /> import * as https from 'https'; import * as openId from 'openid-client'; export interface IRequestInit { headers?: { append: (key: string, value: string) => void; } | ({ append: (key: string, value: string) => void; } & Iterable<[string, string]>) | Record<string, string | string[]> | Iterable<Iterable<string>>; method?: string | undefined; } export interface ITokenSet { access_token: string; expires_at: number; type: string; refresh_token?: string; } export interface IAuthRetryOptions { retry: boolean; retries?: number; factor?: number; minTimeout?: number; maxTimeout?: number; } type LogMethod = (msg: string | Error, err?: Error) => void; export interface IAuthLogger { error: LogMethod; warn: LogMethod; info: LogMethod; debug: LogMethod; } type ClientCertificateOptions = Partial<Pick<https.RequestOptions, 'ca' | 'cert' | 'key'>>; export interface IAuthOptions extends ClientCertificateOptions { authUrl: string; clientId: string; clientSecret: string; expirationLimitSeconds?: number; httpRequestTimeout?: number; useCache?: boolean; retryOptions?: IAuthRetryOptions; logger?: IAuthLogger; loggerMessagePrefix?: string; tokenEndpointAuthMethod?: openId.ClientAuthMethod; issuerMetadataOverrides?: Partial<openId.IssuerMetadata>; grantType?: string; } export declare class TokenFactory { private readonly authOptions; private cache; private tokenRetrievingStatus; constructor(authOptions: IAuthOptions); protected createOpenIdClient(authOptions: IAuthOptions): Promise<openId.BaseClient>; private tryGetFromCache; private getTokenKey; private addToCache; private getTokenFromAuthWithRetries; private getTokenFromAuth; private retrieveToken; getToken(scopes: string, refreshToken?: string): Promise<ITokenSet>; protected logDebug(msg: string): void; protected logWarn(msg: string, err?: Error): void; protected logError(msg: string, err: Error): void; private log; } export declare function createTokenFactory(authOptions: IAuthOptions): TokenFactory; export declare function httpClientFactory<TRequestInfo, TRequestInit extends IRequestInit, TResponse>(scopes: string, tokenFactory: any, fetch: (url: TRequestInfo, init?: TRequestInit) => Promise<TResponse>): { fetch: (input: TRequestInfo, init?: TRequestInit) => Promise<TResponse>; }; export declare function userHttpClientFactory<TRequestInfo, TRequestInit extends IRequestInit, TResponse>(authorizationProvider: (() => Promise<string>) | string, fetch: (url: TRequestInfo, init?: TRequestInit) => Promise<TResponse>): { fetch: (input: TRequestInfo, init?: TRequestInit) => Promise<TResponse>; }; export declare function doFetch<TRequestInfo, TRequestInit extends IRequestInit, TResponse>(authorization: string, fetch: (url: TRequestInfo, init?: TRequestInit) => Promise<TResponse>, input: TRequestInfo, init?: TRequestInit): Promise<TResponse>; export {};