This commit is contained in:
Darren Clarke 2023-08-25 07:11:33 +00:00
parent 8f165d15d2
commit c620e4bf25
264 changed files with 9983 additions and 2280 deletions

View file

@ -1,88 +0,0 @@
import { NextRequest } from "next/server";
import NextAuth from "next-auth";
import Google from "next-auth/providers/google";
import GitHub from "next-auth/providers/github";
import GitLab from "next-auth/providers/gitlab";
import Cognito from "next-auth/providers/cognito";
import { loadConfig, IAppConfig } from "@digiresilience/metamigo-config";
import { MetamigoAdapter } from "app/_lib/nextauth-adapter";
import { CloudflareAccessProvider } from "app/_lib/cloudflare";
const nextAuthOptions = (config: IAppConfig, req: NextRequest) => {
const { nextAuth, cfaccess } = config;
const adapter = MetamigoAdapter(config);
const providers = [];
const { audience, domain } = cfaccess;
const cloudflareAccessEnabled = audience && domain;
if (cloudflareAccessEnabled)
providers.push(CloudflareAccessProvider(audience, domain, adapter, req as any));
else {
if (nextAuth.google?.id)
providers.push(
Google({
clientId: nextAuth.google.id,
clientSecret: nextAuth.google.secret,
})
);
if (nextAuth.github?.id)
providers.push(
GitHub({
clientId: nextAuth.github.id,
clientSecret: nextAuth.github.secret,
})
);
if (nextAuth.gitlab?.id)
providers.push(
GitLab({
clientId: nextAuth.gitlab.id,
clientSecret: nextAuth.gitlab.secret,
})
);
if (nextAuth.cognito?.id)
providers.push(
Cognito({
clientId: nextAuth.cognito.id,
clientSecret: nextAuth.cognito.secret,
// domain: nextAuth.cognito.domain,
})
);
}
if (providers.length === 0)
throw new Error(
"No next-auth providers configured. See Metamigo configuration docs."
);
return {
secret: nextAuth.secret,
session: {
strategy: "database",
maxAge: 8 * 60 * 60, // 8 hours
},
jwt: {
secret: nextAuth.secret,
},
providers,
adapter,
callbacks: {
async session({ session, user }: any) {
session.user.id = user.id;
session.user.userRole = user.userRole;
return session;
},
},
};
};
const handler = async (req: NextRequest, context: any) => {
const config = await loadConfig();
const authOptions = nextAuthOptions(config, req);
// @ts-expect-error: non-existent property
return NextAuth(req, context, authOptions);
};
export { handler as GET, handler as POST };

View file

@ -1,30 +0,0 @@
import { createProxyMiddleware } from "http-proxy-middleware";
export const POST = createProxyMiddleware({
target:
process.env.NODE_ENV === "production"
? "http://metamigo-api:3001"
: "http://127.0.0.1:3001",
changeOrigin: true,
pathRewrite: { "^/graphql": "/graphql" },
xfwd: true,
onProxyReq(proxyReq, req, _res) {
const auth = proxyReq.getHeader("authorization");
if (auth) {
// pass along user provided authorization header
return;
}
// Else extract the session token from the cookie and pass
// as bearer token to the proxy target
let token = req.cookies["__Secure-next-auth.session-token"];
if (!token) token = req.cookies["next-auth.session-token"];
if (token) {
proxyReq.setHeader("authorization", `Bearer ${token}`);
proxyReq.removeHeader("cookie");
} else {
console.error("no token found. proxied request to backend will fail.");
}
},
});

View file

@ -1,33 +0,0 @@
import { createProxyMiddleware } from "http-proxy-middleware";
const handler = createProxyMiddleware({
target:
process.env.NODE_ENV === "production"
? "http://metamigo-api:3001"
: "http://localhost:3001",
changeOrigin: true,
pathRewrite: { "^/api/v1": "/api" },
xfwd: true,
onProxyReq(proxyReq, req) {
const auth = proxyReq.getHeader("authorization");
if (auth) {
// pass along user provided authorization header
return;
}
// Else extract the session token from the cookie and pass
// as bearer token to the proxy target
// const token = req.cookies["next-auth.session-token"];
let token = req.cookies["__Secure-next-auth.session-token"];
if (!token) token = req.cookies["next-auth.session-token"];
if (token) {
proxyReq.setHeader("authorization", `Bearer ${token}`);
proxyReq.removeHeader("cookie");
} else {
console.error("no token found. proxied request to backend will fail.");
}
},
});
export { handler as GET, handler as POST, handler as PUT, handler as DELETE};