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 { 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 };
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue