feat: adiciona testes e melhorias de segurança
- Adiciona testes para auth service (createToken, createTokenPair, logout, refreshAccessToken) - Adiciona testes para rate-limiting guard - Adiciona testes para jwt strategy - Remove arquivos SDK obsoletos - Melhora validações e tratamento de erros em vários serviços
This commit is contained in:
@@ -26,22 +26,19 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
}
|
||||
|
||||
async validate(payload: JwtPayload, req: any) {
|
||||
if (!payload?.id || !payload?.sessionId) {
|
||||
throw new UnauthorizedException('Payload inválido ou incompleto');
|
||||
}
|
||||
|
||||
const token = req.headers?.authorization?.replace('Bearer ', '');
|
||||
if (token && await this.tokenBlacklistService.isBlacklisted(token)) {
|
||||
throw new UnauthorizedException('Token foi invalidado');
|
||||
}
|
||||
|
||||
/**
|
||||
* Usa a mesma chave que o SessionManagementService
|
||||
* Formato: auth:sessions:userId:sessionId
|
||||
*/
|
||||
const sessionKey = this.buildSessionKey(payload.id, payload.sessionId);
|
||||
const cachedUser = await this.redis.get<any>(sessionKey);
|
||||
|
||||
if (cachedUser) {
|
||||
/**
|
||||
* Verifica se a sessão ainda está ativa
|
||||
*/
|
||||
const isSessionActive = await this.sessionManagementService.isSessionActive(
|
||||
payload.id,
|
||||
payload.sessionId
|
||||
@@ -55,7 +52,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
id: cachedUser.id,
|
||||
sellerId: cachedUser.sellerId,
|
||||
storeId: cachedUser.storeId,
|
||||
username: cachedUser.username, // ← Corrigido: usar username em vez de name
|
||||
username: cachedUser.username,
|
||||
email: cachedUser.email,
|
||||
name: cachedUser.name,
|
||||
sessionId: payload.sessionId,
|
||||
@@ -67,9 +64,6 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
throw new UnauthorizedException('Usuário inválido ou inativo');
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifica se usuário está bloqueado (consistência com AuthenticateUserHandler)
|
||||
*/
|
||||
if (user.situacao === 'B') {
|
||||
throw new UnauthorizedException('Usuário bloqueado, acesso não permitido');
|
||||
}
|
||||
@@ -78,7 +72,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
id: user.id,
|
||||
sellerId: user.sellerId,
|
||||
storeId: user.storeId,
|
||||
username: user.name, // ← Manter name como username para compatibilidade
|
||||
username: user.name,
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
sessionId: payload.sessionId,
|
||||
@@ -89,12 +83,6 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
return userData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constrói a chave de sessão no mesmo formato do SessionManagementService
|
||||
* @param userId ID do usuário
|
||||
* @param sessionId ID da sessão
|
||||
* @returns Chave para o Redis
|
||||
*/
|
||||
private buildSessionKey(userId: number, sessionId: string): string {
|
||||
return `auth:sessions:${userId}:${sessionId}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user