oak-general-business/lib/triggers/oauthUserAuth.js

48 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const assert_1 = tslib_1.__importDefault(require("assert"));
const uuid_1 = require("oak-domain/lib/utils/uuid");
const triggers = [
{
name: "在撤销用户OAuth授权前执行操作",
action: "revoke",
when: "after",
entity: "oauthUserAuthorization",
fn: async ({ operation }, context) => {
const { filter } = operation;
(0, assert_1.default)(filter, 'No filter found in revoke operation');
let res = 0;
// 如果没有token可以直接删除oauthUserAuthorization
const opRes = await context.operate("oauthUserAuthorization", {
id: await (0, uuid_1.generateNewIdAsync)(),
action: "remove",
data: {},
filter: {
...filter,
tokenId: {
$exists: false
}
}
}, {});
res += opRes.oauthApplication?.remove || 0;
// 如果有token则将token的revokedAt设置为当前时间
const opRes2 = await context.operate("oauthToken", {
id: await (0, uuid_1.generateNewIdAsync)(),
action: "update",
data: {
revokedAt: new Date()
},
filter: {
oauthUserAuthorization$token: {
...filter
}
}
}, {});
res += opRes2.oauthToken?.update || 0;
return res;
}
}
];
exports.default = triggers;