-
Notifications
You must be signed in to change notification settings - Fork 464
Expand file tree
/
Copy pathproxy.ts
More file actions
42 lines (37 loc) · 1.19 KB
/
Copy pathproxy.ts
File metadata and controls
42 lines (37 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { NextResponse, type NextRequest } from "next/server";
import {
isApiRequestAllowed,
isApiRequestHostAllowed,
} from "@/lib/request-security";
import {
isValidBasicAuthorization,
isWebPasswordEnabled,
} from "@/lib/web-auth";
export function proxy(request: NextRequest) {
const isApiRequest = request.nextUrl.pathname === "/api"
|| request.nextUrl.pathname.startsWith("/api/");
const isTrustedRequest = isApiRequest
? isApiRequestAllowed(request)
: isApiRequestHostAllowed(request);
if (!isTrustedRequest) {
if (!isApiRequest) {
return new NextResponse("Untrusted request", { status: 403 });
}
return NextResponse.json({ error: "Untrusted API request" }, { status: 403 });
}
const password = process.env.PI_WEB_PASSWORD;
if (
isWebPasswordEnabled(password)
&& !isValidBasicAuthorization(request.headers.get("authorization"), password)
) {
return new NextResponse("Authentication required", {
status: 401,
headers: {
"Cache-Control": "no-store",
"WWW-Authenticate": 'Basic realm="Pi Web", charset="UTF-8"',
},
});
}
return NextResponse.next();
}
export const config = { matcher: ["/", "/api/:path*"] };