link-stack/apps/link/middleware.ts

137 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-03-20 17:51:21 +01:00
import { NextResponse } from "next/server";
2023-06-05 14:48:23 +00:00
import { withAuth, NextRequestWithAuth } from "next-auth/middleware";
2023-05-30 09:05:40 +00:00
2024-03-20 17:51:21 +01:00
const rewriteURL = (
request: NextRequestWithAuth,
originBaseURL: string,
destinationBaseURL: string,
headers: any = {},
) => {
2024-10-17 22:39:08 +02:00
let path = request.url.replace(originBaseURL, "");
if (path.startsWith("/")) {
path = path.slice(1);
}
const destinationURL = `${destinationBaseURL}/${path}`;
2023-05-30 09:05:40 +00:00
console.log(`Rewriting ${request.url} to ${destinationURL}`);
2023-06-05 15:00:46 +00:00
const requestHeaders = new Headers(request.headers);
requestHeaders.delete("x-forwarded-user");
2024-11-28 08:27:20 +01:00
requestHeaders.delete("x-forwarded-roles");
requestHeaders.delete("connection");
2024-08-14 10:51:12 +02:00
2023-06-05 15:00:46 +00:00
for (const [key, value] of Object.entries(headers)) {
2023-08-25 07:11:33 +00:00
requestHeaders.set(key, value as string);
2023-06-05 15:00:46 +00:00
}
2024-08-14 10:51:12 +02:00
2024-03-20 17:51:21 +01:00
return NextResponse.rewrite(new URL(destinationURL), {
request: { headers: requestHeaders },
});
2023-05-30 09:05:40 +00:00
};
2023-06-05 14:48:23 +00:00
const checkRewrites = async (request: NextRequestWithAuth) => {
2023-06-05 15:00:46 +00:00
const linkBaseURL = process.env.LINK_URL ?? "http://localhost:3000";
const zammadURL = process.env.ZAMMAD_URL ?? "http://zammad-nginx:8080";
2024-11-28 08:27:20 +01:00
const opensearchBaseURL =
process.env.OPENSEARCH_DASHBOARDS_URL ??
"http://opensearch-dashboards:5601";
2024-08-14 10:51:12 +02:00
const zammadPaths = [
"/zammad",
"/auth/sso",
"/assets",
"/mobile",
"/graphql",
"/cable",
];
2023-08-25 07:11:33 +00:00
const { token } = request.nextauth;
2024-03-20 17:51:21 +01:00
const email = token?.email?.toLowerCase() ?? "unknown";
2024-11-28 08:27:20 +01:00
const roles = (token?.roles as string[]) ?? [];
let headers = {
"x-forwarded-user": email,
"x-forwarded-roles": roles.join(","),
};
2024-03-20 17:51:21 +01:00
2024-11-28 08:27:20 +01:00
if (request.nextUrl.pathname.startsWith("/dashboards")) {
return rewriteURL(
request,
`${linkBaseURL}/dashboards`,
opensearchBaseURL,
headers,
);
} else if (request.nextUrl.pathname.startsWith("/zammad")) {
return rewriteURL(request, `${linkBaseURL}/zammad`, zammadURL, headers);
2024-03-20 17:51:21 +01:00
} else if (zammadPaths.some((p) => request.nextUrl.pathname.startsWith(p))) {
return rewriteURL(request, linkBaseURL, zammadURL, headers);
} else {
const isDev = process.env.NODE_ENV === "development";
const nonce = Buffer.from(crypto.randomUUID()).toString("base64");
const cspHeader = `
default-src 'self';
2024-11-28 08:27:20 +01:00
frame-src 'self' https://digiresilience.org;
connect-src 'self';
script-src 'self' 'nonce-${nonce}' 'strict-dynamic' ${isDev ? "'unsafe-eval'" : ""};
style-src 'self' 'unsafe-inline';
img-src 'self' blob: data:;
font-src 'self';
object-src 'none';
base-uri 'self';
form-action 'self';
frame-ancestors 'none';
upgrade-insecure-requests;
`;
const contentSecurityPolicyHeaderValue = cspHeader
.replace(/\s{2,}/g, " ")
.trim();
const requestHeaders = new Headers(request.headers);
requestHeaders.set("x-nonce", nonce);
requestHeaders.set(
"Content-Security-Policy",
contentSecurityPolicyHeaderValue,
);
2023-08-25 07:11:33 +00:00
const response = NextResponse.next({
request: {
headers: requestHeaders,
},
});
response.headers.set(
"Content-Security-Policy",
contentSecurityPolicyHeaderValue,
);
return response;
}
2023-05-30 09:05:40 +00:00
};
2023-05-24 20:27:57 +00:00
2024-03-20 17:51:21 +01:00
export default withAuth(checkRewrites, {
callbacks: {
authorized: ({ token, req }) => {
if (process.env.SETUP_MODE === "true") {
return true;
}
2023-05-24 20:27:57 +00:00
const path = req.nextUrl.pathname;
2024-03-20 17:51:21 +01:00
const roles: any = token?.roles ?? [];
2024-08-07 12:02:33 +02:00
if (path.startsWith("/login")) {
return true;
}
2024-08-07 12:02:33 +02:00
if (path.startsWith("/admin") && !roles.includes("admin")) {
return false;
}
2024-03-20 17:51:21 +01:00
if (roles.includes("admin") || roles.includes("agent")) {
return true;
}
2024-03-20 17:51:21 +01:00
return false;
},
},
});
2023-08-25 07:11:33 +00:00
export const config = {
matcher: ["/((?!ws|wss|api|_next/static|_next/image|favicon.ico).*)"],
2023-08-25 07:11:33 +00:00
};