From eeb78a523b3ec3185b2e988a5090e61030f35788 Mon Sep 17 00:00:00 2001 From: Constantin Bergatt Date: Thu, 13 Jun 2024 08:56:56 +0200 Subject: [PATCH] BC-6453 - replace `UnauthorizedException` with `AuthorizationErrorLoggableException` --- .../authorization-client.adapter.spec.ts | 17 ++++++++++------- .../authorization-client.adapter.ts | 14 +++++++------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/apps/server/src/infra/authorization-client/authorization-client.adapter.spec.ts b/apps/server/src/infra/authorization-client/authorization-client.adapter.spec.ts index 6951173a165..7bf0ba5ba81 100644 --- a/apps/server/src/infra/authorization-client/authorization-client.adapter.spec.ts +++ b/apps/server/src/infra/authorization-client/authorization-client.adapter.spec.ts @@ -1,5 +1,4 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest'; -import { UnauthorizedException } from '@nestjs/common'; import { REQUEST } from '@nestjs/core'; import { Test, TestingModule } from '@nestjs/testing'; import { AxiosResponse } from 'axios'; @@ -285,21 +284,22 @@ describe(AuthorizationClientAdapter.name, () => { }); const adapter = new AuthorizationClientAdapter(authorizationApi, request); - return { params, adapter }; + const error = new Error('Authentication is required.'); + + return { params, adapter, error }; }; it('should throw an UnauthorizedException', async () => { - const { params, adapter } = setup(); + const { params, adapter, error } = setup(); - await expect(adapter.hasPermissionsByReference(params)).rejects.toThrowError(UnauthorizedException); + const expectedError = new AuthorizationErrorLoggableException(error, params); + + await expect(adapter.hasPermissionsByReference(params)).rejects.toThrowError(expectedError); }); }); describe('when authorizationReferenceControllerAuthorizeByReference returns error', () => { const setup = () => { - const error = new Error('testError'); - authorizationApi.authorizationReferenceControllerAuthorizeByReference.mockRejectedValueOnce(error); - const params = { context: { action: Action.READ, @@ -309,6 +309,9 @@ describe(AuthorizationClientAdapter.name, () => { referenceId: 'someReferenceId', }; + const error = new Error('testError'); + authorizationApi.authorizationReferenceControllerAuthorizeByReference.mockRejectedValueOnce(error); + return { params, error }; }; diff --git a/apps/server/src/infra/authorization-client/authorization-client.adapter.ts b/apps/server/src/infra/authorization-client/authorization-client.adapter.ts index 3c6e625ddcc..157aec77f34 100644 --- a/apps/server/src/infra/authorization-client/authorization-client.adapter.ts +++ b/apps/server/src/infra/authorization-client/authorization-client.adapter.ts @@ -1,4 +1,4 @@ -import { Inject, Injectable, UnauthorizedException } from '@nestjs/common'; +import { Inject, Injectable } from '@nestjs/common'; import { REQUEST } from '@nestjs/core'; import { RawAxiosRequestConfig } from 'axios'; import cookie from 'cookie'; @@ -19,14 +19,13 @@ export class AuthorizationClientAdapter { } public async hasPermissionsByReference(params: AuthorizationBodyParams): Promise { - const options = this.createOptionParams(); + const options = this.createOptionParams(params); try { const response = await this.authorizationApi.authorizationReferenceControllerAuthorizeByReference( params, options ); - const hasPermission = response.data.isAuthorized; return hasPermission; @@ -35,19 +34,20 @@ export class AuthorizationClientAdapter { } } - private createOptionParams(): RawAxiosRequestConfig { - const jwt = this.getJWT(); + private createOptionParams(params: AuthorizationBodyParams): RawAxiosRequestConfig { + const jwt = this.getJWT(params); const options: RawAxiosRequestConfig = { headers: { authorization: `Bearer ${jwt}` } }; return options; } - private getJWT(): string { + private getJWT(params: AuthorizationBodyParams): string { const getJWT = ExtractJwt.fromExtractors([ExtractJwt.fromAuthHeaderAsBearerToken(), this.fromCookie('jwt')]); const jwt = getJWT(this.request) || this.request.headers.authorization; if (!jwt) { - throw new UnauthorizedException('Authentication is required.'); + const error = new Error('Authentication is required.'); + throw new AuthorizationErrorLoggableException(error, params); } return jwt;