mirror of
http://172.20.10.11:3000/gitadmin/INSIGHT-MVP.git
synced 2026-06-25 07:56:39 +02:00
Eigenstaendiger NestJS-Service unter packages/crm-service/ mit: - Prisma Schema (app_crm): Contact, Activity, Pipeline, PipelineStage, Deal - JWT RS256 Auth mit shared Public Key und Token-Revocation - Multi-Tenancy: TenantGuard + tenantId-Filter auf allen Queries - CRUD-Module: Contacts, Activities, Pipelines, Deals - Docker-Integration: docker-compose.crm.yml (Port 3100, Traefik-Route /api/v1/crm) - Health-Check, Swagger, GlobalExceptionFilter, Pagination Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
import {
|
|
Injectable,
|
|
ExecutionContext,
|
|
UnauthorizedException,
|
|
} from '@nestjs/common';
|
|
import { Reflector } from '@nestjs/core';
|
|
import { AuthGuard } from '@nestjs/passport';
|
|
import { IS_PUBLIC_KEY } from '../../common/decorators/public.decorator';
|
|
import { RedisService } from '../../redis/redis.service';
|
|
import { JwtPayload } from '../../common/decorators/current-user.decorator';
|
|
|
|
@Injectable()
|
|
export class JwtAuthGuard extends AuthGuard('jwt') {
|
|
constructor(
|
|
private readonly reflector: Reflector,
|
|
private readonly redis: RedisService,
|
|
) {
|
|
super();
|
|
}
|
|
|
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
|
context.getHandler(),
|
|
context.getClass(),
|
|
]);
|
|
|
|
if (isPublic) {
|
|
return true;
|
|
}
|
|
|
|
const canActivate = await super.canActivate(context);
|
|
if (!canActivate) {
|
|
return false;
|
|
}
|
|
|
|
// Token-Revocation pruefen (Redis Blocklist)
|
|
const request = context.switchToHttp().getRequest();
|
|
const user = request.user as JwtPayload;
|
|
|
|
if (user?.jti) {
|
|
const isBlocked = await this.redis.isTokenBlocked(user.jti);
|
|
if (isBlocked) {
|
|
throw new UnauthorizedException('Token wurde widerrufen');
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
handleRequest<T>(err: Error | null, user: T, info: Error | undefined): T {
|
|
if (err || !user) {
|
|
throw err || new UnauthorizedException('Zugriff verweigert');
|
|
}
|
|
return user;
|
|
}
|
|
}
|