跳到主要内容

模板文件

模板文件中包含自定义auth插件需要的两个文件:CustomAuth.ts和tsconfig.json。

插件模板CustomAuth.ts

import {AuthPlugin} from '../libs/plugins/auth/AuthPlugin'; // 插件文件CustomAuth.ts存储在默认位置(ohpm-repo解压根目录的plugins文件夹内),默认引用地址
export class CustomAuth implements AuthPlugin {
/**
* 用户认证:根据accessToken的值,返回用户的数据:用户的id,用户的名字,用户所属的组织,用户所创建的组织
* @param accessToken 用户的accessToken
*/
async auth(accessToken: string): Promise<{
id: string; // 用户的id,保证唯一性
name: string; // 用户的名字,保证唯一性
belongGroupList: Array<string>; // 用户所在的组织,具有发布指定组织包的权限
groupAdminList: Array<string>; // 用户所管理的组织,具有删除指定组织内包的权限
}> {
let id: string, name: string, belongGroupList: Array<string>, groupAdminList: Array<string>;
return {id, name, belongGroupList, groupAdminList};
}

/**
* 用户信息获取:根据只读accessToken的值,返回用户的数据:用户id,用户名字,用户所属的组织,用户所创建的组织
* @param accessToken 用户的只读accessToken
*/
async authWithReadOnly(accessToken: string): Promise<{
id: string; // 用户的id,保证唯一性
name: string; // 用户的名字,保证唯一性
belongGroupList: Array<string>; // 用户所在的组织,具有发指定组织包的权限
groupAdminList: Array<string>; // 用户所管理的组织,具有删除指定组织内包的权限
}> {
let id: string, name: string, belongGroupList: Array<string>, groupAdminList: Array<string>;
return {id, name, belongGroupList, groupAdminList};
}

/**
* 根据用户id,返回用户的名字
* @param id 用户的id值
*/
async getUserInfo(id: string): Promise<string> {
let name: string;
return name;
}
}

ts编译的配置文件tsconfig.json

// tsconfig.json 文件指定了编译项目所需的根目录下的文件以及编译选项,编译自定义插件文件.ts为.js文件。
{
"include": [
"plugins/*" // 插件文件的位置
],
"compilerOptions": {
"target": "es2016",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "commonjs",
"rootDirs": [
"./src",
"./test"
],
"typeRoots": [
"./node_modules/@types"
],
"types": [
"node",
],
"resolveJsonModule": true,
"outDir": "./plugins/outDir", // 编译后文件输出的位置
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"alwaysStrict": true,
"noImplicitReturns": true,
"skipLibCheck": true
}
}