2022-11-29 23:18:21 +08:00
|
|
|
import { InstallOptions, Plugin } from '@nocobase/server';
|
|
|
|
import { generators } from 'openid-client';
|
2022-11-30 01:00:45 +08:00
|
|
|
import { resolve } from 'path';
|
2022-11-29 23:18:21 +08:00
|
|
|
import { getAuthUrl } from './actions/getAuthUrl';
|
2022-11-30 01:00:45 +08:00
|
|
|
import { getEnabledProviders } from './actions/getEnabledProviders';
|
2022-11-29 23:18:21 +08:00
|
|
|
import { redirect } from './actions/redirect';
|
2022-11-30 01:00:45 +08:00
|
|
|
import { oidc } from './authenticators/oidc';
|
2022-11-29 23:18:21 +08:00
|
|
|
|
|
|
|
export class OidcPlugin extends Plugin {
|
|
|
|
#OIDC_NONCE = null;
|
|
|
|
|
|
|
|
afterAdd() {}
|
|
|
|
|
|
|
|
beforeLoad() {}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
// 导入 collection
|
2023-01-08 20:47:00 +08:00
|
|
|
await this.importCollections(resolve(__dirname, './collections'));
|
2022-11-29 23:18:21 +08:00
|
|
|
|
|
|
|
// 获取 User 插件
|
2022-11-30 01:00:45 +08:00
|
|
|
const userPlugin = this.app.getPlugin<any>('users');
|
2022-11-29 23:18:21 +08:00
|
|
|
|
|
|
|
// 注册 OIDC 验证器
|
|
|
|
userPlugin.authenticators.register('oidc', oidc);
|
|
|
|
|
|
|
|
// 注册接口
|
|
|
|
this.app.resource({
|
|
|
|
name: 'oidc',
|
|
|
|
actions: {
|
|
|
|
getAuthUrl,
|
|
|
|
redirect,
|
2022-11-30 01:00:45 +08:00
|
|
|
getEnabledProviders,
|
2022-11-29 23:18:21 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// 注册中间件,处理 nonce 值
|
|
|
|
this.app.use(async (ctx, next) => {
|
|
|
|
if (ctx.url.startsWith('/api/users:signin?authenticator=oidc')) {
|
|
|
|
ctx.OIDC_NONCE = this.#OIDC_NONCE;
|
|
|
|
}
|
|
|
|
if (ctx.url.startsWith('/api/oidc:getAuthUrl')) {
|
|
|
|
ctx.OIDC_NONCE = this.#OIDC_NONCE = generators.nonce();
|
|
|
|
}
|
|
|
|
await next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// 开放访问权限
|
2022-11-30 01:00:45 +08:00
|
|
|
this.app.acl.allow('oidcProviders', '*', 'allowConfigure');
|
2022-11-29 23:18:21 +08:00
|
|
|
this.app.acl.allow('oidc', '*');
|
|
|
|
}
|
|
|
|
|
|
|
|
async install(options?: InstallOptions) {}
|
|
|
|
|
|
|
|
async afterEnable() {}
|
|
|
|
|
|
|
|
async afterDisable() {}
|
|
|
|
|
|
|
|
async remove() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default OidcPlugin;
|