add more logging
This commit is contained in:
parent
e87222d3c1
commit
2465b8966b
|
|
@ -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<string>('REALM');
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
|
|
@ -25,14 +29,14 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
|||
});
|
||||
}
|
||||
|
||||
async validate(payload: any): Promise<JwtUser> {
|
||||
console.log('JWT Payload:', payload); // Debugging: JWT Payload anzeigen
|
||||
async validate(payload: JwtPayload): Promise<JwtUser> {
|
||||
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 };
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue