From 0aa9c065a5398d6443f5626763b94f25a6ada7fe Mon Sep 17 00:00:00 2001 From: marcos-silva-rodrigues Date: Mon, 16 Jun 2025 10:34:11 -0300 Subject: [PATCH] FEAT: block user from login when maestro is dedicated --- src/authentication/authentication.guard.ts | 13 +++++++++++++ src/modules/auth/auth.service.ts | 22 +++++++++++++++------- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/authentication/authentication.guard.ts b/src/authentication/authentication.guard.ts index bc69f8c..a41bc74 100644 --- a/src/authentication/authentication.guard.ts +++ b/src/authentication/authentication.guard.ts @@ -4,6 +4,7 @@ import { OnApplicationBootstrap, ExecutionContext, Inject, + ForbiddenException, } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; import assert from 'assert'; @@ -113,6 +114,18 @@ export class AuthenticationGuard return false; } + // Bloquear outros customer de usar o maestor dedicado + const DEDICATED_PROXY = process.env.DEDICATED_PROXY + if (DEDICATED_PROXY !== '' && DEDICATED_PROXY !== accessTokenPayload.customer_id) { + throw new ForbiddenException(); + } + + // Bloquear o customer de acesso o maestro publico + const hasNetworkPolicyModule = accessTokenPayload.customer_modules.includes('network-policy'); + if (hasNetworkPolicyModule && DEDICATED_PROXY === '') { + throw new ForbiddenException(); + } + request.accessTokenPayload = accessTokenPayload; request.user = { user_id: accessTokenPayload.user_id, diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index be71d66..a8ceedd 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -54,14 +54,22 @@ export class AuthClientService implements OnModuleInit { return lastValueFrom(this.authService.AuthSnowflakeSignIn(input)); } - checkDedicatedProxy(customerId: string) { + checkDedicatedProxy({ + customer + }: AuthSignInResponse) { const DEDICATED_PROXY = process.env.DEDICATED_PROXY || ''; this.logger.info('SignIn - Setting customer ID for dedicated proxy: ' + DEDICATED_PROXY); - if (DEDICATED_PROXY !== '') { - this.logger.info('Customer ID: ' + customerId); - if (DEDICATED_PROXY !== customerId) { - throw new ForbiddenException(); - } + this.logger.info('Customer ID: ' + customer.id); + + if (DEDICATED_PROXY !== '' && DEDICATED_PROXY !== customer.id) { + throw new ForbiddenException(); + } + + // Bloquear o customer de acesso o maestro publico + this.logger.info('Check if customer have network policy: ' + customer.modules); + const hasNetworkPolicyModule = customer.modules.includes('network-policy'); + if (hasNetworkPolicyModule && DEDICATED_PROXY === '') { + throw new ForbiddenException(); } } @@ -85,7 +93,7 @@ export class AuthClientService implements OnModuleInit { } if (result.customer) { - this.checkDedicatedProxy(result.customer.id); + this.checkDedicatedProxy(result); } return result