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

66 lines
2.6 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;
// 如果是unused并且code的usedAt是空的则把code的usedAt全部设置为当前时间
const opRes0 = await context.operate("oauthAuthorizationCode", {
id: await (0, uuid_1.generateNewIdAsync)(),
action: "update",
data: {
usedAt: new Date()
},
filter: {
usedAt: {
$exists: false
},
oauthUserAuthorization$code: {
...filter,
// 未被使用肯定就没有tokenId
usageState: 'unused',
}
}
}, {});
res += opRes0.oauthAuthorizationCode?.update || 0;
// 如果没有token可以直接删除oauthUserAuthorization (可能是复用的之前的token 也可能是未被使用的授权记录)
const opRes = await context.operate("oauthUserAuthorization", {
id: await (0, uuid_1.generateNewIdAsync)(),
action: "remove",
data: {},
filter: {
...filter,
// 未被使用肯定就没有tokenId
usageState: 'unused',
}
}, {});
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;