Skip to content

Commit

Permalink
Add http logging middleware
Browse files Browse the repository at this point in the history
Signed-off-by: Felix Hoops <9974641+jfelixh@users.noreply.github.com>
  • Loading branch information
jfelixh committed Aug 23, 2024
1 parent 8c575f1 commit 68cb0b6
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 46 deletions.
25 changes: 25 additions & 0 deletions vclogin/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2024 Software Engineering for Business Information Systems (sebis) <matthes@tum.de> .
* SPDX-License-Identifier: MIT
*/

import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";

export function middleware(req: NextRequest) {
// enforce authorization for sensitive endpoints needed for dynamic authorization
if (req.nextUrl.pathname.startsWith("/api/dynamic")) {
const authHeader = req.headers.get("Authorization");
const apiKey = authHeader?.split(" ")[1];
if (apiKey !== process.env.API_KEY) {
return new Response("Unauthorized", { status: 401 });
}
}
// would like to do logging here but because nextjs middleware runs in Edge Runtime
// it does not seem to like pino logger
return NextResponse.next();
}

export const config = {
matcher: "/(api/.*)",
};
25 changes: 0 additions & 25 deletions vclogin/middleware/protection.ts

This file was deleted.

7 changes: 3 additions & 4 deletions vclogin/pages/api/clientMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import { getMetadata } from "@/lib/getMetadata";
import type { NextApiRequest, NextApiResponse } from "next";
import { logger } from "@/config/logger";
import { withLogging } from "@/middleware/logging";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse<any>,
) {
async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
try {
const { method } = req;
if (method === "GET") {
Expand All @@ -28,3 +26,4 @@ export default async function handler(
}

export const config = { api: { bodyParser: false } };
export default withLogging(handler);
7 changes: 3 additions & 4 deletions vclogin/pages/api/dynamic/clientMetadataById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import { getMetadata } from "@/lib/getMetadata";
import type { NextApiRequest, NextApiResponse } from "next";
import { logger } from "@/config/logger";
import { withLogging } from "@/middleware/logging";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse<any>,
) {
async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
try {
const { method } = req;
if (method === "GET") {
Expand All @@ -29,3 +27,4 @@ export default async function handler(
}

export const config = { api: { bodyParser: false } };
export default withLogging(handler);
8 changes: 4 additions & 4 deletions vclogin/pages/api/dynamic/createTempAuthorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import { NextApiRequest, NextApiResponse } from "next";
import crypto from "crypto";
import { redisSet } from "@/config/redis";
import { withLogging } from "@/middleware/logging";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse<any>,
) {
async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
//Get Policy from request body
const { policy, inputDescriptor } = req.body;

Expand All @@ -29,3 +27,5 @@ export default async function handler(
return res.status(500).json({ redirect: "/error" });
}
}

export default withLogging(handler);
8 changes: 4 additions & 4 deletions vclogin/pages/api/dynamic/getAuthResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import { NextApiRequest, NextApiResponse } from "next";
import { logger } from "@/config/logger";
import { redisGet } from "@/config/redis";
import { withLogging } from "@/middleware/logging";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse<any>,
) {
async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
//read uuid from query params
const uuid = req.query["uuid"];
logger.debug("uuid: ", uuid);
Expand All @@ -29,3 +27,5 @@ export default async function handler(
res.status(200).json({ auth_res: "error_not_found" });
}
}

export default withLogging(handler);
8 changes: 4 additions & 4 deletions vclogin/pages/api/dynamic/getQRCodeString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
* SPDX-License-Identifier: MIT
*/

import { withLogging } from "@/middleware/logging";
import { NextApiRequest, NextApiResponse } from "next";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse<any>,
) {
async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
const { userId, uuid } = req.body;

//Generate QR Code String from UUID
Expand All @@ -24,3 +22,5 @@ export default async function handler(

return res.status(200).json({ qrCodeString });
}

export default withLogging(handler);
5 changes: 4 additions & 1 deletion vclogin/pages/api/dynamic/presentCredentialById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { verifyAuthenticationPresentation } from "@/lib/verifyPresentation";
import { getToken } from "@/lib/getToken";
import { logger } from "@/config/logger";
import { redisSet, redisGet } from "@/config/redis";
import { withLogging } from "@/middleware/logging";

const getHandler = async (req: NextApiRequest, res: NextApiResponse) => {
logger.debug("LOGIN API GET BY ID");
Expand Down Expand Up @@ -115,7 +116,7 @@ const handlers: any = {
GET: getHandler,
};

export default async function handler(
async function handler(
req: NextApiRequest,
res: NextApiResponse<any>, //todo look for separate handles
) {
Expand All @@ -129,3 +130,5 @@ export default async function handler(
res.status(500).end();
}
}

export default withLogging(handler);

0 comments on commit 68cb0b6

Please sign in to comment.