This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.ts
45 lines (40 loc) · 1.78 KB
/
route.ts
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
43
44
45
import { NextRequest, NextResponse } from "next/server";
import { FhirProxy } from "@/integrations/fhir/FhirProxy";
import { logRequest, logResponse } from "@/utils/loggerUtils";
import { FhirSession } from "@/auth/fhir/FhirSession";
import { FhirAuthError } from "@/auth/fhir/FhirAuthError";
import { JwtVerificationInput } from "@/auth/fhir/JwtVerificationInput";
const authErrorResponse = (text: string): Response => {
return new NextResponse(text, {status: 401});
}
const proxyHandler = async (request: NextRequest): Promise<Response> => {
const authHeader = request.headers.get("Authorization")
if(authHeader !== null) {
try {
// This verifies that incoming JWT is issued by a valid issuer and has a hpr-nummer claim
await FhirSession.fromVerifiedJWT(JwtVerificationInput.fromAuthorizationHeader(authHeader))
} catch (verifErr) {
if(verifErr instanceof FhirAuthError) {
return authErrorResponse(verifErr.message)
} else {
throw verifErr
}
}
} else {
authErrorResponse(`No Authorization header value in request. Cannot validate session`)
}
const proxy = FhirProxy.initDipsProxy();
return await proxy.forwardRequest(request)
}
const proxyHandlerWithRequestLogging = async (request: NextRequest): Promise<Response> => {
logRequest(request)
const resp = await proxyHandler(request)
logResponse(request.nextUrl, resp)
return resp
}
export const GET = proxyHandlerWithRequestLogging;
export const POST = proxyHandlerWithRequestLogging;
export const PUT = proxyHandlerWithRequestLogging;
export const PATCH = proxyHandlerWithRequestLogging;
export const DELETE = proxyHandlerWithRequestLogging;
export const HEAD = proxyHandlerWithRequestLogging;