token-manager.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * 飞书 Token 管理模块
  3. * 负责获取、保存、刷新 tenant_access_token
  4. */
  5. import { getFeishuConfig, validateConfig } from './config';
  6. // 飞书 API 端点
  7. const FEISHU_API_BASE = 'https://open.feishu.cn/open-apis';
  8. const TOKEN_API = `${FEISHU_API_BASE}/auth/v3/tenant_access_token/internal`;
  9. // Token 信息接口
  10. interface TokenInfo {
  11. accessToken: string;
  12. expireTime: number; // 过期时间戳(毫秒)
  13. }
  14. // 飞书 Token API 响应
  15. interface FeishuTokenResponse {
  16. code: number;
  17. msg: string;
  18. tenant_access_token?: string;
  19. expire?: number; // 有效期(秒)
  20. }
  21. /**
  22. * Token 管理器类
  23. */
  24. export class TokenManager {
  25. private Parse: any;
  26. private static instance: TokenManager;
  27. constructor() {
  28. // 使用全局的 Parse 实例
  29. this.Parse = (globalThis as any).Parse;
  30. if (!this.Parse) {
  31. throw new Error('Parse is not initialized');
  32. }
  33. }
  34. /**
  35. * 获取单例实例
  36. */
  37. static getInstance(): TokenManager {
  38. if (!TokenManager.instance) {
  39. TokenManager.instance = new TokenManager();
  40. }
  41. return TokenManager.instance;
  42. }
  43. /**
  44. * 从飞书 API 获取新的 access token
  45. */
  46. private async fetchTokenFromFeishu(): Promise<TokenInfo> {
  47. const config = getFeishuConfig();
  48. if (!validateConfig(config)) {
  49. throw new Error('飞书配置无效:请设置 FEISHU_APP_ID 和 FEISHU_APP_SECRET 环境变量');
  50. }
  51. const response = await fetch(TOKEN_API, {
  52. method: 'POST',
  53. headers: {
  54. 'Content-Type': 'application/json',
  55. },
  56. body: JSON.stringify({
  57. app_id: config.app_id,
  58. app_secret: config.app_secret
  59. })
  60. });
  61. if (!response.ok) {
  62. throw new Error(`获取飞书 token 失败: ${response.status} ${response.statusText}`);
  63. }
  64. const data: FeishuTokenResponse = await response.json();
  65. if (data.code !== 0) {
  66. throw new Error(`获取飞书 token 失败: ${data.msg}`);
  67. }
  68. if (!data.tenant_access_token || !data.expire) {
  69. throw new Error('飞书 API 返回数据格式错误');
  70. }
  71. // 计算过期时间(提前 5 分钟过期,避免边界情况)
  72. const expireTime = Date.now() + (data.expire - 300) * 1000;
  73. return {
  74. accessToken: data.tenant_access_token,
  75. expireTime
  76. };
  77. }
  78. /**
  79. * 从数据库获取 Store 配置
  80. */
  81. private async getStoreConfig(): Promise<any> {
  82. try {
  83. const query = new this.Parse.Query('Store');
  84. query.equalTo('objectId', 'config');
  85. const store = await query.first({ useMasterKey: true });
  86. return store;
  87. } catch (error) {
  88. console.error('获取 Store 配置失败:', error);
  89. return null;
  90. }
  91. }
  92. /**
  93. * 从数据库读取 token
  94. */
  95. private async getTokenFromDatabase(): Promise<TokenInfo | null> {
  96. try {
  97. const store = await this.getStoreConfig();
  98. if (!store) {
  99. return null;
  100. }
  101. const config = store.get('config');
  102. if (!config || !config.feishu || !config.feishu.accessToken || !config.feishu.expireTime) {
  103. return null;
  104. }
  105. return {
  106. accessToken: config.feishu.accessToken,
  107. expireTime: config.feishu.expireTime
  108. };
  109. } catch (error) {
  110. console.error('从数据库读取 token 失败:', error);
  111. return null;
  112. }
  113. }
  114. /**
  115. * 保存 token 到数据库
  116. */
  117. private async saveTokenToDatabase(tokenInfo: TokenInfo): Promise<void> {
  118. try {
  119. let store = await this.getStoreConfig();
  120. if (!store) {
  121. // 创建新的 Store 记录
  122. const StoreClass = this.Parse.Object.extend('Store');
  123. store = new StoreClass();
  124. store.id = 'config';
  125. }
  126. // 获取现有的 config
  127. let config = store.get('config') || {};
  128. // 更新 feishu 配置
  129. config.feishu = {
  130. accessToken: tokenInfo.accessToken,
  131. expireTime: tokenInfo.expireTime
  132. };
  133. // 保存到数据库
  134. store.set('config', config);
  135. await store.save(null, { useMasterKey: true });
  136. console.log('Token 已保存到数据库');
  137. } catch (error) {
  138. console.error('保存 token 到数据库失败:', error);
  139. throw error;
  140. }
  141. }
  142. /**
  143. * 检查 token 是否过期
  144. */
  145. private isTokenExpired(tokenInfo: TokenInfo): boolean {
  146. return Date.now() >= tokenInfo.expireTime;
  147. }
  148. /**
  149. * 获取有效的 access token
  150. * 优先从数据库读取,如果不存在或已过期则重新获取
  151. */
  152. async getAccessToken(): Promise<string> {
  153. try {
  154. // 1. 尝试从数据库读取
  155. let tokenInfo = await this.getTokenFromDatabase();
  156. // 2. 检查是否存在且未过期
  157. if (tokenInfo && !this.isTokenExpired(tokenInfo)) {
  158. console.log('使用数据库中的 token');
  159. return tokenInfo.accessToken;
  160. }
  161. // 3. 从飞书 API 获取新 token
  162. console.log('从飞书 API 获取新 token');
  163. tokenInfo = await this.fetchTokenFromFeishu();
  164. // 4. 保存到数据库
  165. await this.saveTokenToDatabase(tokenInfo);
  166. return tokenInfo.accessToken;
  167. } catch (error) {
  168. console.error('获取 access token 失败:', error);
  169. throw error;
  170. }
  171. }
  172. /**
  173. * 强制刷新 token
  174. */
  175. async refreshToken(): Promise<string> {
  176. console.log('强制刷新 token');
  177. const tokenInfo = await this.fetchTokenFromFeishu();
  178. await this.saveTokenToDatabase(tokenInfo);
  179. return tokenInfo.accessToken;
  180. }
  181. }
  182. /**
  183. * 获取 Token 管理器实例
  184. */
  185. export function getTokenManager(): TokenManager {
  186. return TokenManager.getInstance();
  187. }