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

fix: ORV2-2532 Proceed to Route Handler if one of the guard validates successfully #1460

Merged
merged 1 commit into from
Jul 5, 2024
Merged
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
25 changes: 25 additions & 0 deletions dops/src/guard/jwt-one-of-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Observable } from 'rxjs';
import { JwtAuthGuard } from './auth.guard';
import { JwtServiceAccountAuthGuard } from './jwt-sa-auth.guard';
import { Reflector } from '@nestjs/core';

@Injectable()
export class JwtOneOfAuthGuard implements CanActivate {
constructor(private reflector: Reflector) {}
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
const jwtAuthGuard = new JwtAuthGuard(this.reflector);
const jwtServiceAccountAuthGuard = new JwtServiceAccountAuthGuard(
this.reflector,
);

try {
return jwtAuthGuard.canActivate(context); // Attempt to activate the jwtAuthGuard
} catch (error) {
// If the jwtAuthGuard fails, attempt to activate the jwtServiceAccountAuthGuard
return jwtServiceAccountAuthGuard.canActivate(context);
}
}
}
5 changes: 2 additions & 3 deletions dops/src/modules/dgen/dgen.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ import { Role } from '../../enum/roles.enum';
import { CreateGeneratedReportDto } from './dto/request/create-generated-report.dto';
import { DgenService } from './dgen.service';
import { ReadFileDto } from '../common/dto/response/read-file.dto';
import { JwtAuthGuard } from 'src/guard/auth.guard';
import { JwtServiceAccountAuthGuard } from 'src/guard/jwt-sa-auth.guard';
import { JwtOneOfAuthGuard } from '../../guard/jwt-one-of-auth.guard';

@ApiTags('Document Generator (DGEN)')
@ApiBadRequestResponse({
Expand Down Expand Up @@ -64,7 +63,7 @@ export class DgenController {
description: 'Required when IDP is not IDIR .',
})
@Roles(Role.GENERATE_DOCUMENT)
@UseGuards(JwtAuthGuard, JwtServiceAccountAuthGuard)
@UseGuards(JwtOneOfAuthGuard)
@Post('/template/render')
async generate(
@Req() request: Request,
Expand Down
5 changes: 2 additions & 3 deletions dops/src/modules/notification/notification.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import { NotificationService } from './notification.service';
import { ExceptionDto } from '../../exception/exception.dto';
import { NotificationDocumentDto } from './dto/request/notification-document.dto';
import { NotificationDto } from './dto/request/notification.dto';
import { JwtAuthGuard } from 'src/guard/auth.guard';
import { JwtServiceAccountAuthGuard } from 'src/guard/jwt-sa-auth.guard';
import { JwtOneOfAuthGuard } from '../../guard/jwt-one-of-auth.guard';

@ApiBearerAuth()
@ApiBadRequestResponse({
Expand Down Expand Up @@ -61,7 +60,7 @@ export class NotificationController {
description:
'Processes and sends an notification with specified documents as attachments to the given recipient(s), and returns a transaction ID for the operation.',
})
@UseGuards(JwtAuthGuard, JwtServiceAccountAuthGuard)
@UseGuards(JwtOneOfAuthGuard)
@Post('/document')
@Roles({ allOf: [Role.SEND_NOTIFICATION, Role.READ_DOCUMENT] })
async notificationWithDocumentsFromDops(
Expand Down
25 changes: 25 additions & 0 deletions vehicles/src/common/guard/jwt-one-of-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Observable } from 'rxjs';
import { JwtAuthGuard } from './auth.guard';
import { JwtServiceAccountAuthGuard } from './jwt-sa-auth.guard';
import { Reflector } from '@nestjs/core';

@Injectable()
export class JwtOneOfAuthGuard implements CanActivate {
constructor(private reflector: Reflector) {}
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
const jwtAuthGuard = new JwtAuthGuard(this.reflector);
const jwtServiceAccountAuthGuard = new JwtServiceAccountAuthGuard(
this.reflector,
);

try {
return jwtAuthGuard.canActivate(context); // Attempt to activate the jwtAuthGuard
} catch (error) {
// If the jwtAuthGuard fails, attempt to activate the jwtServiceAccountAuthGuard
return jwtServiceAccountAuthGuard.canActivate(context);
}
}
}