From 22c27dd67098bbd6a5738efd0640bd108f0f2647 Mon Sep 17 00:00:00 2001 From: Matt Schoch Date: Fri, 19 Jan 2024 16:16:55 -0500 Subject: [PATCH] Adding nonce into AuthZRequest tpye --- apps/authz/src/app/app.controller.ts | 5 +++- apps/authz/src/app/app.module.ts | 12 +++++++-- apps/authz/src/app/evaluation-request.dto.ts | 9 ++++++- apps/authz/src/main.ts | 26 ++++++++++++++++--- .../shared/module/persistence/mock_data.ts | 1 + apps/authz/src/shared/types/domain.type.ts | 1 + 6 files changed, 46 insertions(+), 8 deletions(-) diff --git a/apps/authz/src/app/app.controller.ts b/apps/authz/src/app/app.controller.ts index 4bbd8cca8..a705cf203 100644 --- a/apps/authz/src/app/app.controller.ts +++ b/apps/authz/src/app/app.controller.ts @@ -54,6 +54,9 @@ export class AppController { result }) - return result + return { + request: fakeRequest, + result, + } } } diff --git a/apps/authz/src/app/app.module.ts b/apps/authz/src/app/app.module.ts index 9691e5414..74a39cbe5 100644 --- a/apps/authz/src/app/app.module.ts +++ b/apps/authz/src/app/app.module.ts @@ -1,10 +1,11 @@ import { PersistenceModule } from '@app/authz/shared/module/persistence/persistence.module' -import { Logger, Module, OnApplicationBootstrap } from '@nestjs/common' +import { Logger, Module, OnApplicationBootstrap, ValidationPipe } from '@nestjs/common' import { ConfigModule } from '@nestjs/config' import { load } from './app.config' import { AppController } from './app.controller' import { AppService } from './app.service' import { OpaService } from './opa/opa.service' +import { APP_PIPE } from '@nestjs/core' @Module({ imports: [ @@ -14,7 +15,14 @@ import { OpaService } from './opa/opa.service' PersistenceModule ], controllers: [AppController], - providers: [AppService, OpaService] + providers: [ + AppService, + OpaService, + { + provide: APP_PIPE, + useClass: ValidationPipe + } + ] }) export class AppModule implements OnApplicationBootstrap { private logger = new Logger(AppModule.name) diff --git a/apps/authz/src/app/evaluation-request.dto.ts b/apps/authz/src/app/evaluation-request.dto.ts index 641da5e1a..547c1d448 100644 --- a/apps/authz/src/app/evaluation-request.dto.ts +++ b/apps/authz/src/app/evaluation-request.dto.ts @@ -25,6 +25,11 @@ export class BaseRequestDataDto { enum: Action }) action: Action + + @IsString() + @IsDefined() + @ApiProperty() + nonce: string } export class TransactionRequestDto { @@ -142,7 +147,9 @@ export class EvaluationRequestDto { @ValidateNested() @Type((opts) => { - return opts?.object.action === Action.SIGN_TRANSACTION ? TransactionRequestDto : SignMessageRequestDataDto + return opts?.object.request.action === Action.SIGN_TRANSACTION + ? SignTransactionRequestDataDto + : SignMessageRequestDataDto }) @IsDefined() @ApiProperty({ diff --git a/apps/authz/src/main.ts b/apps/authz/src/main.ts index be0b172c1..0ff4945ea 100644 --- a/apps/authz/src/main.ts +++ b/apps/authz/src/main.ts @@ -1,19 +1,37 @@ -import { Logger } from '@nestjs/common' +import { INestApplication, Logger, ValidationPipe } from '@nestjs/common' import { ConfigService } from '@nestjs/config' import { NestFactory } from '@nestjs/core' import { AppModule } from './app/app.module' +import { lastValueFrom, map, of, switchMap } from 'rxjs' + +/** + * Adds global pipes to the application. + * + * @param app - The INestApplication instance. + * @returns The modified INestApplication instance. + */ +const withGlobalPipes = (app: INestApplication): INestApplication => { + app.useGlobalPipes(new ValidationPipe()) + + return app +} async function bootstrap() { const logger = new Logger('AuthorizationNodeBootstrap') - const app = await NestFactory.create(AppModule) - const configService = app.get(ConfigService) + const application = await NestFactory.create(AppModule) + const configService = application.get(ConfigService) const port = configService.get('PORT') if (!port) { throw new Error('Missing PORT environment variable') } - await app.listen(port) + await lastValueFrom( + of(application).pipe( + map(withGlobalPipes), + switchMap((app) => app.listen(port)) + ) + ) logger.log(`AuthZ is running on port ${port}`) } diff --git a/apps/authz/src/shared/module/persistence/mock_data.ts b/apps/authz/src/shared/module/persistence/mock_data.ts index 3d268c166..8bd932213 100644 --- a/apps/authz/src/shared/module/persistence/mock_data.ts +++ b/apps/authz/src/shared/module/persistence/mock_data.ts @@ -312,6 +312,7 @@ export const generateInboundRequest = async (): Promise => const txRequest = ERC20_TRANSFER_TX_REQUEST const request = { action: Action.SIGN_TRANSACTION, + nonce: 'random-nonce-111', transactionRequest: txRequest, resourceId: TREASURY_WALLET_X.uid } diff --git a/apps/authz/src/shared/types/domain.type.ts b/apps/authz/src/shared/types/domain.type.ts index e05d9f313..8b15ad263 100644 --- a/apps/authz/src/shared/types/domain.type.ts +++ b/apps/authz/src/shared/types/domain.type.ts @@ -65,6 +65,7 @@ export type HistoricalTransfer = { */ export type AuthZRequest = { action: Action + nonce: string // A unique nonce for this request, to prevent replay attacks resourceId?: string transactionRequest?: TransactionRequest // for signTransaction message?: string // for signMessage