tachybase_todo/packages/plugins/api-keys/src/server/plugin.ts
Dunqing 25a3a8affa
feat: support token blacklist (#2168)
* feat: support token blacklist, Close T-799

* feat: clean

* fix: possible token does not exist

* fix: update

* feat: update

* feat: add node-cron to delete expired token

* fix: findOrCreate not work and add test case

* test: add token-blacklist tests

* feat: add test cases for blacklist in authManager

* test: update better

* fix: should hidden token field

* test: clean

* test: clean

* fix: should stop cron in afterStop

* refactor: move delete expired token in token blacklist service

* feat: remove plugin disable/enable logic

* fix: clean

* test: revert

* fix: cron typo
2023-07-05 21:57:57 +08:00

54 lines
1.4 KiB
TypeScript

import { Plugin } from '@nocobase/server';
import { resolve } from 'path';
import { NAMESPACE } from '../constants';
import { create, destroy } from './actions/api-keys';
import { enUS, zhCN } from './locale';
export interface ApiKeysPluginConfig {
name?: string;
}
export default class ApiKeysPlugin extends Plugin<ApiKeysPluginConfig> {
resourceName = 'apiKeys';
constructor(app, options) {
super(app, options);
}
async beforeLoad() {
this.app.i18n.addResources('zh-CN', NAMESPACE, zhCN);
this.app.i18n.addResources('en-US', NAMESPACE, enUS);
await this.app.resourcer.define({
name: this.resourceName,
actions: {
create,
destroy,
},
only: ['list', 'create', 'destroy'],
});
this.app.acl.registerSnippet({
name: ['pm', this.name, 'configuration'].join('.'),
actions: ['apiKeys:list', 'apiKeys:create', 'apiKeys:destroy'],
});
}
async load() {
await this.db.import({
directory: resolve(__dirname, '../collections'),
});
this.app.resourcer.use(async (ctx, next) => {
const { resourceName, actionName } = ctx.action.params;
if (resourceName == this.resourceName && ['list', 'destroy'].includes(actionName)) {
ctx.action.mergeParams({
filter: {
createdById: ctx.auth.user.id,
},
});
}
await next();
});
}
}