From ad0d2e717f0cd409530735ab7143c94d910f939e Mon Sep 17 00:00:00 2001 From: xion Date: Thu, 3 Apr 2025 13:11:12 +0800 Subject: [PATCH] temp add delete all keys --- package.json | 2 +- src/oauth/oauth.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 5568c26..4326588 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/code-center-module", - "version": "0.0.17", + "version": "0.0.18", "description": "", "main": "dist/system.mjs", "module": "dist/system.mjs", diff --git a/src/oauth/oauth.ts b/src/oauth/oauth.ts index 68d5816..5af698a 100644 --- a/src/oauth/oauth.ts +++ b/src/oauth/oauth.ts @@ -70,6 +70,7 @@ interface Store { delObject: (value?: T) => Promise; keys: (key?: string) => Promise; setToken: (value: { accessToken: string; refreshToken: string; value?: T }, opts?: StoreSetOpts) => Promise; + delKeys: (keys: string[]) => Promise; } export class RedisTokenStore implements Store { private redis: Redis; @@ -168,6 +169,11 @@ export class RedisTokenStore implements Store { await this.set(userPrefix + ':refreshToken:' + refreshToken, refreshToken, refreshTokenExpire); } } + async delKeys(keys: string[]) { + const prefix = this.prefix; + const number = await this.redis.del(keys.map((key) => prefix + key)); + return number; + } } export class OAuth { @@ -285,4 +291,39 @@ export class OAuth { } this.store.delObject(user); } + + /** + * 获取某一个用户的所有token + * @param userId + * @returns + */ + async getUserTokens(userId: string, orgId?: string) { + const userPrefix = orgId ? `org:${orgId}:user:${userId}` : `user:${userId}`; + const tokens = await this.store.keys(`${userPrefix}:token:*`); + return tokens; + } + /** + * 过期某一个用户的所有token + * @param userId + * @param orgId + */ + async expireUserTokens(userId: string, orgId?: string) { + const userPrefix = orgId ? `org:${orgId}:user:${userId}` : `user:${userId}`; + const tokens = await this.store.keys(`${userPrefix}:token:*`); + for (const token of tokens) { + try { + const user = await this.store.getObject(token); + this.store.delObject(user); + } catch (error) { + console.error('expireUserTokens error', userId, orgId, error); + } + } + } + /** + * 过期所有用户的token, 然后重启服务 + */ + async expireAllTokens() { + const tokens = await this.store.keys('*'); + await this.store.delKeys(tokens); + } }