32 lines
1.2 KiB
TypeScript
32 lines
1.2 KiB
TypeScript
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|
import { JwtService } from '@nestjs/jwt';
|
|
import { AuditService } from '../audit/audit.service';
|
|
import { LdapAuthService } from '../lldap/ldap-auth.service';
|
|
|
|
@Injectable()
|
|
export class AuthService {
|
|
constructor(
|
|
private readonly ldapAuth: LdapAuthService,
|
|
private readonly jwt: JwtService,
|
|
private readonly audit: AuditService,
|
|
) {}
|
|
|
|
async login(username: string, password: string, ipAddress?: string, userAgent?: string) {
|
|
const valid = await this.ldapAuth.verifyPassword(username, password);
|
|
if (!valid) {
|
|
await this.audit.record({ type: 'auth.login_failed', username, ipAddress, userAgent });
|
|
throw new UnauthorizedException('Ungültige Zugangsdaten.');
|
|
}
|
|
|
|
await this.audit.record({ type: 'auth.login_success', username, ipAddress, userAgent });
|
|
return {
|
|
accessToken: await this.jwt.signAsync({ sub: username, username }),
|
|
user: { username },
|
|
};
|
|
}
|
|
|
|
async logout(username: string, ipAddress?: string, userAgent?: string): Promise<void> {
|
|
await this.audit.record({ type: 'auth.logout_success', username, ipAddress, userAgent });
|
|
}
|
|
}
|