fix(auth): Rolle, Aktiv-Status und Kennwort-Pflicht je Anfrage aus der Datenbank
JwtStrategy.validate las bisher alles aus dem 30-Tage-Token: ein herabgestufter Administrator behielt seine Rechte bis zum Ablauf, ein deaktiviertes oder geloeschtes Konto arbeitete mit seiner Sitzung weiter, und Oberflaeche (/auth/me aus der DB) und API (Token) sahen verschiedene Rollen – die Benutzerliste scheiterte nach einer Rollenaenderung (Befund des Nutzers auf alpha). Jetzt ein gebundener PK-Lesezugriff je Anfrage (forTenant), 401 bei fehlendem, deaktiviertem oder mandantenfremdem Konto. Lokal nachgewiesen: Herabstufen -> sofort 403, Deaktivieren -> sofort 401. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,16 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { UnauthorizedException } from '@nestjs/common';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { forTenant } from '../../prisma/prisma-tenant.extension';
|
||||
import { JwtStrategy } from './jwt.strategy';
|
||||
|
||||
vi.mock('../../prisma/prisma-tenant.extension', () => ({
|
||||
forTenant: vi.fn((p: unknown) => p),
|
||||
}));
|
||||
|
||||
/**
|
||||
* JwtStrategy.validate — pinnt die Durchreichung von mustChangePassword
|
||||
* (260921-fi3, Aufgabe 1, Befund 1/D-01). Direkte Konstruktion ohne
|
||||
* JwtStrategy.validate — seit quick-260930 kommen Rolle, Aktiv-Status und
|
||||
* Kennwort-Pflicht bei jeder Anfrage aus der Datenbank, nicht aus dem Token
|
||||
* (Rollenaenderung/Deaktivierung wirkt sofort). Direkte Konstruktion ohne
|
||||
* Nest-Testmodul, Muster aus `../../tenant/tenant.guard.spec.ts`.
|
||||
*/
|
||||
|
||||
@@ -11,51 +18,77 @@ function makeConfigService() {
|
||||
return { get: () => 'test-secret' } as any;
|
||||
}
|
||||
|
||||
type Row = {
|
||||
id: string;
|
||||
username: string;
|
||||
role: string;
|
||||
tenantId: string;
|
||||
isActive: boolean;
|
||||
mustChangePassword: boolean;
|
||||
} | null;
|
||||
|
||||
function makePrisma(row: Row) {
|
||||
return { user: { findUnique: vi.fn(async () => row) } } as any;
|
||||
}
|
||||
|
||||
const payload = {
|
||||
sub: 'u1',
|
||||
username: 'kschaller',
|
||||
role: 'SUPER_ADMIN' as const,
|
||||
tenantId: 't1',
|
||||
mustChangePassword: false,
|
||||
};
|
||||
|
||||
const dbRow = {
|
||||
id: 'u1',
|
||||
username: 'kschaller',
|
||||
role: 'ADMIN',
|
||||
tenantId: 't1',
|
||||
isActive: true,
|
||||
mustChangePassword: false,
|
||||
};
|
||||
|
||||
describe('JwtStrategy.validate', () => {
|
||||
it('Anspruch mustChangePassword=true im Token: liefert request.user.mustChangePassword === true', async () => {
|
||||
const strategy = new JwtStrategy(makeConfigService());
|
||||
it('Rolle kommt aus der Datenbank, nicht aus dem Token (herabgestufter Super-Admin ist sofort Admin)', async () => {
|
||||
const prisma = makePrisma(dbRow);
|
||||
const strategy = new JwtStrategy(makeConfigService(), prisma);
|
||||
|
||||
const result = await strategy.validate({
|
||||
sub: 'u1',
|
||||
username: 'admin',
|
||||
role: 'ADMIN',
|
||||
tenantId: 't1',
|
||||
mustChangePassword: true,
|
||||
});
|
||||
|
||||
expect(result.mustChangePassword).toBe(true);
|
||||
});
|
||||
|
||||
it('Anspruch fehlt im Token (Alt-Sitzung, vor dieser Aenderung ausgestellt): liefert false statt undefined', async () => {
|
||||
const strategy = new JwtStrategy(makeConfigService());
|
||||
|
||||
const result = await strategy.validate({
|
||||
sub: 'u1',
|
||||
username: 'admin',
|
||||
role: 'ADMIN',
|
||||
tenantId: 't1',
|
||||
});
|
||||
|
||||
expect(result.mustChangePassword).toBe(false);
|
||||
});
|
||||
|
||||
it('id, username, role und tenantId werden unveraendert wie bisher durchgereicht', async () => {
|
||||
const strategy = new JwtStrategy(makeConfigService());
|
||||
|
||||
const result = await strategy.validate({
|
||||
sub: 'u1',
|
||||
username: 'nutzer1',
|
||||
role: 'USER',
|
||||
tenantId: 't2',
|
||||
mustChangePassword: false,
|
||||
});
|
||||
const result = await strategy.validate(payload);
|
||||
|
||||
expect(result).toEqual({
|
||||
id: 'u1',
|
||||
username: 'nutzer1',
|
||||
role: 'USER',
|
||||
tenantId: 't2',
|
||||
username: 'kschaller',
|
||||
role: 'ADMIN',
|
||||
tenantId: 't1',
|
||||
mustChangePassword: false,
|
||||
});
|
||||
expect(forTenant).toHaveBeenCalledWith(prisma, 't1');
|
||||
expect(prisma.user.findUnique).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ where: { id: 'u1' } }),
|
||||
);
|
||||
});
|
||||
|
||||
it('deaktiviertes Konto: 401, auch mit gueltigem Token', async () => {
|
||||
const strategy = new JwtStrategy(makeConfigService(), makePrisma({ ...dbRow, isActive: false }));
|
||||
await expect(strategy.validate(payload)).rejects.toBeInstanceOf(UnauthorizedException);
|
||||
});
|
||||
|
||||
it('geloeschtes Konto: 401', async () => {
|
||||
const strategy = new JwtStrategy(makeConfigService(), makePrisma(null));
|
||||
await expect(strategy.validate(payload)).rejects.toBeInstanceOf(UnauthorizedException);
|
||||
});
|
||||
|
||||
it('Konto gehoert nicht (mehr) zum Mandanten aus dem Token: 401', async () => {
|
||||
const strategy = new JwtStrategy(makeConfigService(), makePrisma({ ...dbRow, tenantId: 't2' }));
|
||||
await expect(strategy.validate(payload)).rejects.toBeInstanceOf(UnauthorizedException);
|
||||
});
|
||||
|
||||
it('Kennwort-Pflicht kommt aus der Datenbank (vom Administrator nachtraeglich gesetzt)', async () => {
|
||||
const strategy = new JwtStrategy(
|
||||
makeConfigService(),
|
||||
makePrisma({ ...dbRow, mustChangePassword: true }),
|
||||
);
|
||||
const result = await strategy.validate(payload);
|
||||
expect(result.mustChangePassword).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user