dorfteich/apps/api/src/auth/auth.guard.ts
Claude Fable 5 9674c0bae2
Some checks failed
CI / Lint, typecheck, test (push) Successful in 3m41s
CD / Build and push images (push) Successful in 3m44s
CI / Build container images (push) Has been skipped
CD / Deploy to Test (push) Successful in 9s
CD / Smoke tests against Test (push) Successful in 1m18s
CD / Promote to Int (push) Successful in 11s
CI / Auth e2e pack (push) Failing after 2m53s
CI / Import/export fidelity gate (push) Has been skipped
Batch notifications into localized e-mail digests with unsubscribe (#95)
New per-user digestFrequency (hourly default | daily | off) on the
profile and in the settings UI. A scheduler job (15 min cadence) mails a
user once their oldest unread, unmailed notification exceeds the cadence
window: one localized mail per batch, grouped per pond then per page
with actor names and change/comment counts, enqueued through the mail
outbox. Sending marks the batch mailed — never read — and re-checks page
read permission per entry at send time; entries the user can no longer
read are dropped from the mail but still marked handled, so revoked
content cannot queue forever. Every mail carries a signed, single-purpose
unsubscribe link: it only flips the setting to off, renders a session-free
confirmation page, and sets no cookie.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EwZ4jR4KFAPvpjWevfUGX1
2026-07-11 23:37:26 +02:00

119 lines
3.9 KiB
TypeScript

import {
CanActivate,
ExecutionContext,
ForbiddenException,
Injectable,
SetMetadata,
UnauthorizedException,
createParamDecorator,
} from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import type { CurrentUser as CurrentUserShape } from '@dorfteich/shared';
import type { User } from '@prisma/client';
import type { Request, Response } from 'express';
import { AppConfig } from '../config/app-config.service';
import { SessionsService } from './sessions.service';
export const SESSION_COOKIE = 'dt_session';
const IS_PUBLIC_KEY = 'isPublic';
/** Marks a route as reachable without a session (login, signup, healthz…). */
export const Public = (): MethodDecorator & ClassDecorator => SetMetadata(IS_PUBLIC_KEY, true);
export interface AuthedRequest extends Request {
user?: User;
sessionId?: string;
sessionToken?: string;
}
/** Injects the authenticated Prisma user into a handler parameter. */
export const CurrentUser = createParamDecorator((_data: unknown, context: ExecutionContext) => {
return context.switchToHttp().getRequest<AuthedRequest>().user;
});
export function toCurrentUser(user: User): CurrentUserShape {
return {
id: user.id,
username: user.username,
email: user.email,
displayName: user.displayName,
locale: user.locale === 'de' ? 'de' : 'en',
isSiteAdmin: user.isSiteAdmin,
autoWatchOwnPages: user.autoWatchOwnPages,
autoWatchOnComment: user.autoWatchOnComment,
digestFrequency:
user.digestFrequency === 'daily' || user.digestFrequency === 'off'
? user.digestFrequency
: 'hourly',
};
}
/** Session cookie contract shared by login and the setup wizard (issue #80). */
export function setSessionCookie(response: Response, token: string, production: boolean): void {
response.cookie(SESSION_COOKIE, token, {
httpOnly: true,
sameSite: 'lax',
secure: production,
maxAge: 30 * 24 * 60 * 60 * 1000,
path: '/',
});
}
const MUTATING_METHODS = new Set(['POST', 'PUT', 'PATCH', 'DELETE']);
/**
* Global authentication guard: routes are protected by default and opt
* out with @Public(). Also enforces the CSRF origin check on mutating
* requests that carry a session cookie — SameSite=Lax is the first line
* of defense, this is the second (security.md).
*/
@Injectable()
export class AuthGuard implements CanActivate {
constructor(
private readonly reflector: Reflector,
private readonly sessions: SessionsService,
private readonly config: AppConfig,
) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest<AuthedRequest>();
const rawToken = (request.cookies as Record<string, string> | undefined)?.[SESSION_COOKIE];
if (rawToken && MUTATING_METHODS.has(request.method)) {
this.assertSameOrigin(request);
}
// Attach the user whenever the cookie is valid — public routes may
// still want to know who is asking.
if (rawToken) {
const validated = await this.sessions.validate(rawToken);
if (validated) {
request.user = validated.user;
request.sessionId = validated.session.id;
request.sessionToken = rawToken;
}
}
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
context.getHandler(),
context.getClass(),
]);
if (isPublic) return true;
if (!request.user) throw new UnauthorizedException();
return true;
}
private assertSameOrigin(request: Request): void {
const origin = request.headers.origin ?? request.headers.referer;
// Non-browser clients (curl, supertest) send neither header; SameSite
// cookies already stop cross-site browser requests without Origin.
if (!origin) return;
const expected = new URL(this.config.env.APP_BASE_URL).origin;
if (new URL(origin).origin !== expected) {
throw new ForbiddenException({ code: 'csrf_origin_mismatch' });
}
}
}