link-stack/apps/link/middleware.ts

93 lines
3 KiB
TypeScript
Raw Normal View History

2023-05-30 09:05:40 +00:00
import { NextResponse } from 'next/server';
import type { NextRequest } 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
const rewriteURL = (request: NextRequest, originBaseURL: string, destinationBaseURL: string, headers: any = {}) => {
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 14:48:23 +00:00
const requestHeaders = new Headers(request.headers);
for (const [key, value] of Object.entries(headers)) {
// @ts-ignore
requestHeaders.set(key, value);
}
requestHeaders.delete('connection');
console.log({finalHeaders: requestHeaders});
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) => {
console.log({currentURL: request.nextUrl.href});
const linkBaseURL = "http://localhost:3000";
const zammadURL = "http://zammad-nginx:8080";
2023-05-30 09:05:40 +00:00
if (request.nextUrl.pathname.startsWith('/proxy/leafcutter')) {
2023-06-05 14:48:23 +00:00
return rewriteURL(request, linkBaseURL, "http://leafcutter:3000");
2023-05-30 09:05:40 +00:00
} else if (request.nextUrl.pathname.startsWith('/proxy/metamigo')) {
2023-06-05 14:48:23 +00:00
return rewriteURL(request, linkBaseURL, "http://metamigo:3000");
2023-05-30 09:05:40 +00:00
} else if (request.nextUrl.pathname.startsWith('/proxy/zammad')) {
2023-06-05 14:48:23 +00:00
console.log('proxying to zammad');
const { token } = request.nextauth;
console.log({nextauth: request.nextauth});
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-06-05 14:48:23 +00:00
console.log({headers});
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;
},
}
}
);