link-stack/apps/link/middleware.ts

102 lines
3.4 KiB
TypeScript
Raw Permalink 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
2023-07-18 12:26:57 +00:00
// console.log({ finalHeaders: requestHeaders });
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
console.log({ currentURL: request.nextUrl.href });
const linkBaseURL = process.env.LINK_URL ?? "http://localhost:3000";
const zammadURL = process.env.ZAMMAD_URL ?? "http://zammad-nginx:8080";
2023-07-18 12:26:57 +00:00
const leafcutterURL = process.env.LEAFCUTTER_URL ?? "https://lc.digiresilience.org";
const metamigoURL = process.env.METAMIGO_URL ?? "http://metamigo-frontend:3000";
console.log({ linkBaseURL, zammadURL, leafcutterURL, metamigoURL });
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" };
return rewriteURL(request, linkBaseURL, leafcutterURL, headers);
2023-05-30 09:05:40 +00:00
} else if (request.nextUrl.pathname.startsWith('/proxy/metamigo')) {
2023-06-05 15:00:46 +00:00
return rewriteURL(request, linkBaseURL, metamigoURL);
2023-05-30 09:05:40 +00:00
} else if (request.nextUrl.pathname.startsWith('/proxy/zammad')) {
2023-06-05 15:00:46 +00:00
console.log('proxying to zammad');
2023-06-05 14:48:23 +00:00
const { token } = request.nextauth;
2023-07-18 12:26:57 +00:00
// console.log({ nextauth: request.nextauth });
2023-06-05 14:48:23 +00:00
2023-05-30 09:05:40 +00:00
const headers = {
2023-06-05 14:48:23 +00:00
'X-Forwarded-User': token.email.toLowerCase(),
host: 'link-stack-dev.digiresilience.org'
2023-05-30 09:05:40 +00:00
};
2023-07-18 12:26:57 +00:00
// console.log({ headers });
2023-06-05 14:48:23 +00:00
return rewriteURL(request, `${linkBaseURL}/proxy/zammad`, zammadURL, headers);
} else if (request.nextUrl.pathname.startsWith('/assets') || request.nextUrl.pathname.startsWith('/api/v1')) {
2023-05-30 09:05:40 +00:00
console.log('asset');
2023-06-05 14:48:23 +00:00
return rewriteURL(request, linkBaseURL, zammadURL);
} else if (request.nextUrl.pathname.startsWith('/proxy/assets')) {
2023-05-30 09:05:40 +00:00
console.log('proxy asset');
2023-06-05 14:48:23 +00:00
return rewriteURL(request, `${linkBaseURL}/proxy`, zammadURL);
} else if (request.nextUrl.pathname.startsWith('/proxy/api')) {
console.log('proxy api');
return rewriteURL(request, `${linkBaseURL}/proxy`, 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,
headers,
} = req;
// check login page
const parsedURL = new URL(url);
if (parsedURL.pathname.startsWith('/login')) {
return true;
}
// check session auth
const authorizedDomains = ["redaranj.com", "digiresilience.org"];
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;
},
}
}
);