init
This commit is contained in:
+71
@@ -0,0 +1,71 @@
|
||||
import NextAuth from "next-auth";
|
||||
import MicrosoftEntraID from "next-auth/providers/microsoft-entra-id";
|
||||
import Credentials from "next-auth/providers/credentials";
|
||||
import { authConfig } from "@/auth.config";
|
||||
import { lookupRole, safeEqual } from "@/lib/authz";
|
||||
|
||||
// Auth.js v5 (NextAuth). Node-runtime config: spreads the edge-safe authConfig
|
||||
// and adds the providers + Node-only callbacks (signIn/jwt) that need node:fs
|
||||
// via lookupRole. Imported by the API route handler and server components —
|
||||
// NEVER by the edge middleware (which uses src/auth.config.ts instead).
|
||||
//
|
||||
// Two sign-in paths:
|
||||
// 1. Microsoft Entra ID SSO — allowed only if the email is in config/users.json.
|
||||
// 2. Local username/password — break-glass admin, bypasses the JSON allowlist.
|
||||
export const { handlers, auth, signIn, signOut } = NextAuth({
|
||||
...authConfig,
|
||||
providers: [
|
||||
MicrosoftEntraID({
|
||||
clientId: process.env.AUTH_MICROSOFT_ENTRA_ID_ID,
|
||||
clientSecret: process.env.AUTH_MICROSOFT_ENTRA_ID_SECRET,
|
||||
// Tenant-specific issuer restricts sign-in to our organization only.
|
||||
issuer: process.env.AUTH_MICROSOFT_ENTRA_ID_ISSUER,
|
||||
}),
|
||||
Credentials({
|
||||
name: "Local account",
|
||||
credentials: {
|
||||
username: { label: "Username", type: "text" },
|
||||
password: { label: "Password", type: "password" },
|
||||
},
|
||||
authorize(credentials) {
|
||||
const username = String(credentials?.username ?? "");
|
||||
const password = String(credentials?.password ?? "");
|
||||
|
||||
const expectedUser = process.env.LOCAL_ADMIN_USERNAME || "admin";
|
||||
const expectedPass = process.env.LOCAL_ADMIN_PASSWORD ?? "";
|
||||
|
||||
// Empty password env disables local login entirely (fail closed).
|
||||
if (expectedPass === "") return null;
|
||||
|
||||
if (safeEqual(username, expectedUser) && safeEqual(password, expectedPass)) {
|
||||
return { id: "local-admin", name: username, role: "admin" };
|
||||
}
|
||||
return null;
|
||||
},
|
||||
}),
|
||||
],
|
||||
callbacks: {
|
||||
...authConfig.callbacks,
|
||||
// Gate SSO sign-ins against the JSON allowlist. The local Credentials
|
||||
// account is always allowed (its role is set in authorize()).
|
||||
signIn({ user, account, profile }) {
|
||||
if (account?.provider === "credentials") return true;
|
||||
|
||||
const email =
|
||||
user.email ??
|
||||
(profile?.email as string | undefined) ??
|
||||
(profile?.preferred_username as string | undefined);
|
||||
|
||||
// Returning false surfaces to the login page as ?error=AccessDenied.
|
||||
return lookupRole(email) !== null;
|
||||
},
|
||||
// On first sign-in, bake the role into the JWT so the edge middleware can
|
||||
// read it without touching node:fs.
|
||||
jwt({ token, user }) {
|
||||
if (user) {
|
||||
token.role = user.role ?? lookupRole(user.email) ?? "user";
|
||||
}
|
||||
return token;
|
||||
},
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user