link-stack/apps/link/middleware.ts

83 lines
3 KiB
TypeScript
Raw Normal View History

2023-05-30 09:05:40 +00: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
2023-06-14 06:33:06 +00:00
const rewriteURL = (request: NextRequestWithAuth, originBaseURL: string, destinationBaseURL: string, headers: any = {}) => {
2023-06-05 15:00:46 +00:00
if (request.nextUrl.protocol.startsWith('ws')) {
return NextResponse.next();
}
2023-05-30 09:05:40 +00:00
if (request.nextUrl.pathname.includes('/_next/static/development/')) {
return NextResponse.next();
}
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)) {
// @ts-ignore
requestHeaders.set(key, value);
}
2023-06-05 14:48:23 +00:00
2023-06-05 15:00:46 +00:00
requestHeaders.delete('connection');
2023-06-05 14:48:23 +00: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";
2023-07-11 10:05:52 +00:00
const leafcutterURL = process.env.LEAFCUTTER_URL ?? "https://lc.digiresilience.org";
const metamigoURL = process.env.METAMIGO_URL ?? "http://metamigo-frontend:3000";
2023-07-21 12:26:02 +00:00
const labelStudioURL = process.env.LABEL_STUDIO_URL ?? "http://label-studio:8080";
2023-06-05 14:48:23 +00:00
2023-05-30 09:05:40 +00:00
if (request.nextUrl.pathname.startsWith('/proxy/leafcutter')) {
2023-06-21 12:48:07 +00:00
const headers = { 'X-Leafcutter-Embedded': "true" };
2023-07-21 12:26:02 +00:00
return rewriteURL(request, `${linkBaseURL}/proxy/leafcutter`, leafcutterURL, headers);
2023-05-30 09:05:40 +00:00
} else if (request.nextUrl.pathname.startsWith('/proxy/metamigo')) {
2023-07-21 12:26:02 +00:00
return rewriteURL(request, `${linkBaseURL}/proxy/metamigo`, metamigoURL);
} else if (request.nextUrl.pathname.startsWith('/proxy/label-studio')) {
return rewriteURL(request, `${linkBaseURL}/proxy/label-studio`, labelStudioURL);
2023-05-30 09:05:40 +00:00
} else if (request.nextUrl.pathname.startsWith('/proxy/zammad')) {
2023-06-05 14:48:23 +00:00
const { token } = request.nextauth;
2023-07-21 12:26:02 +00:00
const headers = { 'X-Forwarded-User': token.email.toLowerCase() };
2023-06-05 14:48:23 +00:00
return rewriteURL(request, `${linkBaseURL}/proxy/zammad`, zammadURL, headers);
} else if (request.nextUrl.pathname.startsWith('/proxy/api')) {
return rewriteURL(request, `${linkBaseURL}/proxy`, zammadURL);
2023-07-21 12:26:02 +00:00
} else if (request.nextUrl.pathname.startsWith('/api/v1')) {
return rewriteURL(request, linkBaseURL, zammadURL);
2023-05-30 09:05:40 +00:00
}
};
2023-05-24 20:27:57 +00:00
export default withAuth(
2023-05-30 09:05:40 +00:00
checkRewrites,
2023-05-24 20:27:57 +00:00
{
pages: {
signIn: `/login`,
},
callbacks: {
authorized: ({ token, req }) => {
const {
url,
} = req;
// check login page
const parsedURL = new URL(url);
if (parsedURL.pathname.startsWith('/login')) {
return true;
}
// check session auth
2023-07-19 14:02:08 +00:00
const authorizedDomains = ["redaranj.com", "digiresilience.org", "sr2.uk"];
2023-05-24 20:27:57 +00:00
const userDomain = token?.email?.toLowerCase().split("@").pop() ?? "unauthorized.net";
if (authorizedDomains.includes(userDomain)) {
return true;
}
2023-05-24 20:27:57 +00:00
return false;
},
}
}
);