diff --git a/libs/nestjs/authentication/src/guards/jwt-global-auth.guard.ts b/libs/nestjs/authentication/src/guards/jwt-global-auth.guard.ts index 2adfedbba..0f9d1eeee 100644 --- a/libs/nestjs/authentication/src/guards/jwt-global-auth.guard.ts +++ b/libs/nestjs/authentication/src/guards/jwt-global-auth.guard.ts @@ -1,10 +1,11 @@ import { ExecutionContext, Injectable } from '@nestjs/common'; -import { GUARDS_METADATA } from '@nestjs/common/constants'; import { Reflector } from '@nestjs/core'; import { Observable } from 'rxjs'; import { JwtAuthGuard } from './jwt-auth.guard'; +import { shouldSkipGlobalGuard } from '@trxn/nestjs-core'; + @Injectable() export class JwtGlobalAuthGuard extends JwtAuthGuard { constructor(protected readonly reflector: Reflector) { @@ -14,18 +15,7 @@ export class JwtGlobalAuthGuard extends JwtAuthGuard { canActivate( context: ExecutionContext, ): boolean | Promise | Observable { - const contextType: string = context.getType(); - - // Skip the guard for rabbitmq requests - if (contextType === 'rmq') return true; - - const useGuardOverriding = this.reflector.getAllAndOverride( - GUARDS_METADATA, - [context.getHandler(), context.getClass()], - ); - - // If we have other guard in the called method we get to them directly - if (useGuardOverriding && useGuardOverriding.length > 0) return true; + if (shouldSkipGlobalGuard(context, this.reflector)) return true; // Check request authentication by using the JwtAuthGuard return super.canActivate(context); diff --git a/libs/nestjs/casl/src/guards/policies-guard.ts b/libs/nestjs/casl/src/guards/policies-guard.ts index 73862cfcd..cffa0e891 100644 --- a/libs/nestjs/casl/src/guards/policies-guard.ts +++ b/libs/nestjs/casl/src/guards/policies-guard.ts @@ -9,7 +9,11 @@ import { ModuleRef, Reflector } from '@nestjs/core'; import { CaslAbilityFactoryService } from '../services'; import { isClass, PolicyHandlerType } from '@trxn/common'; -import { getRequestFromContext, POLICIES_KEY } from '@trxn/nestjs-core'; +import { + getRequestFromContext, + POLICIES_KEY, + shouldSkipGlobalGuard, +} from '@trxn/nestjs-core'; import { MinimalUser, User } from '@trxn/nestjs-user'; @Injectable() @@ -23,17 +27,14 @@ export class PoliciesGuard implements CanActivate { async canActivate( context: ExecutionContext, ): Promise { + if (shouldSkipGlobalGuard(context, this.reflector)) return true; + const policyHandlers = this.reflector.get[]>( POLICIES_KEY, context.getHandler(), ) || []; - const contextType: string = context.getType(); - - // Skip the guard for rabbitmq requests - if (contextType === 'rmq') return true; - // Extract request from the context const req = getRequestFromContext(context); diff --git a/libs/nestjs/core/src/helpers/global-guard.helper.ts b/libs/nestjs/core/src/helpers/global-guard.helper.ts new file mode 100644 index 000000000..a74d5007c --- /dev/null +++ b/libs/nestjs/core/src/helpers/global-guard.helper.ts @@ -0,0 +1,44 @@ +import { ExecutionContext } from '@nestjs/common'; +import { GUARDS_METADATA } from '@nestjs/common/constants'; +import { Reflector } from '@nestjs/core'; + +export function hasUnderlyingGuards( + context: ExecutionContext, + reflector: Reflector, +) { + const useGuardOverriding = reflector.getAllAndOverride(GUARDS_METADATA, [ + context.getHandler(), + context.getClass(), + ]); + + // If we have other guard in the called method we get to them directly + if (useGuardOverriding && useGuardOverriding.length > 0) return true; + + return false; +} + +export function isRabbitMQ(context: ExecutionContext) { + const contextType: string = context.getType(); + + // Skip the guard for rabbitmq requests + if (contextType === 'rmq') return true; + + return false; +} + +/** + * Check if the request should skip the global guard + * + * It return true if the request is a rabbitmq request or if the underlying + * method has guards configured with the @UseGuards() decorator directly on the class + * + * @param context + * @param reflector + * @returns + */ +export function shouldSkipGlobalGuard( + context: ExecutionContext, + reflector: Reflector, +) { + return isRabbitMQ(context) || hasUnderlyingGuards(context, reflector); +} diff --git a/libs/nestjs/core/src/helpers/index.ts b/libs/nestjs/core/src/helpers/index.ts index b02ba6394..38dfa1e26 100644 --- a/libs/nestjs/core/src/helpers/index.ts +++ b/libs/nestjs/core/src/helpers/index.ts @@ -3,6 +3,7 @@ export * from './compress-whitespace'; export * from './format-entity-ids'; export * from './format-filter-type'; export * from './format-sort'; +export * from './global-guard.helper'; export * from './set-extras.helper'; export * from './is-development.helper'; export * from './is-production.helper';