oauthUnauthorizedRequest.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import {
  2. BIGMODEL_PROVIDER_ID,
  3. ZAI_PROVIDER_ID,
  4. buildBigModelApiUrl,
  5. buildRuntimeZaiBusinessUrl,
  6. } from "@zcode/shared";
  7. import type { ICredentialService } from "#src/credential/credential.js";
  8. import { resolveBigModelUserinfoUrl } from "#src/oauth/providers/bigmodelProviderConfig.js";
  9. import { resolveZaiUserinfoUrl } from "#src/oauth/providers/zaiProviderConfig.js";
  10. function tryResolveHttpUrl(resolve: () => string | URL): URL | null {
  11. try {
  12. const url = new URL(resolve());
  13. return url.protocol === "http:" || url.protocol === "https:" ? url : null;
  14. } catch {
  15. return null;
  16. }
  17. }
  18. const OAUTH_BUSINESS_PATHS = [
  19. "/api/biz/customer/getCustomerInfo",
  20. "/api/biz/subscription/enterprise/v2/pricing",
  21. "/api/biz/team/subscribe/product/querySubscribeDetail",
  22. ] as const;
  23. async function isCurrentBusinessOAuthCredentialRequest(options: {
  24. input: string | URL;
  25. headers: Headers;
  26. credentialService: Pick<ICredentialService, "load">;
  27. env?: NodeJS.ProcessEnv;
  28. }): Promise<boolean> {
  29. const authorization = options.headers.get("authorization")?.trim() ?? "";
  30. if (!authorization) return false;
  31. const provider = await options.credentialService.load("oauth:active_provider");
  32. if (!(await isOAuthBusinessRequest({ ...options, provider }))) return false;
  33. const accessToken = (
  34. await options.credentialService.load(`oauth:${provider}:access_token`)
  35. )?.trim();
  36. if (!accessToken || (authorization !== accessToken && authorization !== `Bearer ${accessToken}`))
  37. return false;
  38. // 读取磁盘期间可能切换平台,不能拿上一平台残留 token 的 401 清理当前登录。
  39. return (await options.credentialService.load("oauth:active_provider")) === provider;
  40. }
  41. export async function isOAuthBusinessRequest(options: {
  42. input: string | URL;
  43. credentialService: Pick<ICredentialService, "load">;
  44. env?: NodeJS.ProcessEnv;
  45. provider?: string | null;
  46. }): Promise<boolean> {
  47. const provider =
  48. options.provider ?? (await options.credentialService.load("oauth:active_provider"));
  49. if (provider !== BIGMODEL_PROVIDER_ID && provider !== ZAI_PROVIDER_ID) return false;
  50. const env = options.env ?? process.env;
  51. const requestUrl = tryResolveHttpUrl(() => options.input);
  52. if (!requestUrl) return false;
  53. const urls =
  54. provider === BIGMODEL_PROVIDER_ID
  55. ? [
  56. ...OAUTH_BUSINESS_PATHS.map((path) => () => buildBigModelApiUrl(env, path)),
  57. () => resolveBigModelUserinfoUrl(env),
  58. ]
  59. : [
  60. ...OAUTH_BUSINESS_PATHS.map((path) => () => buildRuntimeZaiBusinessUrl(env, path)),
  61. () => resolveZaiUserinfoUrl(env),
  62. ];
  63. return urls.some((resolve) => {
  64. const expected = tryResolveHttpUrl(resolve);
  65. return (
  66. expected !== null &&
  67. requestUrl.origin === expected.origin &&
  68. requestUrl.pathname === expected.pathname
  69. );
  70. });
  71. }
  72. export async function isCurrentOAuthBusinessCredentialRequest(options: {
  73. input: string | URL;
  74. headers: Headers;
  75. credentialService: Pick<ICredentialService, "load">;
  76. env?: NodeJS.ProcessEnv;
  77. }): Promise<boolean> {
  78. return isCurrentBusinessOAuthCredentialRequest(options);
  79. }
  80. // 这里只判定候选请求;实际退出须由 OAuthService 在会话变更队列内复核,不能依赖异步旧快照。
  81. export async function isCurrentOAuthCredentialRequest(options: {
  82. input: string | URL;
  83. headers: Headers;
  84. credentialService: Pick<ICredentialService, "load">;
  85. env?: NodeJS.ProcessEnv;
  86. }): Promise<boolean> {
  87. const authorization = options.headers.get("authorization")?.trim() ?? "";
  88. if (!authorization) return false;
  89. const currentJwt = (await options.credentialService.load("zcodejwttoken"))?.trim() ?? "";
  90. if (currentJwt && authorization === `Bearer ${currentJwt}`) return true;
  91. return isCurrentBusinessOAuthCredentialRequest(options);
  92. }