UNPKG

@gdjiami/jslib

Version:

Jiami FrontEnd helpers and Services

116 lines (115 loc) 2.69 kB
import { EventEmitter } from './EventEmitter'; export declare const SMS_CLIENT_TYPE: string[]; export declare const TOKEN_HEADER = "X-Access-Token"; /** * 鉴权类型 */ export declare enum GRANT_TYPE { Auth = "authorization_code", Refresh = "refresh_token" } /** * 鉴权信息 */ export interface AuthInfo { accessToken: string; refreshToken: string; deadline: number; corpId: string; clientType: string; userId: string; name: string; } /** * 鉴权参数 */ export interface AuthParams { code: string; corpId: string; clientId: string; grantType: GRANT_TYPE; } /** * 基于密码的鉴权参数 */ export interface AuthByPasswordParams { user: string; password: string; extra?: any; } /** * refresh token 参数 */ export interface RefreshParams { refreshToken: string; grantType: GRANT_TYPE; } /** * 配置项 */ export interface TokenAuthServiceConfig { clientId: string; /** * 通过code鉴权 */ getToken?: (params: AuthParams) => Promise<AuthInfo>; /** * 通过用户名密码鉴权 */ getTokenByPassword?: (params: AuthByPasswordParams) => Promise<AuthInfo>; /** * 刷新token */ refreshToken: (params: RefreshParams) => Promise<AuthInfo>; onAuthSuccess?: (info: AuthInfo) => void; onAuthFailed?: (error: Error) => void; onRefreshFailed?: (error: Error) => void; refreshErrorMessage?: string; storage?: Storage; } /** * 基于Token的鉴权服务 * ## events * info-updated */ export declare class TokenAuthService extends EventEmitter { static INFO_UPDATED: string; private config; private info; private refreshing; private refreshCallbacks; private lastRefreshTime?; private refreshTimer; constructor(config: TokenAuthServiceConfig); private readonly storage; logout(): void; clean(): void; /** * 更新鉴权信息 */ saveAuthInfo(info: AuthInfo): void; /** * 通过用户名密码鉴权 */ authByPassword(params: AuthByPasswordParams): Promise<void>; /** * token 鉴权 */ auth(overrideParams?: object): Promise<void>; /** * 刷新token * 这个方法可能会被调用多次,所以要将它们加入队列,以便后续重试 */ refresh(): Promise<void>; isAdmin(): boolean; /** * 获取token */ getToken(): string; /** * 获取用户信息 */ getUserInfo(): AuthInfo; private infoUpdated; private handleAuthSuccess; }