Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
import crypto from "node:crypto";
import { buildEnv, serverEnv } from "@cap/env";
import { User } from "@cap/web-domain";
import { eq } from "drizzle-orm";
import type { NextAuthOptions } from "next-auth";
import { getServerSession as _getServerSession } from "next-auth";
import type { Adapter } from "next-auth/adapters";
import { decode, type JWT, type JWTDecodeParams } from "next-auth/jwt";
import AppleProvider from "next-auth/providers/apple";
import CredentialsProvider from "next-auth/providers/credentials";
import EmailProvider from "next-auth/providers/email";
import GoogleProvider from "next-auth/providers/google";
import type { Provider } from "next-auth/providers/index";
import WorkOSProvider from "next-auth/providers/workos";
import { getEmailDeliveryStatus, sendEmail } from "../emails/config.ts";
import { db } from "../index.ts";
import { users } from "../schema.ts";
import {
isBlockedAccountEmail,
isEmailAllowedForSignup,
isEmailBlockedFromSignup,
} from "./domain-utils.ts";
import { DrizzleAdapter } from "./drizzle-adapter.ts";
import {
provisionSsoMembership,
type SsoAuthContext,
type ValidatedSsoIdentity,
validateSsoSignIn,
} from "./sso.ts";
import { ssoLoginErrorPath } from "./sso-state.ts";
export const maxDuration = 120;
const OTP_CODE_MAX_AGE_SECONDS = 10 * 60;
// authOptions() is rebuilt per request (getServerSession, getCurrentUser), so
// the trusted-proxy warning is gated at module scope to log once per process.
let trustedProxyWarned = false;
export async function decodeSessionToken(
params: JWTDecodeParams,
): Promise<JWT | null> {
const token = await decode(params);
if (!token) return null;
// A blocked email loses its existing sessions, not just new sign-ins.
if (typeof token.email === "string" && isBlockedAccountEmail(token.email))
return null;
const userId = typeof token.id === "string" ? token.id : null;
if (!userId) return token;
const [user] = await db()
.select({ authSessionVersion: users.authSessionVersion })
.from(users)
.where(eq(users.id, User.UserId.make(userId)))
.limit(1);
if (!user) return null;
const sessionVersion =
typeof token.sessionVersion === "number" ? token.sessionVersion : 0;
if (sessionVersion !== user.authSessionVersion) return null;
return token;
}
const OIDC_PROVIDER_ID = "oidc";
type OidcClaims = {
sub: string;
email?: string;
email_verified?: boolean;
name?: string;
preferred_username?: string;
picture?: string;
};
export const authOptions = (ssoContext?: SsoAuthContext): NextAuthOptions => {
let _adapter: Adapter | undefined;
let _providers: Provider[] | undefined;
let validatedSsoIdentity: ValidatedSsoIdentity | null = null;
return {
get adapter() {
if (_adapter) return _adapter;
_adapter = DrizzleAdapter(db(), {
getSsoIdentity: () => validatedSsoIdentity,
});
return _adapter;
},
debug: process.env.NODE_ENV !== "production",
session: {
strategy: "jwt",
},
jwt: {
decode: decodeSessionToken,
},
get secret() {
return serverEnv().NEXTAUTH_SECRET;
},
pages: {
signIn: "/login",
},
get providers() {
if (_providers) return _providers;
const appleClientId = serverEnv().APPLE_CLIENT_ID;
const appleClientSecret = serverEnv().APPLE_CLIENT_SECRET;
const oidcIssuer = serverEnv().OIDC_ISSUER;
const oidcClientId = serverEnv().OIDC_CLIENT_ID;
const oidcClientSecret = serverEnv().OIDC_CLIENT_SECRET;
const trustedProxyEnabled =
serverEnv().TRUSTED_PROXY_AUTH_HEADER &&
serverEnv().TRUSTED_PROXY_AUTH_EMAIL &&
serverEnv().TRUSTED_PROXY_AUTH_SECRET?.trim();
if (!trustedProxyWarned && trustedProxyEnabled) {
trustedProxyWarned = true;
console.warn(
"[auth] trusted-proxy sign-in is ENABLED. Keep the origin private and use an authenticating reverse proxy that strips client-sent copies of both the trust and secret headers and sets them only on owner-authenticated requests.",
);
}
_providers = [
...(oidcIssuer && oidcClientId && oidcClientSecret
? [
{
id: OIDC_PROVIDER_ID,
name: serverEnv().OIDC_NAME || "SSO",
type: "oauth" as const,
wellKnown: `${oidcIssuer.replace(/\/$/, "")}/.well-known/openid-configuration`,
issuer: oidcIssuer,
clientId: oidcClientId,
clientSecret: oidcClientSecret,
idToken: true,
checks: ["state" as const, "pkce" as const],
authorization: { params: { scope: "openid email profile" } },
// Linking is gated on a verified email in the signIn callback,
// because a generic issuer is not known to verify addresses.
allowDangerousEmailAccountLinking: true,
profile(profile: OidcClaims) {
const email = profile.email?.trim().toLowerCase();
return {
id: profile.sub,
name:
profile.name ||
profile.preferred_username ||
email?.split("@")[0] ||
profile.sub,
email,
image: profile.picture ?? null,
};
},
},
]
: []),
...(appleClientId && appleClientSecret
? [
AppleProvider({
clientId: appleClientId,
clientSecret: appleClientSecret,
}),
]
: []),
GoogleProvider({
clientId: serverEnv().GOOGLE_CLIENT_ID as string,
clientSecret: serverEnv().GOOGLE_CLIENT_SECRET as string,
authorization: {
params: {
scope: [
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
].join(" "),
prompt: "select_account",
},
},
}),
...(serverEnv().WORKOS_CLIENT_ID && serverEnv().WORKOS_API_KEY
? [
WorkOSProvider({
clientId: serverEnv().WORKOS_CLIENT_ID as string,
clientSecret: serverEnv().WORKOS_API_KEY as string,
checks: ["state", "pkce"],
allowDangerousEmailAccountLinking: true,
profile(profile) {
return {
id: profile.id,
name:
[profile.first_name, profile.last_name]
.filter(Boolean)
.join(" ") ||
profile.email?.split("@")[0] ||
profile.id,
email: profile.email?.trim().toLowerCase(),
image: null,
};
},
}),
]
: []),
...(trustedProxyEnabled
? [
CredentialsProvider({
id: "trusted-proxy",
name: "Reverse Proxy",
credentials: {},
// SECURITY: this header is trusted ONLY because the operator
// opted in via env, asserting Cap runs behind a reverse proxy
// that strips client-sent copies of both trust headers and sets
// them only on owner-authenticated requests. Never enable without such a
// proxy — it would be a login bypass. Fails closed below.
async authorize(_credentials, req) {
const env = serverEnv();
const header = env.TRUSTED_PROXY_AUTH_HEADER?.toLowerCase();
const email = env.TRUSTED_PROXY_AUTH_EMAIL?.toLowerCase();
const secret = env.TRUSTED_PROXY_AUTH_SECRET;
if (!header || !email || !secret?.trim()) return null;
// The router is the sole authority for this header (it strips
// any client-sent copy), so its presence means the request was
// owner-authenticated. Fail closed on anything else.
if (req?.headers?.[header] !== "true") return null;
// Always require the proxy's secret, even if a caller can reach
// the origin directly. Compare fixed-size digests in constant time.
const provided = req?.headers?.["x-trusted-proxy-secret"];
if (typeof provided !== "string" || !provided) return null;
const digest = (s: string) =>
crypto.createHash("sha256").update(s).digest();
if (!crypto.timingSafeEqual(digest(provided), digest(secret)))
return null;
const [user] = await db()
.select({
id: users.id,
email: users.email,
name: users.name,
image: users.image,
})
.from(users)
.where(eq(users.email, email))
.limit(1);
if (!user) return null;
return {
id: user.id,
email: user.email,
name: user.name,
image: user.image,
};
},
}),
]
: []),
EmailProvider({
// next-auth defaults to 24h, but the code is 6 digits and the
// verify path has no attempt limiting, so a day-long window is a
// practical brute-force target. The OTP email and the dev console
// have always told users 10 minutes; this makes that true.
maxAge: OTP_CODE_MAX_AGE_SECONDS,
async generateVerificationToken() {
return crypto.randomInt(100000, 1000000).toString();
},
async sendVerificationRequest({ identifier, token }) {
const emailStatus = getEmailDeliveryStatus();
if (!emailStatus.configured) {
if (
process.env.NODE_ENV === "production" &&
buildEnv.NEXT_PUBLIC_IS_CAP === "true"
) {
throw new Error(emailStatus.reason);
}
console.log("\n");
console.log(
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━",
);
console.log("🔐 VERIFICATION CODE (email delivery disabled)");
console.log(`ℹ️ ${emailStatus.reason}`);
console.log(
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━",
);
console.log(`📧 Email: ${identifier}`);
console.log(`🔢 Code: ${token}`);
console.log(
`⏱ Expires in: ${OTP_CODE_MAX_AGE_SECONDS / 60} minutes`,
);
console.log(
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━",
);
console.log("\n");
} else {
const { OTPEmail } = await import("../emails/otp-email");
const email = OTPEmail({ code: token, email: identifier });
const delivery = await sendEmail({
email: identifier,
subject: `Your Cap Verification Code`,
react: email,
});
if (delivery?.error) {
throw new Error(
`Failed to send verification email: ${delivery.error.message}`,
);
}
}
},
}),
];
return _providers;
},
cookies: {
sessionToken: {
name: `next-auth.session-token`,
options: {
httpOnly: true,
sameSite: "none",
path: "/",
secure: true,
},
},
callbackUrl: {
name: "next-auth.callback-url",
options: {
httpOnly: true,
sameSite: "none",
path: "/",
secure: true,
},
},
pkceCodeVerifier: {
name: "next-auth.pkce.code_verifier",
options: {
httpOnly: true,
sameSite: "none",
path: "/",
secure: true,
maxAge: 60 * 15,
},
},
},
callbacks: {
async signIn({ user, email, credentials, account, profile }) {
if (account?.provider === OIDC_PROVIDER_ID) {
const claims = profile as OidcClaims | undefined;
if (!claims?.email || claims.email_verified !== true) {
return "/login?error=OidcEmailUnverified";
}
}
if (account?.provider === "workos") {
validatedSsoIdentity = null;
try {
validatedSsoIdentity = await validateSsoSignIn(
profile,
account.providerAccountId,
ssoContext,
);
} catch {
return ssoLoginErrorPath(
"SsoSignInFailed",
ssoContext?.intent?.returnTo,
);
}
}
const rawEmail =
user?.email ||
(typeof email === "string"
? email
: typeof credentials?.email === "string"
? credentials.email
: null);
if (!rawEmail || typeof rawEmail !== "string") return true;
const userEmail = rawEmail.toLowerCase();
if (
isEmailBlockedFromSignup(
userEmail,
serverEnv().CAP_BLOCKED_SIGNUP_DOMAINS,
)
) {
console.warn(`Sign-in blocked for email: ${userEmail}`);
return "/login?error=SignupBlocked";
}
const allowedDomains = serverEnv().CAP_ALLOWED_SIGNUP_DOMAINS;
if (!allowedDomains) return true;
const [existingUser] = await db()
.select()
.from(users)
.where(eq(users.email, userEmail))
.limit(1);
// Only apply domain restrictions for new users, existing ones can always sign in
if (
!existingUser &&
!isEmailAllowedForSignup(userEmail, allowedDomains)
) {
console.warn(`Signup blocked for email domain: ${userEmail}`);
return false;
}
return true;
},
async session({ token, session }) {
if (!session.user) return session;
if (token?.id && typeof token.id === "string") {
(session.user as { id: string }).id = token.id;
session.user.name = token.name ?? null;
session.user.email = token.email ?? null;
session.user.image = token.picture ?? null;
}
return session;
},
async jwt({ token, user, account }) {
if (account?.provider === "workos") {
if (!user || !validatedSsoIdentity) {
throw new Error("The SSO sign-in was not verified.");
}
await provisionSsoMembership(
User.UserId.make(user.id),
validatedSsoIdentity,
);
}
if (user || !token.id) {
const [dbUser] = await db()
.select({
id: users.id,
name: users.name,
lastName: users.lastName,
email: users.email,
image: users.image,
authSessionVersion: users.authSessionVersion,
})
.from(users)
.where(
user
? eq(users.id, User.UserId.make(user.id))
: eq(users.email, (token.email || "").toLowerCase()),
)
.limit(1);
if (!dbUser) {
if (user) {
token.id = user?.id;
}
return token;
}
return {
id: dbUser.id,
name: dbUser.name,
lastName: dbUser.lastName,
email: dbUser.email,
picture: dbUser.image,
sessionVersion: dbUser.authSessionVersion,
};
}
return token;
},
},
};
};
export const getServerSession = () => _getServerSession(authOptions());