Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions libs/user/src/lib/auths/guard/hybrid-jwt.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
CanActivate,
ExecutionContext,
Injectable,
Logger,
UnauthorizedException,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
Expand All @@ -23,6 +24,12 @@ import { getSecret } from '../../utils/config.utils';
*/
@Injectable()
export class HybridJwtGuard implements CanActivate {
/**
* Logger for audit logging of service impersonation events.
* Tracks both successful and failed impersonation attempts for security monitoring.
*/
private readonly logger = new Logger(HybridJwtGuard.name);

constructor(
private jwtService: JwtService,
private prisma: PrismaService,
Expand Down Expand Up @@ -78,13 +85,21 @@ export class HybridJwtGuard implements CanActivate {

if (impersonateId) {
if (!serviceClient.canImpersonate) {
// Log failed impersonation attempt
this.logger.warn(
`Service impersonation denied - ${payload.serviceName}: service not allowed to impersonate`,
);
throw new UnauthorizedException(
'This service is not allowed to impersonate users',
);
}

const user = await this.loadUserById(impersonateId);
if (!user) {
// Log failed impersonation attempt
this.logger.warn(
`Service impersonation denied - ${payload.serviceName}: user not found`,
);
throw new UnauthorizedException('Impersonated user not found');
}

Expand All @@ -101,12 +116,21 @@ export class HybridJwtGuard implements CanActivate {
);

if (!canImpersonate) {
// Log failed impersonation attempt
this.logger.warn(
`Service impersonation denied - ${payload.serviceName}: user ${user.uuid} roles not allowed`,
);
throw new UnauthorizedException(
'Service not allowed to impersonate users with these roles',
);
}
}

// Log successful impersonation
this.logger.log(
`Service impersonation granted - ${payload.serviceName}: user ${user.uuid} with roles ${userRoleNames.join(',')}`,
);

request.user = {
id: user.id,
userId: user.id,
Expand Down