-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Update middleware to protect additional paths and use API key …
…for authorization Signed-off-by: Ilayda Cansin Koc <ilaydacansin@gmail.com>
- Loading branch information
1 parent
e8fb483
commit df7e7e7
Showing
1 changed file
with
10 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,20 @@ | ||
import { NextResponse } from "next/server"; | ||
import type { NextRequest } from "next/server"; | ||
|
||
const excludePath = [ | ||
"/api/dynamic/presentCredentialById", | ||
"/api/dynamic/clientMetadataById", | ||
"/api/presentCredential", | ||
"/api/clientMetadata", | ||
const protectedPaths = [ | ||
"/api/dynamic/createTempAuthorization", | ||
"/api/dynamic/getAuthResponse", | ||
"/api/dynamic/getQRCodeString", | ||
]; | ||
|
||
export function middleware(req: NextRequest, res: NextResponse) { | ||
export function middleware(req: NextRequest) { | ||
const authHeader = req.headers.get("Authorization"); | ||
const oidcClientId = authHeader?.split(" ")[1]; | ||
|
||
if (excludePath.includes(req.nextUrl.pathname)) { | ||
return NextResponse.next(); | ||
} | ||
|
||
if (oidcClientId && oidcClientId === process.env.OIDC_CLIENT_ID) { | ||
const path = req.nextUrl.pathname; | ||
const apiKey = authHeader?.split(" ")[1]; | ||
if (protectedPaths.includes(path) && apiKey === process.env.API_KEY) { | ||
return NextResponse.next(); | ||
} else { | ||
} else if (protectedPaths.includes(path) && apiKey !== process.env.API_KEY) { | ||
return new Response("Unauthorized", { status: 401 }); | ||
} | ||
return NextResponse.next(); | ||
} |