Files
leaudit-platform-frontend/app/routes/api.oauth.token.tsx
T

56 lines
2.5 KiB
TypeScript

import { type ActionFunctionArgs, json } from "@remix-run/node";
import { OAuthClient } from "~/api/login/oauth-client";
import { OAUTH_CONFIG } from "~/config/api-config";
import { validateRequest, logSecurityEvent } from "~/middleware/host-validation";
/**
* 这个Action作为获取OAuth Access Token的服务器端代理。
* 它接收来自前端回调的`code`,然后在后端安全地换取令牌,
* 以避免在网络策略限制服务器直接访问外部服务时出现问题。
*/
export async function action({ request }: ActionFunctionArgs) {
// 1. Host头验证
const hostValidation = validateRequest(request);
if (!hostValidation.valid) {
logSecurityEvent('host_validation_failed', hostValidation.error || 'Unknown validation error', request);
console.error('❌ OAuth Token API Host验证失败:', hostValidation.error);
return json({ success: false, error: "Forbidden: Invalid Host header" }, { status: 403 });
}
// 2. 只允许POST请求
if (request.method !== "POST") {
return json({ success: false, error: "Method Not Allowed" }, { status: 405 });
}
try {
// 2. 从请求体中获取`code`
const { code } = await request.json();
if (!code || typeof code !== 'string') {
return json({ success: false, error: "Missing or invalid 'code' in request body" }, { status: 400 });
}
console.log("🔧 [/api/oauth/token] 收到代理请求, code:", code ? `${code.substring(0, 10)}...` : null);
// 3. 在服务器端使用OAuthClient安全地获取访问令牌
const oauthClient = new OAuthClient(OAUTH_CONFIG);
const tokenResponse = await oauthClient.getAccessToken(code);
// 4. 处理来自IDaaS服务器的响应
if (!tokenResponse) {
console.error("❌ [/api/oauth/token] 从IDaaS获取访问令牌失败。");
// 502 Bad Gateway 表示上游服务器(IDaaS)响应无效
return json({ success: false, error: "Failed to get access token from IDaaS" }, { status: 502 });
}
console.log("✅ [/api/oauth/token] 已通过代理成功获取令牌。");
// 5. 将成功的令牌响应返回给调用方 (callback.tsx)
return json({ success: true, ...tokenResponse });
} catch (error) {
console.error("❌ [/api/oauth/token] 代理API发生意外错误:", error);
const errorMessage = error instanceof Error ? error.message : "An unknown error occurred.";
return json({ success: false, error: "Proxy internal error", details: errorMessage }, { status: 500 });
}
}