From 91ff16290889cc4564a6421aad2bab4ca6d9f853 Mon Sep 17 00:00:00 2001 From: qcqcqc <1220204124@zust.edu.cn> Date: Thu, 23 Oct 2025 15:30:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9A=E6=9C=9F=E5=88=B7=E6=96=B0?= =?UTF-8?q?=E8=A6=81=E8=BF=87=E6=9C=9F=E7=9A=84oauth=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/watchers/index.ts | 3 ++- src/watchers/oauth.ts | 53 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/watchers/oauth.ts diff --git a/src/watchers/index.ts b/src/watchers/index.ts index c259829ca..80320b191 100644 --- a/src/watchers/index.ts +++ b/src/watchers/index.ts @@ -3,8 +3,9 @@ import { Watcher } from 'oak-domain/lib/types'; import { BackendRuntimeContext } from '../context/BackendRuntimeContext'; import token from './token'; import extraFile from './extraFile'; +import oauth from './oauth'; -export default [...token, ...extraFile] as Watcher< +export default [...token, ...extraFile, ...oauth] as Watcher< EntityDict, keyof EntityDict, BackendRuntimeContext diff --git a/src/watchers/oauth.ts b/src/watchers/oauth.ts new file mode 100644 index 000000000..eecc934b4 --- /dev/null +++ b/src/watchers/oauth.ts @@ -0,0 +1,53 @@ +import { EntityDict } from '../oak-app-domain'; +import { BackendRuntimeContext } from '../context/BackendRuntimeContext'; +import { Watcher, BBWatcher } from 'oak-domain/lib/types/Watcher'; + +const watchers: Watcher< + EntityDict, + 'oauthUser', + BackendRuntimeContext +>[] = [ + { + name: '定期刷新过期的oauthUser令牌', + entity: 'oauthUser', + filter() { + const now = Date.now(); + const tenMinutes = 10 * 60 * 1000; // 10分钟 + const oneDay = 24 * 60 * 60 * 1000; // 1天 + + return { + $and: [ + { + refreshToken: { + $exists: true, + }, + }, + { + state: { + provider: { + refreshEndpoint: { + $exists: true, + } + } + } + }, + // accessToken在10分钟内过期(包括已过期) + { + accessExpiresAt: { + $lt: now + tenMinutes, + }, + }, + // refreshToken还没过期(至少还有1天有效期) + { + refreshExpiresAt: { + $gt: now + oneDay, + }, + }, + ], + }; + }, + action: 'refreshTokens', + actionData: {}, + }, + ]; +export default watchers;