Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add helpers to check if we need to disable the global guard or not #630

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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';

Check warning on line 2 in libs/nestjs/core/src/helpers/global-guard.helper.ts

View check run for this annotation

Codecov / codecov/patch

libs/nestjs/core/src/helpers/global-guard.helper.ts#L2

Added line #L2 was not covered by tests
import { Reflector } from '@nestjs/core';

export function hasUnderlyingGuards(

Check warning on line 5 in libs/nestjs/core/src/helpers/global-guard.helper.ts

View check run for this annotation

Codecov / codecov/patch

libs/nestjs/core/src/helpers/global-guard.helper.ts#L5

Added line #L5 was not covered by tests
context: ExecutionContext,
reflector: Reflector,

Check warning on line 7 in libs/nestjs/core/src/helpers/global-guard.helper.ts

View check run for this annotation

Codecov / codecov/patch

libs/nestjs/core/src/helpers/global-guard.helper.ts#L7

Added line #L7 was not covered by tests
) {
const useGuardOverriding = reflector.getAllAndOverride(GUARDS_METADATA, [

Check warning on line 9 in libs/nestjs/core/src/helpers/global-guard.helper.ts

View check run for this annotation

Codecov / codecov/patch

libs/nestjs/core/src/helpers/global-guard.helper.ts#L9

Added line #L9 was not covered by tests
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;

Check warning on line 15 in libs/nestjs/core/src/helpers/global-guard.helper.ts

View check run for this annotation

Codecov / codecov/patch

libs/nestjs/core/src/helpers/global-guard.helper.ts#L15

Added line #L15 was not covered by tests

return false;

Check warning on line 17 in libs/nestjs/core/src/helpers/global-guard.helper.ts

View check run for this annotation

Codecov / codecov/patch

libs/nestjs/core/src/helpers/global-guard.helper.ts#L17

Added line #L17 was not covered by tests
}

export function isRabbitMQ(context: ExecutionContext) {
const contextType: string = context.getType();

Check warning on line 21 in libs/nestjs/core/src/helpers/global-guard.helper.ts

View check run for this annotation

Codecov / codecov/patch

libs/nestjs/core/src/helpers/global-guard.helper.ts#L20-L21

Added lines #L20 - L21 were not covered by tests

// Skip the guard for rabbitmq requests
if (contextType === 'rmq') return true;

Check warning on line 24 in libs/nestjs/core/src/helpers/global-guard.helper.ts

View check run for this annotation

Codecov / codecov/patch

libs/nestjs/core/src/helpers/global-guard.helper.ts#L24

Added line #L24 was not covered by tests

return false;

Check warning on line 26 in libs/nestjs/core/src/helpers/global-guard.helper.ts

View check run for this annotation

Codecov / codecov/patch

libs/nestjs/core/src/helpers/global-guard.helper.ts#L26

Added line #L26 was not covered by tests
}

/**
* 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(

Check warning on line 39 in libs/nestjs/core/src/helpers/global-guard.helper.ts

View check run for this annotation

Codecov / codecov/patch

libs/nestjs/core/src/helpers/global-guard.helper.ts#L39

Added line #L39 was not covered by tests
context: ExecutionContext,
reflector: Reflector,

Check warning on line 41 in libs/nestjs/core/src/helpers/global-guard.helper.ts

View check run for this annotation

Codecov / codecov/patch

libs/nestjs/core/src/helpers/global-guard.helper.ts#L41

Added line #L41 was not covered by tests
) {
return isRabbitMQ(context) || hasUnderlyingGuards(context, reflector);

Check warning on line 43 in libs/nestjs/core/src/helpers/global-guard.helper.ts

View check run for this annotation

Codecov / codecov/patch

libs/nestjs/core/src/helpers/global-guard.helper.ts#L43

Added line #L43 was not covered by tests
}
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
Loading