Secure Sakani portal and show full album titles

This commit is contained in:
Leon Tonhäuser committed 2026-11-30 19:14:57 +01:00
1 parent c40ccf77cf
commit 69264ce165
49 files changed
+1362 -601

No files matched your search

+32 -8
View File
@@ -1,7 +1,7 @@
import "server-only";
import { APIError } from "better-auth";
import { createAuthMiddleware } from "better-auth/api";
import { createAuthMiddleware, getSessionFromCtx } from "better-auth/api";
import { drizzleAdapter } from "@better-auth/drizzle-adapter";
import { betterAuth, type BetterAuthOptions } from "better-auth";
import { twoFactor } from "better-auth/plugins";
@@ -27,6 +27,13 @@ import {
type RegistrationInput,
} from "./registration";
export const MFA_SESSION_TOKEN_PREFIX = "mfa-";
const mfaVerificationPaths = new Set([
"/two-factor/verify-totp",
"/two-factor/verify-backup-code",
]);
export const portalAuthRateLimitOptions = {
enabled: true,
storage: "memory",
@@ -91,13 +98,19 @@ export function createPortalAuth({
},
plugins: [
twoFactor({
// TOTP + backup codes only: without `otpOptions.sendOTP` no OTP-e-mail
// surface exists, and trust-device stays off to keep the attack area
// small. Backup codes stay encrypted (plugin default).
// TOTP + backup codes only. Expire new trust-device records immediately.
trustDeviceMaxAge: 0,
}),
],
hooks: {
before: createPasswordPolicyHook(),
before: createAuthSecurityHook(),
after: createAuthMiddleware(async (ctx) => {
if (ctx.path !== "/two-factor/disable" || !ctx.context.newSession) return;
const { session, user } = ctx.context.newSession;
const sessions = await ctx.context.internalAdapter.listSessions(user.id);
const otherTokens = sessions.filter(({ token }) => token !== session.token).map(({ token }) => token);
if (otherTokens.length > 0) await ctx.context.internalAdapter.deleteSessions(otherTokens);
}),
},
emailAndPassword: {
enabled: true,
@@ -127,8 +140,12 @@ export function createPortalAuth({
databaseHooks: {
session: {
create: {
async before(session) {
return registrationRepository.canCreateSession(session.userId);
async before(session, ctx) {
if (!(await registrationRepository.canCreateSession(session.userId))) return false;
// These endpoints issue a new session only after verifying the factor.
if (ctx?.path && mfaVerificationPaths.has(ctx.path)) {
return { data: { ...session, token: MFA_SESSION_TOKEN_PREFIX + session.token } };
}
},
},
},
@@ -153,12 +170,19 @@ export function createPortalAuth({
return { auth, registrationService };
}
export function createPasswordPolicyHook() {
function createAuthSecurityHook() {
// Applies the shared policy to password-reset and change-password flows,
// which bypass the registration validation. Registration is covered by
// validateRegistration(); sign-up additionally enforces Better Auth's
// minPasswordLength.
return createAuthMiddleware(async (ctx) => {
if (ctx.path?.startsWith("/two-factor/") && !mfaVerificationPaths.has(ctx.path)) {
const current = await getSessionFromCtx(ctx);
if ((current?.user as { twoFactorEnabled?: boolean } | undefined)?.twoFactorEnabled &&
!current?.session.token.startsWith(MFA_SESSION_TOKEN_PREFIX)) {
throw new APIError("UNAUTHORIZED");
}
}
if (ctx.path !== "/reset-password" && ctx.path !== "/change-password") {
return;
}