swagger configurado na rota chavenfe
This commit is contained in:
@@ -1,24 +1,43 @@
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
// ✅ jwt.strategy.ts
|
||||
import { Inject, Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||||
import { JwtPayload } from '../models/jwt-payload.model';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
import { UserRepository } from '../../auth/users/UserRepository';
|
||||
import { RedisClientToken } from '../../core/configs/cache/redis-client.adapter.provider';
|
||||
import { IRedisClient } from '../../core/configs/cache/IRedisClient';
|
||||
|
||||
@Injectable()
|
||||
export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
constructor(private readonly authService: AuthService) {
|
||||
constructor(
|
||||
@Inject(RedisClientToken) private readonly redis: IRedisClient,
|
||||
private readonly userRepository: UserRepository,
|
||||
) {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
secretOrKey: '4557C0D7-DFB0-40DA-BF83-91A75103F7A9',
|
||||
secretOrKey: '4557C0D7-DFB0-40DA-BF83-91A75103F7A9',
|
||||
});
|
||||
}
|
||||
|
||||
async validate(payload: JwtPayload) {
|
||||
const user = await this.authService.validateUser(payload);
|
||||
if (!user) {
|
||||
throw new UnauthorizedException();
|
||||
const sessionKey = `session:${payload.id}`;
|
||||
const user = await this.redis.get<any>(sessionKey);
|
||||
|
||||
if (user) {
|
||||
// Audit log placeholder
|
||||
// await this.auditAccess(user);
|
||||
return user;
|
||||
}
|
||||
return user;
|
||||
|
||||
const userDb = await this.userRepository.findById(payload.id);
|
||||
if (!userDb || userDb.situacao === 'I' || userDb.dataDesligamento) {
|
||||
throw new UnauthorizedException('Usuário inválido ou inativo');
|
||||
}
|
||||
|
||||
await this.redis.set(sessionKey, userDb, 60 * 60 * 8);
|
||||
// Audit fallback
|
||||
// await this.auditAccess(userDb, 'fallback');
|
||||
|
||||
return userDb;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user