2023-10-12 13:54:00 +08:00
|
|
|
import { Gateway, InstallOptions, Plugin } from '@nocobase/server';
|
2022-11-29 23:18:21 +08:00
|
|
|
import { getAuthUrl } from './actions/getAuthUrl';
|
|
|
|
import { redirect } from './actions/redirect';
|
2023-06-07 23:46:42 +08:00
|
|
|
import { authType } from '../constants';
|
|
|
|
import { OIDCAuth } from './oidc-auth';
|
2023-10-12 13:54:00 +08:00
|
|
|
import { resolve } from 'path';
|
2022-11-29 23:18:21 +08:00
|
|
|
|
|
|
|
export class OidcPlugin extends Plugin {
|
|
|
|
afterAdd() {}
|
|
|
|
|
|
|
|
beforeLoad() {}
|
|
|
|
|
|
|
|
async load() {
|
2023-10-12 13:54:00 +08:00
|
|
|
this.db.addMigrations({
|
|
|
|
namespace: 'auth',
|
|
|
|
directory: resolve(__dirname, 'migrations'),
|
|
|
|
context: {
|
|
|
|
plugin: this,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-06-07 23:46:42 +08:00
|
|
|
this.app.authManager.registerTypes(authType, {
|
|
|
|
auth: OIDCAuth,
|
|
|
|
});
|
2022-11-29 23:18:21 +08:00
|
|
|
|
|
|
|
// 注册接口
|
|
|
|
this.app.resource({
|
|
|
|
name: 'oidc',
|
|
|
|
actions: {
|
|
|
|
getAuthUrl,
|
|
|
|
redirect,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
this.app.acl.allow('oidc', '*', 'public');
|
2023-10-12 13:54:00 +08:00
|
|
|
|
|
|
|
Gateway.getInstance().addAppSelectorMiddleware(async (ctx, next) => {
|
|
|
|
const { req } = ctx;
|
|
|
|
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
|
|
const params = url.searchParams;
|
|
|
|
const state = params.get('state');
|
|
|
|
if (!state) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
const search = new URLSearchParams(state);
|
|
|
|
const appName = search.get('app');
|
|
|
|
if (appName) {
|
|
|
|
ctx.resolvedAppName = appName;
|
|
|
|
}
|
|
|
|
await next();
|
|
|
|
});
|
2022-11-29 23:18:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async install(options?: InstallOptions) {}
|
|
|
|
|
|
|
|
async afterEnable() {}
|
|
|
|
|
|
|
|
async afterDisable() {}
|
|
|
|
|
|
|
|
async remove() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default OidcPlugin;
|