Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 3 additions & 13 deletions libs/nestjs/authentication/src/guards/jwt-global-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -14,18 +15,7 @@ export class JwtGlobalAuthGuard extends JwtAuthGuard {
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
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);
Expand Down
13 changes: 7 additions & 6 deletions libs/nestjs/casl/src/guards/policies-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -23,17 +27,14 @@ export class PoliciesGuard implements CanActivate {
async canActivate<U extends User = MinimalUser>(
context: ExecutionContext,
): Promise<boolean> {
if (shouldSkipGlobalGuard(context, this.reflector)) return true;

const policyHandlers =
this.reflector.get<PolicyHandlerType<unknown>[]>(
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);

Expand Down
44 changes: 44 additions & 0 deletions libs/nestjs/core/src/helpers/global-guard.helper.ts
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions libs/nestjs/core/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down