feat: MFA (TOTP) und Self-Service-Profilseite
Backend Image / build-and-push (push) Successful in 2m57s
Frontend Image / build-and-push (push) Successful in 2m30s

Benutzer koennen jetzt selbst Zwei-Faktor-Authentifizierung einrichten
und ihr Profil verwalten (Name, E-Mail, Passwort, Profilbild).

Backend:
- Neues MfaModule (src/auth/mfa/): AES-256-GCM-verschluesselte
  TOTP-Secrets (otplib), QR-Code-Erzeugung, kurzlebige Setup-/
  Challenge-JWTs fuer den 2-Schritt-Login-Flow
- Neues ProfileModule (src/profile/): PATCH /profile,
  POST /profile/password, POST/DELETE/GET /profile/avatar,
  POST /profile/mfa/enroll(/confirm), POST /profile/mfa/disable -
  alle self-scoped ueber actor.id, ohne @RequirePermission (siehe
  ALLOWLIST in authorization-coverage.e2e-spec.ts)
- AuthService.login() zweistufig: bei aktivem MFA erst
  { mfaRequired, mfaToken } statt Tokens, neuer Endpunkt
  POST /auth/mfa/verify schliesst den Flow ab
- Admin-Recovery: POST /users/:id/mfa/reset (users:update) fuer
  verlorene MFA-Geraete, da es keinen E-Mail-Reset-Flow gibt

Frontend:
- Login-Seite mit zweitem Schritt fuer den TOTP-Code
- Neue Profilseite (/profile, ausserhalb des admin-only
  Settings-Bereichs): Profilbild, Name/E-Mail, Passwort, MFA-Ein-/
  Ausrichtung mit QR-Code-Dialog
- Header zeigt jetzt Avatar mit Dropdown (Profil/Abmelden) statt
  nur E-Mail-Text
- Admin-Users-Tabelle zeigt MFA-Status + Reset-Button

Haertung nach Security-Review (Backend/Frontend-Diff, kein CRITICAL,
mehrere MEDIUM behoben):
- Access-Token wird bei Passwortwechsel sofort ungueltig
  (User.passwordChangedAt + JWT-iat-Vergleich in
  PermissionResolverService.isTokenValid, vorher isUserActive) statt
  erst nach bis zu 15 Minuten
- E-Mail-Aenderung verlangt jetzt aktuelles Passwort
- POST /profile/password und /profile/mfa/disable haben jetzt das
  gleiche strikte Throttling wie Login (SENSITIVE_ACTION_THROTTLE)
- Kein stiller leerer Verschluesselungs-Key-Fallback mehr (wirft
  jetzt explizit)

Bewusst zurueckgestellt (dokumentiert, kein aktiver Exploit-Pfad):
dedizierter Verschluesselungs-Key fuer MFA-Secrets statt
JWT_ACCESS_SECRET-Ableitung, dedizierte users:mfa-reset-Permission
statt users:update, separate JWT-Secrets pro Token-Zweck.

Verifiziert: tsc/eslint clean, 297/297 Backend- + 31/31
Frontend-Unit-Tests, Build clean auf beiden Projekten. e2e weiterhin
durch bekanntes WSL2/Postgres-Problem blockiert. Unabhaengiges
Security- und Code-Quality-Review durchgefuehrt (0 CRITICAL, 0 HIGH).
This commit is contained in:
Nick Szaunig committed 2026-08-27 00:25:34 +02:00
1 parent 6776bdfe73
commit 62004483cf
55 files changed
+2876 -129

No files matched your search

@@ -52,4 +52,10 @@ export class UsersController {
activate(@Param('id') id: string, @CurrentUser() actor: AuthenticatedUser) {
return this.usersService.activate(id, actor.id);
}
@RequirePermission('users:update')
@Post(':id/mfa/reset')
resetMfa(@Param('id') id: string, @CurrentUser() actor: AuthenticatedUser) {
return this.usersService.resetMfa(id, actor.id);
}
}
@@ -280,6 +280,40 @@ describe('UsersService', () => {
expect(permissionResolver.invalidateUser).not.toHaveBeenCalled();
});
it('resetMfa clears mfaEnabled/mfaSecret and records a MFA_DISABLED audit entry', async () => {
prisma.user.findUnique.mockResolvedValue(existingUser);
prisma.user.update.mockResolvedValue({
...existingUser,
mfaEnabled: false,
mfaSecret: null,
});
await usersService.resetMfa(existingUser.id, adminUser.id);
expect(prisma.user.update).toHaveBeenCalledWith(
expect.objectContaining({
where: { id: existingUser.id },
data: { mfaEnabled: false, mfaSecret: null },
}),
);
expect(auditLog.record).toHaveBeenCalledWith(
expect.objectContaining({
entityType: 'User',
entityId: existingUser.id,
action: 'MFA_DISABLED',
userId: adminUser.id,
}),
);
});
it('resetMfa throws NotFoundException when the user does not exist', async () => {
prisma.user.findUnique.mockResolvedValue(null);
await expect(
usersService.resetMfa('missing', adminUser.id),
).rejects.toBeInstanceOf(NotFoundException);
expect(prisma.user.update).not.toHaveBeenCalled();
});
it('update throws ConflictException when reassigning the last active user away from a system role', async () => {
prisma.user.findUnique.mockResolvedValue(adminUser);
prisma.role.findUnique.mockResolvedValue(staffRole);
@@ -29,6 +29,7 @@ export interface UserSummary {
isActive: boolean;
createdAt: Date;
deactivatedAt: Date | null;
mfaEnabled: boolean;
}
@Injectable()
@@ -50,6 +51,7 @@ export class UsersService {
isActive: user.isActive,
createdAt: user.createdAt,
deactivatedAt: user.deactivatedAt,
mfaEnabled: user.mfaEnabled,
};
}
@@ -200,6 +202,23 @@ export class UsersService {
return this.toSummary(user);
}
/** Recovery path for a user who lost their MFA device (no email-reset flow exists in this project). */
async resetMfa(id: string, actingUserId?: string): Promise<UserSummary> {
await this.findById(id);
const user = await this.prisma.user.update({
where: { id },
data: { mfaEnabled: false, mfaSecret: null },
...USER_WITH_ROLE,
});
await this.auditLog.record({
entityType: 'User',
entityId: id,
action: 'MFA_DISABLED',
userId: actingUserId,
});
return this.toSummary(user);
}
async activate(id: string, actingUserId?: string): Promise<UserSummary> {
const before = await this.findById(id);
if (before.isActive) {