From 2465b8966b2bad518632e9651de70623f9e67e05 Mon Sep 17 00:00:00 2001 From: Andreas Knuth Date: Tue, 28 May 2024 14:41:26 -0500 Subject: [PATCH] add more logging --- bizmatch-server/src/jwt.strategy.ts | 20 ++++++++++++-------- bizmatch-server/src/models/main.model.ts | 8 ++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/bizmatch-server/src/jwt.strategy.ts b/bizmatch-server/src/jwt.strategy.ts index 6115260..58995d6 100644 --- a/bizmatch-server/src/jwt.strategy.ts +++ b/bizmatch-server/src/jwt.strategy.ts @@ -1,13 +1,17 @@ -import { Injectable, UnauthorizedException } from '@nestjs/common'; +import { Inject, Injectable, UnauthorizedException } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { PassportStrategy } from '@nestjs/passport'; import { passportJwtSecret } from 'jwks-rsa'; +import { WINSTON_MODULE_PROVIDER } from 'nest-winston'; import { ExtractJwt, Strategy } from 'passport-jwt'; -import { JwtUser } from './models/main.model'; - +import { Logger } from 'winston'; +import { JwtPayload, JwtUser } from './models/main.model'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { - constructor(configService: ConfigService) { + constructor( + configService: ConfigService, + @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger, + ) { const realm = configService.get('REALM'); super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), @@ -25,14 +29,14 @@ export class JwtStrategy extends PassportStrategy(Strategy) { }); } - async validate(payload: any): Promise { - console.log('JWT Payload:', payload); // Debugging: JWT Payload anzeigen + async validate(payload: JwtPayload): Promise { + this.logger.info('JWT Payload:', payload); // Debugging: JWT Payload anzeigen if (!payload) { - console.error('Invalid payload'); + this.logger.error('Invalid payload'); throw new UnauthorizedException(); } if (!payload.sub || !payload.preferred_username) { - console.error('Missing required claims'); + this.logger.error('Missing required claims'); throw new UnauthorizedException(); } return { userId: payload.sub, username: payload.preferred_username, roles: payload.realm_access?.roles }; diff --git a/bizmatch-server/src/models/main.model.ts b/bizmatch-server/src/models/main.model.ts index 5ce7efb..a3e0ee0 100644 --- a/bizmatch-server/src/models/main.model.ts +++ b/bizmatch-server/src/models/main.model.ts @@ -135,6 +135,14 @@ export interface JwtToken { email: 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 { account: Realmaccess; }