From 320c7cb48cf56f86ae0de35be2a8266fd2590573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=9B=E5=85=AE?= Date: Fri, 13 Nov 2026 22:10:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=94=AF=E6=8C=81=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E5=87=BA=E7=AB=99=E4=BB=A3=E7=90=86=E6=A0=A1=E9=AA=8C=E8=B0=B7?= =?UTF-8?q?=E6=AD=8C=E5=87=AD=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- package.json | 1 + pnpm-lock.yaml | 3 +++ src/config/env.ts | 1 + src/modules/auth/firebase-google.ts | 13 +++++++++++-- 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 676a976..53fc553 100644 --- a/README.md +++ b/README.md @@ -257,7 +257,7 @@ curl http://localhost:3500/health ## 数字人 Google 登录 -运行数据库迁移 `021_digital_firebase_identity.sql`,并在服务端配置 `FIREBASE_PROJECT_ID=personal-digital-human`。接口 `POST /api/auth/digital/firebase` 接受 JSON `{ "idToken": "..." }`。Admin SDK 验证目标项目签发的 Firebase ID token 与 Google 登录提供方。首次登录创建 C 端账号,后续按项目与区分大小写的 UID 查找原账号,不根据邮箱合并存量账号。 +运行数据库迁移 `021_digital_firebase_identity.sql`,并在服务端配置 `FIREBASE_PROJECT_ID=personal-digital-human`。Firebase Admin SDK 需要访问 Google 公钥端点。可通过 `FIREBASE_AUTH_HTTPS_PROXY` 为 Firebase 验签配置 HTTP CONNECT 出站代理。接口 `POST /api/auth/digital/firebase` 接受 JSON `{ "idToken": "..." }`。Admin SDK 验证目标项目签发的 Firebase ID token 与 Google 登录提供方。首次登录创建 C 端账号,后续按项目与区分大小写的 UID 查找原账号,不根据邮箱合并存量账号。 响应返回业务 JWT 及用户信息。旧密码和游客会话无法访问数字人 C 端 API。Android 和 Web 使用同一个 Firebase 项目与同一环境的 API 时,同一 Firebase UID 对应同一个业务用户。Web 通过 BFF 将业务 JWT 写入 HttpOnly Cookie,Android 可用业务 JWT 访问受保护接口。 diff --git a/package.json b/package.json index 7613ff1..3b88f07 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "dotenv": "^17.4.2", "fastify": "^5.8.5", "firebase-admin": "^14.5.1", + "https-proxy-agent": "^7.0.6", "mysql2": "^3.22.3", "zod": "^4.4.3" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 628ab46..ad19b45 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -20,6 +20,9 @@ importers: firebase-admin: specifier: ^14.5.1 version: 14.5.1 + https-proxy-agent: + specifier: ^7.0.6 + version: 7.0.6 mysql2: specifier: ^3.22.3 version: 3.22.3(@types/node@25.9.1) diff --git a/src/config/env.ts b/src/config/env.ts index 18529fb..c52f913 100644 --- a/src/config/env.ts +++ b/src/config/env.ts @@ -27,6 +27,7 @@ const envSchema = z.object({ .regex(/^[a-z][a-z0-9_-]{1,63}$/) .default("ui-new-old"), DEEPSEEK_API_KEY: z.string().min(1).optional(), + FIREBASE_AUTH_HTTPS_PROXY: z.url({ protocol: /^https?$/ }).optional(), FIREBASE_PROJECT_ID: z.string().regex(/^[a-z][a-z0-9-]{4,29}$/).optional(), JWT_SECRET: z.string().min(32), JWT_EXPIRES_IN: z.string().min(1).default("2h"), diff --git a/src/modules/auth/firebase-google.ts b/src/modules/auth/firebase-google.ts index 18f27dd..e26133b 100644 --- a/src/modules/auth/firebase-google.ts +++ b/src/modules/auth/firebase-google.ts @@ -1,5 +1,6 @@ /** 验证指定 Firebase 项目签发的 Google 登录身份。 */ import { getApps, initializeApp } from "firebase-admin/app"; +import { HttpsProxyAgent } from "https-proxy-agent"; import { getAuth } from "firebase-admin/auth"; import { env } from "../../config/env"; import { HttpError, unauthorized } from "../../shared/http-error"; @@ -11,11 +12,19 @@ export async function verifyFirebaseGoogleIdentity(idToken: string) { throw new HttpError(503, "FIREBASE_UNAVAILABLE", "Google 登录暂不可用"); } + const proxyUrl = env.FIREBASE_AUTH_HTTPS_PROXY; const appName = "digital-google-auth"; const app = getApps().find(({ name }) => name === appName) - ?? initializeApp({ projectId }, appName); + ?? initializeApp({ + projectId, + httpAgent: proxyUrl ? new HttpsProxyAgent(proxyUrl) : undefined, + }, appName); const claims = await getAuth(app).verifyIdToken(idToken).catch((error: unknown) => { - const code = (error as { code?: string }).code; + const { code, message } = error as { code?: string; message?: string }; + // Admin SDK 也会把公钥获取失败包装成 auth/argument-error。 + if (message && /error fetching public keys|error in secret or public key callback|ECONN|ETIMEDOUT|EAI_AGAIN|ENOTFOUND/i.test(message)) { + throw new HttpError(503, "FIREBASE_UNAVAILABLE", "Google 登录暂不可用"); + } if (code === "auth/argument-error" || code === "auth/invalid-id-token" || code === "auth/id-token-expired") { throw unauthorized("Google 登录凭据无效或已过期,请重新登录"); }