add more logging

This commit is contained in:
Andreas Knuth 2024-05-28 14:41:26 -05:00
parent e87222d3c1
commit 2465b8966b
2 changed files with 20 additions and 8 deletions

View File

@ -1,13 +1,17 @@
import { Injectable, UnauthorizedException } from '@nestjs/common'; import { Inject, Injectable, UnauthorizedException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { PassportStrategy } from '@nestjs/passport'; import { PassportStrategy } from '@nestjs/passport';
import { passportJwtSecret } from 'jwks-rsa'; import { passportJwtSecret } from 'jwks-rsa';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { ExtractJwt, Strategy } from 'passport-jwt'; import { ExtractJwt, Strategy } from 'passport-jwt';
import { JwtUser } from './models/main.model'; import { Logger } from 'winston';
import { JwtPayload, JwtUser } from './models/main.model';
@Injectable() @Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) { export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(configService: ConfigService) { constructor(
configService: ConfigService,
@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
) {
const realm = configService.get<string>('REALM'); const realm = configService.get<string>('REALM');
super({ super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
@ -25,14 +29,14 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
}); });
} }
async validate(payload: any): Promise<JwtUser> { async validate(payload: JwtPayload): Promise<JwtUser> {
console.log('JWT Payload:', payload); // Debugging: JWT Payload anzeigen this.logger.info('JWT Payload:', payload); // Debugging: JWT Payload anzeigen
if (!payload) { if (!payload) {
console.error('Invalid payload'); this.logger.error('Invalid payload');
throw new UnauthorizedException(); throw new UnauthorizedException();
} }
if (!payload.sub || !payload.preferred_username) { if (!payload.sub || !payload.preferred_username) {
console.error('Missing required claims'); this.logger.error('Missing required claims');
throw new UnauthorizedException(); throw new UnauthorizedException();
} }
return { userId: payload.sub, username: payload.preferred_username, roles: payload.realm_access?.roles }; return { userId: payload.sub, username: payload.preferred_username, roles: payload.realm_access?.roles };

View File

@ -135,6 +135,14 @@ export interface JwtToken {
email: string; email: string;
user_id: string; user_id: string;
} }
export interface JwtPayload {
sub: string;
preferred_username: string;
realm_access?: {
roles?: string[];
};
[key: string]: any; // für andere optionale Felder im JWT-Payload
}
interface Resourceaccess { interface Resourceaccess {
account: Realmaccess; account: Realmaccess;
} }