44 lines
1.5 KiB
JavaScript
44 lines
1.5 KiB
JavaScript
import assert from 'assert';
|
||
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
|
||
import { randomUUID } from 'crypto';
|
||
const triggers = [
|
||
{
|
||
name: "创建oauth app时,填充数据",
|
||
action: "create",
|
||
when: "before",
|
||
entity: "oauthApplication",
|
||
fn: async ({ operation }, context) => {
|
||
assert(operation.data && !Array.isArray(operation.data), "oauthApplication create data 必须存在且为单条记录");
|
||
const { data } = operation;
|
||
const systemId = context.getSystemId();
|
||
data.systemId = systemId;
|
||
data.clientSecret = randomUUID();
|
||
// 默认不强制 PKCE
|
||
data.requirePKCE = data.requirePKCE ?? false;
|
||
return 0; // 没有引起数据库行修改
|
||
}
|
||
},
|
||
{
|
||
name: "更新apps的secret",
|
||
action: "resetSecret",
|
||
when: "before",
|
||
entity: "oauthApplication",
|
||
fn: async ({ operation }, context) => {
|
||
const { filter } = operation;
|
||
assert(filter && filter.id, "resetSecret 操作必须指定 filter.id");
|
||
const opRes = await context.operate("oauthApplication", {
|
||
id: await generateNewIdAsync(),
|
||
action: "update",
|
||
data: {
|
||
clientSecret: randomUUID(),
|
||
},
|
||
filter: {
|
||
id: filter.id,
|
||
}
|
||
}, {});
|
||
return opRes;
|
||
}
|
||
},
|
||
];
|
||
export default triggers;
|