feat: 修复revoke端口相关逻辑

This commit is contained in:
Pan Qiancheng 2025-10-24 10:49:33 +08:00
parent 841faface2
commit fb8fae0525
8 changed files with 64 additions and 33 deletions

View File

@ -328,7 +328,7 @@ export async function authorize(params, context) {
oauthAppId: oauthApp.id,
applicationId: context.getApplicationId(),
userId: context.getCurrentUserId(),
scope: [scope || ""],
scope: scope === undefined ? [] : [scope],
expiresAt: Date.now() + 10 * 60 * 1000, // 10分钟后过期
}
}, {});

View File

@ -136,7 +136,7 @@ export default OakComponent({
}).then((result) => {
const { redirectUri } = result.result;
assert(redirectUri, 'redirectUri should be present in authorize result');
window.location.href = redirectUri;
window.location.replace(redirectUri);
}).catch((err) => {
console.error('Error during OAuth authorization:', err);
this.setState({

View File

@ -458,17 +458,26 @@ const oauthRevocationEndpoint = {
}
// 4. 撤销操作(无论找到与否,都返回 200但如果找到则执行失效操作
if (tokenRecord) {
const pastTime = Date.now() - 1000;
// 将 Access Token 和 Refresh Token 的过期时间都设为过去,使其立即失效
await context.operate("oauthToken", {
// const pastTime = Date.now() - 1000;
// // 将 Access Token 和 Refresh Token 的过期时间都设为过去,使其立即失效
// await context.operate("oauthToken", {
// id: await generateNewIdAsync(),
// action: "update",
// data: {
// accessExpiresAt: pastTime,
// refreshExpiresAt: pastTime,
// },
// filter: {
// id: tokenRecord.id,
// }
// }, {});
// 使用这个token的认证记录都撤销掉在trigger里会自动设置 revokedAt
await context.operate("oauthUserAuthorization", {
id: await generateNewIdAsync(),
action: "update",
data: {
accessExpiresAt: pastTime,
refreshExpiresAt: pastTime,
},
action: "revoke",
data: {},
filter: {
id: tokenRecord.id,
tokenId: tokenRecord.id,
}
}, {});
}
@ -484,5 +493,6 @@ const endpoints = {
'oauth/access_token': oauthTokenEndpoint,
'oauth/userinfo': oauthUserInfoEndpoint,
'oauth/token': refreshTokenEndpoint,
'oauth/revoke': oauthRevocationEndpoint,
};
export default endpoints;

View File

@ -335,7 +335,7 @@ async function authorize(params, context) {
oauthAppId: oauthApp.id,
applicationId: context.getApplicationId(),
userId: context.getCurrentUserId(),
scope: [scope || ""],
scope: scope === undefined ? [] : [scope],
expiresAt: Date.now() + 10 * 60 * 1000, // 10分钟后过期
}
}, {});

View File

@ -461,17 +461,26 @@ const oauthRevocationEndpoint = {
}
// 4. 撤销操作(无论找到与否,都返回 200但如果找到则执行失效操作
if (tokenRecord) {
const pastTime = Date.now() - 1000;
// 将 Access Token 和 Refresh Token 的过期时间都设为过去,使其立即失效
await context.operate("oauthToken", {
// const pastTime = Date.now() - 1000;
// // 将 Access Token 和 Refresh Token 的过期时间都设为过去,使其立即失效
// await context.operate("oauthToken", {
// id: await generateNewIdAsync(),
// action: "update",
// data: {
// accessExpiresAt: pastTime,
// refreshExpiresAt: pastTime,
// },
// filter: {
// id: tokenRecord.id,
// }
// }, {});
// 使用这个token的认证记录都撤销掉在trigger里会自动设置 revokedAt
await context.operate("oauthUserAuthorization", {
id: await (0, uuid_1.generateNewIdAsync)(),
action: "update",
data: {
accessExpiresAt: pastTime,
refreshExpiresAt: pastTime,
},
action: "revoke",
data: {},
filter: {
id: tokenRecord.id,
tokenId: tokenRecord.id,
}
}, {});
}
@ -487,5 +496,6 @@ const endpoints = {
'oauth/access_token': oauthTokenEndpoint,
'oauth/userinfo': oauthUserInfoEndpoint,
'oauth/token': refreshTokenEndpoint,
'oauth/revoke': oauthRevocationEndpoint,
};
exports.default = endpoints;

View File

@ -405,7 +405,7 @@ export async function authorize<ED extends EntityDict>(params: {
oauthAppId: oauthApp.id,
applicationId: context.getApplicationId()!,
userId: context.getCurrentUserId()!,
scope: [scope || ""],
scope: scope === undefined ? [] : [scope],
expiresAt: Date.now() + 10 * 60 * 1000, // 10分钟后过期
}
}, {})

View File

@ -155,7 +155,7 @@ export default OakComponent({
}).then((result) => {
const { redirectUri } = result.result;
assert(redirectUri, 'redirectUri should be present in authorize result');
window.location.href = redirectUri;
window.location.replace(redirectUri);
}).catch((err: Error) => {
console.error('Error during OAuth authorization:', err);

View File

@ -499,7 +499,7 @@ const oauthRevocationEndpoint: Endpoint<EntityDict, BackendRuntimeContext<Entity
// 3. 查找令牌记录
let tokenRecord = null;
const tokenProjection = {
const tokenProjection: EntityDict['oauthToken']['Selection'] = {
data: { id: 1, code: { oauthAppId: 1 } },
filter: {}
};
@ -518,18 +518,28 @@ const oauthRevocationEndpoint: Endpoint<EntityDict, BackendRuntimeContext<Entity
// 4. 撤销操作(无论找到与否,都返回 200但如果找到则执行失效操作
if (tokenRecord) {
const pastTime = Date.now() - 1000;
// const pastTime = Date.now() - 1000;
// 将 Access Token 和 Refresh Token 的过期时间都设为过去,使其立即失效
await context.operate("oauthToken", {
// // 将 Access Token 和 Refresh Token 的过期时间都设为过去,使其立即失效
// await context.operate("oauthToken", {
// id: await generateNewIdAsync(),
// action: "update",
// data: {
// accessExpiresAt: pastTime,
// refreshExpiresAt: pastTime,
// },
// filter: {
// id: tokenRecord.id,
// }
// }, {});
// 使用这个token的认证记录都撤销掉在trigger里会自动设置 revokedAt
await context.operate("oauthUserAuthorization", {
id: await generateNewIdAsync(),
action: "update",
data: {
accessExpiresAt: pastTime,
refreshExpiresAt: pastTime,
},
action: "revoke",
data: {},
filter: {
id: tokenRecord.id,
tokenId: tokenRecord.id,
}
}, {});
}
@ -548,6 +558,7 @@ const endpoints: Record<string, Endpoint<EntityDict, BRC<EntityDict>>> = {
'oauth/access_token': oauthTokenEndpoint,
'oauth/userinfo': oauthUserInfoEndpoint,
'oauth/token': refreshTokenEndpoint,
'oauth/revoke': oauthRevocationEndpoint,
}
export default endpoints;