ops 代理路由直接使用 entitlement token 作为 Bearer 鉴权
This commit is contained in:
@@ -3,14 +3,19 @@ import { applyAuthResponseCookies, buildBackendRequestHeaders } from "@/lib/back
|
|||||||
import { buildProxyExceptionResponse } from "@/lib/api-proxy";
|
import { buildProxyExceptionResponse } from "@/lib/api-proxy";
|
||||||
|
|
||||||
const API_BASE = process.env.POLYWEATHER_API_BASE_URL;
|
const API_BASE = process.env.POLYWEATHER_API_BASE_URL;
|
||||||
|
const ENTITLEMENT_TOKEN = process.env.POLYWEATHER_BACKEND_ENTITLEMENT_TOKEN?.trim() || "";
|
||||||
|
|
||||||
export async function POST(req: NextRequest) {
|
export async function POST(req: NextRequest) {
|
||||||
if (!API_BASE) return NextResponse.json({ error: "API_BASE not configured" }, { status: 500 });
|
if (!API_BASE) return NextResponse.json({ error: "API_BASE not configured" }, { status: 500 });
|
||||||
try {
|
try {
|
||||||
const auth = await buildBackendRequestHeaders(req);
|
const auth = await buildBackendRequestHeaders(req);
|
||||||
const body = await req.text();
|
const body = await req.text();
|
||||||
|
const headers: Record<string, string> = { ...auth.headers as Record<string, string>, "Content-Type": "application/json" };
|
||||||
|
if (ENTITLEMENT_TOKEN) {
|
||||||
|
headers.Authorization = `Bearer ${ENTITLEMENT_TOKEN}`;
|
||||||
|
}
|
||||||
const res = await fetch(`${API_BASE}/api/ops/subscriptions/extend`, {
|
const res = await fetch(`${API_BASE}/api/ops/subscriptions/extend`, {
|
||||||
method: "POST", headers: { ...auth.headers, "Content-Type": "application/json" }, body, cache: "no-store",
|
method: "POST", headers, body, cache: "no-store",
|
||||||
});
|
});
|
||||||
const raw = await res.text();
|
const raw = await res.text();
|
||||||
const response = new NextResponse(raw, { status: res.status, headers: { "Content-Type": "application/json", "Cache-Control": "no-store" } });
|
const response = new NextResponse(raw, { status: res.status, headers: { "Content-Type": "application/json", "Cache-Control": "no-store" } });
|
||||||
|
|||||||
@@ -1,16 +1,22 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { applyAuthResponseCookies, buildBackendRequestHeaders } from "@/lib/backend-auth";
|
import { applyAuthResponseCookies, buildBackendRequestHeaders, BACKEND_ENTITLEMENT_HEADER } from "@/lib/backend-auth";
|
||||||
import { buildProxyExceptionResponse } from "@/lib/api-proxy";
|
import { buildProxyExceptionResponse } from "@/lib/api-proxy";
|
||||||
|
|
||||||
const API_BASE = process.env.POLYWEATHER_API_BASE_URL;
|
const API_BASE = process.env.POLYWEATHER_API_BASE_URL;
|
||||||
|
const ENTITLEMENT_TOKEN = process.env.POLYWEATHER_BACKEND_ENTITLEMENT_TOKEN?.trim() || "";
|
||||||
|
|
||||||
export async function POST(req: NextRequest) {
|
export async function POST(req: NextRequest) {
|
||||||
if (!API_BASE) return NextResponse.json({ error: "API_BASE not configured" }, { status: 500 });
|
if (!API_BASE) return NextResponse.json({ error: "API_BASE not configured" }, { status: 500 });
|
||||||
try {
|
try {
|
||||||
const auth = await buildBackendRequestHeaders(req);
|
const auth = await buildBackendRequestHeaders(req);
|
||||||
const body = await req.text();
|
const body = await req.text();
|
||||||
|
const headers: Record<string, string> = { ...auth.headers as Record<string, string>, "Content-Type": "application/json" };
|
||||||
|
// Ops endpoints: pass entitlement token as Bearer for robust admin auth.
|
||||||
|
if (ENTITLEMENT_TOKEN) {
|
||||||
|
headers.Authorization = `Bearer ${ENTITLEMENT_TOKEN}`;
|
||||||
|
}
|
||||||
const res = await fetch(`${API_BASE}/api/ops/subscriptions/grant`, {
|
const res = await fetch(`${API_BASE}/api/ops/subscriptions/grant`, {
|
||||||
method: "POST", headers: { ...auth.headers, "Content-Type": "application/json" }, body, cache: "no-store",
|
method: "POST", headers, body, cache: "no-store",
|
||||||
});
|
});
|
||||||
const raw = await res.text();
|
const raw = await res.text();
|
||||||
const response = new NextResponse(raw, { status: res.status, headers: { "Content-Type": "application/json", "Cache-Control": "no-store" } });
|
const response = new NextResponse(raw, { status: res.status, headers: { "Content-Type": "application/json", "Cache-Control": "no-store" } });
|
||||||
|
|||||||
Reference in New Issue
Block a user