@joingo/sdk-security
Version:
SDK for JavaScript: Security Application Block!
84 lines (77 loc) • 3.87 kB
text/typescript
/* ************************************************************************************************************************ *\
* SDK for JavaScript! *
* *
* COPYRIGHT © 2021 BEIJING JOINGO INFORMATION TECHNOLOGY CO., LTD. ALL RIGHTS RESERVED. *
* LICENSED UNDER THE MIT LICENSE. SEE LICENSE FILE IN THE PROJECT ROOT FOR FULL LICENSE INFORMATION. *
* *
* AUTHOR : WANG YUCAI *
* E-MAIL ADDRESS: WANGYUCAI@JOINGO.VIP *
* DATE TIME : 2021-12-29 11:47 *
\* ************************************************************************************************************************ */
import { VoidFunc1 } from "@joingo/sdk-core";
// CODEFILE: authentication.ts
// FEATURE: 提供了身份认证相关的方法。
// FILE-VERSION: v2021.12.29-build.1147
/**
* 定义了基于用户名、密码的身份认证凭据结构。
*/
export type BasicCredentialsConstructor = {
/**
* 获取一个字符串,用于表示登录用户名。
*
* @type {string}
* @readonly
*/
readonly userName: string;
/**
* 获取一个字符串,用于表示登录密码。
*
* @type {string}
* @readonly
*/
readonly password: string;
};
/**
* (异步的方法) 基础身份认证方法。
*
* @interface AsyncBasicAuthenticateHandler
* @template TAuthenticatedResult
* @async
*/
interface AsyncBasicAuthenticateHandler<TAuthenticatedResult> {
/**
* @param {BasicCredentialsConstructor} credentials 身份认证凭据。
* @returns {Promise<TAuthenticatedResult>}
* @memberof AsyncBasicAuthenticateHandler
*/
(credentials: BasicCredentialsConstructor): Promise<TAuthenticatedResult>;
}
/**
* 执行登录。
*
* @export
* @template TAuthenticatedResult 身份认证结果。
* @param {BasicCredentialsConstructor} credentials 基础身份认证凭据。
* @param {AsyncBasicAuthenticateHandler<TAuthenticatedResult>} authenHandler 基础身份认证方法。
* @param {VoidFunc1<TAuthenticatedResult>} successCallback 基础身份认证成功回调方法。
* @param {VoidFunc1<any>} [failedCallback] 基础身份认证失败回调方法。
*/
export function signin<TAuthenticatedResult>(credentials: BasicCredentialsConstructor, authenHandler: AsyncBasicAuthenticateHandler<TAuthenticatedResult>, successCallback: VoidFunc1<TAuthenticatedResult>, failedCallback?: VoidFunc1<any>): void {
authenHandler(credentials).then(result => successCallback(result)).catch(error => {
console.error(`[ERROR]: 执行基础身份认证失败。详情参见:%o`, error);
if (failedCallback) failedCallback(error);
});
}
/**
* (异步的方法) 执行登录。
*
* @export
* @template TAuthenticatedResult
* @param {BasicCredentialsConstructor} credentials 基础身份认证凭据。
* @param {AsyncBasicAuthenticateHandler<TAuthenticatedResult>} authenHandler 基础身份认证方法。
* @returns {Promise<TAuthenticatedResult>}
* @async
*/
export function signinAsync<TAuthenticatedResult>(credentials: BasicCredentialsConstructor, authenHandler: AsyncBasicAuthenticateHandler<TAuthenticatedResult>): Promise<TAuthenticatedResult> {
return authenHandler(credentials);
}