link-stack/apps/link/middleware.ts

84 lines
2.6 KiB
TypeScript
Raw Permalink 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 = {},
) => {
2023-05-30 09:05:40 +00:00
const destinationURL = request.url.replace(originBaseURL, destinationBaseURL);
console.log(`Rewriting ${request.url} to ${destinationURL}`);
2023-06-05 15:00:46 +00:00
const requestHeaders = new Headers(request.headers);
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-03-20 17:51:21 +01:00
requestHeaders.delete("connection");
2023-06-05 14:48:23 +00: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-03-20 17:51:21 +01:00
const opensearchDashboardsURL =
process.env.OPENSEARCH_DASHBOARDS_URL ?? "http://macmini:5601";
const zammadPaths = ["/zammad", "/api/v1", "/auth/sso", "/assets", "/mobile"];
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";
let headers = { "x-forwarded-user": email };
if (request.nextUrl.pathname.startsWith("/dashboards")) {
const roles: string[] = (token?.roles as string[]) ?? [];
const leafcutterRole = roles.includes("admin")
? "leafcutter_admin"
: "leafcutter_user";
headers["x-forwarded-roles"] = "admin"; // leafcutterRole;
// headers["secruitytenant"] = "global";
// headers["x-forwarded-for"] = 'link';
2024-03-16 12:51:56 +01:00
2024-03-20 17:51:21 +01:00
return rewriteURL(
request,
`${linkBaseURL}/dashboards`,
opensearchDashboardsURL,
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);
2023-05-30 09:05:40 +00:00
}
2023-08-25 07:11:33 +00:00
return NextResponse.next();
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, {
pages: {
signIn: `/login`,
},
callbacks: {
authorized: ({ token, req }) => {
if (req.nextUrl.pathname === "/api/v1") {
return true;
}
2023-10-02 14:22:48 +02:00
2024-03-20 17:51:21 +01:00
if (process.env.SETUP_MODE === "true") {
return true;
}
2023-05-24 20:27:57 +00:00
2024-03-20 17:51:21 +01:00
const roles: any = token?.roles ?? [];
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 = {
2024-03-20 17:51:21 +01:00
matcher: ["/((?!ws|wss|_next/static|_next/image|favicon.ico).*)"],
2023-08-25 07:11:33 +00:00
};