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

118 lines
4.9 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.

import assert from 'assert';
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
const triggers = [
{
name: "在撤销用户OAuth授权前执行操作",
action: "revoke",
when: "after",
entity: "oauthUserAuthorization",
fn: async ({ operation }, context) => {
const { filter } = operation;
assert(filter, 'No filter found in revoke operation');
const datas = await context.select("oauthUserAuthorization", {
data: {
userId: 1,
applicationId: 1,
tokenId: 1,
codeId: 1,
usageState: 1,
},
filter: { ...filter },
}, {});
let res = 0;
for (const data of datas) {
// 如果是unused并且code的usedAt是空的则把code的usedAt全部设置为当前时间
const opRes0 = await context.operate("oauthAuthorizationCode", {
id: await generateNewIdAsync(),
action: "update",
data: {
usedAt: new Date()
},
filter: {
usedAt: {
$exists: false
},
// 某一个用户对某一个应用的授权记录
oauthAppId: data.applicationId,
userId: data.userId,
}
}, {});
res += opRes0.oauthAuthorizationCode?.update || 0;
// // 如果没有token可以直接删除oauthUserAuthorization (可能是复用的之前的token 也可能是未被使用的授权记录)
if (data.usageState === 'unused' && !data.tokenId) {
const opRes = await context.operate("oauthUserAuthorization", {
id: await generateNewIdAsync(),
action: "remove",
data: {},
filter: {
id: data.id,
}
}, {});
res += opRes.oauthApplication?.remove || 0;
}
// 如果有token则将token的revokedAt设置为当前时间
if (data.tokenId) {
const opRes2 = await context.operate("oauthToken", {
id: await generateNewIdAsync(),
action: "update",
data: {
revokedAt: new Date()
},
filter: {
id: data.tokenId
}
}, {});
res += opRes2.oauthToken?.update || 0;
}
}
// // 如果是unused并且code的usedAt是空的则把code的usedAt全部设置为当前时间
// const opRes0 = await context.operate("oauthAuthorizationCode", {
// id: await 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 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 generateNewIdAsync(),
// action: "update",
// data: {
// revokedAt: new Date()
// },
// filter: {
// oauthUserAuthorization$token: {
// ...filter
// }
// }
// }, {});
// res += opRes2.oauthToken?.update || 0;
return res;
}
}
];
export default triggers;