tachybase_todo/packages/plugins/oidc/src/server/plugin.ts

62 lines
1.5 KiB
TypeScript
Raw Normal View History

import { InstallOptions, Plugin } from '@nocobase/server';
import { generators } from 'openid-client';
import { resolve } from 'path';
import { getAuthUrl } from './actions/getAuthUrl';
import { getEnabledProviders } from './actions/getEnabledProviders';
import { redirect } from './actions/redirect';
import { oidc } from './authenticators/oidc';
export class OidcPlugin extends Plugin {
#OIDC_NONCE = null;
afterAdd() {}
beforeLoad() {}
async load() {
// 导入 collection
feat: duplicator plugin (#1265) * chore: dump plugin * chore: rename plugin * chore: add duplicator into preset * chore: tmp commit * feat: restore & dump action * feat: collection dump & restore * feat: collection dump & restore * fix: dump with json type * fix: dump uischema * chore: tmp commit * chore: tmp commit * feat: restore custom collections * chore: code * fix: build * chore: tmp commit * fix: pm.generateClientFile * feat: dump with user plugins * feat: restore ignore collection * feat: ignore user with rolesUsers * chore: client plugins * refactor: restore insert sql * chore: code format * feat: restore with sequelize insert query * fix: restore json field * fix: json restore * refactor: dumper * refactor: restorer * chore: dump file name * chore: dump file name * chore: dump message * fix: restore with jsonb fields * feat: field data writer * chore: code * feat: collection group manager * feat: duplicator client * feat: duplicator panel * chore: disable duplicator ui * feat: dump with inquirer * chore: dumper * chore: collection group manager * feat: restore with inquirer * chore: comment * chore: inquirer page size * feat: warning before restore * feat: sync postgres sequence id after import collection * chore: restore checked * feat: dump with through table * feat: restore with through table * feat: restore with sequence field * chore: graph collection manager collection group * fix: dump with no column tables * fix: dump empty table * fix: force remove workdir * chore: disable throw error when sync empty table * feat: support map field restore * fix: restore from pg dumped file * fix: dump with logic field * chore: console.log * chore: collection group * chore: handle import collection error * fix: dump migrations table * feat: display custom collection title * fix: restore collection title display * fix: dump iframe html * fix: dump with postgres inhertitance * fix: dump sql * chore: export snapshot field * fix: import with sequences * fix: import sequences * fix: storage Co-authored-by: chenos <chenlinxh@gmail.com>
2023-01-08 12:45:02 +08:00
await this.importCollections(resolve(__dirname, '../collections'));
// 获取 User 插件
const userPlugin = this.app.getPlugin<any>('users');
// 注册 OIDC 验证器
userPlugin.authenticators.register('oidc', oidc);
// 注册接口
this.app.resource({
name: 'oidc',
actions: {
getAuthUrl,
redirect,
getEnabledProviders,
},
});
// 注册中间件,处理 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();
});
// 开放访问权限
this.app.acl.allow('oidcProviders', '*', 'allowConfigure');
this.app.acl.allow('oidc', '*');
}
async install(options?: InstallOptions) {}
async afterEnable() {}
async afterDisable() {}
async remove() {}
}
export default OidcPlugin;