fix: 支持通过出站代理校验谷歌凭据
This commit is contained in:
@@ -257,7 +257,7 @@ curl http://localhost:3500/health
|
|||||||
|
|
||||||
## 数字人 Google 登录
|
## 数字人 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 访问受保护接口。
|
响应返回业务 JWT 及用户信息。旧密码和游客会话无法访问数字人 C 端 API。Android 和 Web 使用同一个 Firebase 项目与同一环境的 API 时,同一 Firebase UID 对应同一个业务用户。Web 通过 BFF 将业务 JWT 写入 HttpOnly Cookie,Android 可用业务 JWT 访问受保护接口。
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,7 @@
|
|||||||
"dotenv": "^17.4.2",
|
"dotenv": "^17.4.2",
|
||||||
"fastify": "^5.8.5",
|
"fastify": "^5.8.5",
|
||||||
"firebase-admin": "^14.5.1",
|
"firebase-admin": "^14.5.1",
|
||||||
|
"https-proxy-agent": "^7.0.6",
|
||||||
"mysql2": "^3.22.3",
|
"mysql2": "^3.22.3",
|
||||||
"zod": "^4.4.3"
|
"zod": "^4.4.3"
|
||||||
},
|
},
|
||||||
|
|||||||
Generated
+3
@@ -20,6 +20,9 @@ importers:
|
|||||||
firebase-admin:
|
firebase-admin:
|
||||||
specifier: ^14.5.1
|
specifier: ^14.5.1
|
||||||
version: 14.5.1
|
version: 14.5.1
|
||||||
|
https-proxy-agent:
|
||||||
|
specifier: ^7.0.6
|
||||||
|
version: 7.0.6
|
||||||
mysql2:
|
mysql2:
|
||||||
specifier: ^3.22.3
|
specifier: ^3.22.3
|
||||||
version: 3.22.3(@types/node@25.9.1)
|
version: 3.22.3(@types/node@25.9.1)
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ const envSchema = z.object({
|
|||||||
.regex(/^[a-z][a-z0-9_-]{1,63}$/)
|
.regex(/^[a-z][a-z0-9_-]{1,63}$/)
|
||||||
.default("ui-new-old"),
|
.default("ui-new-old"),
|
||||||
DEEPSEEK_API_KEY: z.string().min(1).optional(),
|
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(),
|
FIREBASE_PROJECT_ID: z.string().regex(/^[a-z][a-z0-9-]{4,29}$/).optional(),
|
||||||
JWT_SECRET: z.string().min(32),
|
JWT_SECRET: z.string().min(32),
|
||||||
JWT_EXPIRES_IN: z.string().min(1).default("2h"),
|
JWT_EXPIRES_IN: z.string().min(1).default("2h"),
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
/** 验证指定 Firebase 项目签发的 Google 登录身份。 */
|
/** 验证指定 Firebase 项目签发的 Google 登录身份。 */
|
||||||
import { getApps, initializeApp } from "firebase-admin/app";
|
import { getApps, initializeApp } from "firebase-admin/app";
|
||||||
|
import { HttpsProxyAgent } from "https-proxy-agent";
|
||||||
import { getAuth } from "firebase-admin/auth";
|
import { getAuth } from "firebase-admin/auth";
|
||||||
import { env } from "../../config/env";
|
import { env } from "../../config/env";
|
||||||
import { HttpError, unauthorized } from "../../shared/http-error";
|
import { HttpError, unauthorized } from "../../shared/http-error";
|
||||||
@@ -11,11 +12,19 @@ export async function verifyFirebaseGoogleIdentity(idToken: string) {
|
|||||||
throw new HttpError(503, "FIREBASE_UNAVAILABLE", "Google 登录暂不可用");
|
throw new HttpError(503, "FIREBASE_UNAVAILABLE", "Google 登录暂不可用");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const proxyUrl = env.FIREBASE_AUTH_HTTPS_PROXY;
|
||||||
const appName = "digital-google-auth";
|
const appName = "digital-google-auth";
|
||||||
const app = getApps().find(({ name }) => name === appName)
|
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 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") {
|
if (code === "auth/argument-error" || code === "auth/invalid-id-token" || code === "auth/id-token-expired") {
|
||||||
throw unauthorized("Google 登录凭据无效或已过期,请重新登录");
|
throw unauthorized("Google 登录凭据无效或已过期,请重新登录");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user