From 572af93c9f996eeee1e673a264cc96eb839151ce Mon Sep 17 00:00:00 2001 From: macacia Date: Fri, 18 Oct 2024 17:32:52 +0200 Subject: [PATCH] P4ADEV-1275 resolved change request --- ...horizeClientCredentialsRequestService.java | 11 ++- .../auth/service/a2a/ClientService.java | 1 - .../auth/service/a2a/ClientServiceImpl.java | 7 -- .../a2a/ValidateClientCredentialsService.java | 6 +- ...zeClientCredentialsRequestServiceTest.java | 36 -------- ...zeClientCredentialsRequestServiceTest.java | 89 +++++++++++++++++++ .../a2a/ClientCredentialsServiceTest.java | 6 +- .../auth/service/a2a/ClientServiceTest.java | 58 ------------ .../ValidateClientCredentialsServiceTest.java | 6 +- 9 files changed, 107 insertions(+), 113 deletions(-) delete mode 100644 src/test/java/it/gov/pagopa/payhub/auth/service/AuthorizeClientCredentialsRequestServiceTest.java create mode 100644 src/test/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestServiceTest.java diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java index bd85e5ab..fedb0424 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java @@ -1,5 +1,7 @@ package it.gov.pagopa.payhub.auth.service.a2a; +import it.gov.pagopa.payhub.auth.exception.custom.ClientUnauthorizedException; +import it.gov.pagopa.payhub.auth.mapper.ClientMapper; import it.gov.pagopa.payhub.model.generated.ClientDTO; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -8,12 +10,17 @@ @Slf4j public class AuthorizeClientCredentialsRequestService { private final ClientService clientService; + private final ClientMapper clientMapper; - public AuthorizeClientCredentialsRequestService(ClientService clientService) { + public AuthorizeClientCredentialsRequestService(ClientService clientService, ClientMapper clientMapper) { this.clientService = clientService; + this.clientMapper = clientMapper; } public ClientDTO authorizeCredentials(String clientId, String clientSecret) { - return clientService.authorizeCredentials(clientId, clientSecret); + return clientService.getClientByClientId(clientId) + .map(clientMapper::mapToDTO) + .filter(dto -> dto.getClientSecret().equals(clientSecret)) + .orElseThrow(() -> new ClientUnauthorizedException("Unauthorized client for client-credentials")); } } diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ClientService.java b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ClientService.java index 67ae3156..1abb977a 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ClientService.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ClientService.java @@ -13,5 +13,4 @@ public interface ClientService { String getClientSecret(String organizationIpaCode, String clientId); List getClients(String organizationIpaCode); Optional getClientByClientId(String clientId); - ClientDTO authorizeCredentials(String clientId, String clientSecret); } diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ClientServiceImpl.java b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ClientServiceImpl.java index 2adb344d..da66b623 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ClientServiceImpl.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ClientServiceImpl.java @@ -50,11 +50,4 @@ public Optional getClientByClientId(String clientId) { return clientRetrieverService.getClientByClientId(clientId); } - public ClientDTO authorizeCredentials(String clientId, String clientSecret) { - return getClientByClientId(clientId) - .map(clientMapper::mapToDTO) - .filter(dto -> dto.getClientSecret().equals(clientSecret)) - .orElseThrow(() -> new ClientUnauthorizedException("Unauthorized client for client-credentials")); - } - } diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ValidateClientCredentialsService.java b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ValidateClientCredentialsService.java index 6d46042f..fd6960c0 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ValidateClientCredentialsService.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ValidateClientCredentialsService.java @@ -1,6 +1,6 @@ package it.gov.pagopa.payhub.auth.service.a2a; -import it.gov.pagopa.payhub.auth.exception.custom.ClientUnauthorizedException; +import it.gov.pagopa.payhub.auth.exception.custom.InvalidExchangeRequestException; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @@ -19,13 +19,13 @@ public void validate(String scope, String clientSecret) { private void validateProtocolConfiguration(String scope) { if (!ALLOWED_SCOPE.equals(scope)){ - throw new ClientUnauthorizedException("Invalid scope " + scope); + throw new InvalidExchangeRequestException("Invalid scope " + scope); } } private void validateClientSecret(String clientSecret) { if (!StringUtils.hasText(clientSecret)) { - throw new ClientUnauthorizedException("clientSecret is mandatory with client-credentials grant type"); + throw new InvalidExchangeRequestException("clientSecret is mandatory with client-credentials grant type"); } } diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/AuthorizeClientCredentialsRequestServiceTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/AuthorizeClientCredentialsRequestServiceTest.java deleted file mode 100644 index 080dfcd5..00000000 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/AuthorizeClientCredentialsRequestServiceTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package it.gov.pagopa.payhub.auth.service; - -import it.gov.pagopa.payhub.auth.service.a2a.AuthorizeClientCredentialsRequestService; -import it.gov.pagopa.payhub.auth.service.a2a.ClientService; -import it.gov.pagopa.payhub.model.generated.ClientDTO; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mock; -import org.mockito.Mockito; - -class AuthorizeClientCredentialsRequestServiceTest { - - @Mock - private ClientService clientService; - private AuthorizeClientCredentialsRequestService service; - - @BeforeEach - void init() { - service = new AuthorizeClientCredentialsRequestService(clientService); - } - - @Test - void givenValidTokenWhenPostTokenThenSuccess(){ - // Given - String clientId="CLIENT_ID"; - String clientSecret="CLIENT_SECRET"; - ClientDTO expectedClientDTO = new ClientDTO(); - - Mockito.doReturn(expectedClientDTO).when(clientService).authorizeCredentials(clientId, clientSecret); - //When - ClientDTO result = service.authorizeCredentials(clientId, clientSecret); - //Then - Assertions.assertEquals(expectedClientDTO, result); - } -} diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestServiceTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestServiceTest.java new file mode 100644 index 00000000..8628a961 --- /dev/null +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestServiceTest.java @@ -0,0 +1,89 @@ +package it.gov.pagopa.payhub.auth.service.a2a; + +import it.gov.pagopa.payhub.auth.exception.custom.ClientUnauthorizedException; +import it.gov.pagopa.payhub.auth.mapper.ClientMapper; +import it.gov.pagopa.payhub.auth.model.Client; +import it.gov.pagopa.payhub.model.generated.ClientDTO; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Optional; +import java.util.UUID; + +@ExtendWith(MockitoExtension.class) +class AuthorizeClientCredentialsRequestServiceTest { + + @Mock + private ClientService clientServiceMock; + @Mock + private ClientMapper clientMapperMock; + private AuthorizeClientCredentialsRequestService service; + + @BeforeEach + void init() { + service = new AuthorizeClientCredentialsRequestService(clientServiceMock, clientMapperMock); + } + + @Test + void givenRightCredentialsWhenVerifyCredentialsThenOk() { + // Given + String organizationIpaCode = "IPA_TEST_2"; + String clientName = "SERVICE_001"; + String clientId = organizationIpaCode + clientName; + String clientSecretMock = UUID.randomUUID().toString(); + + Client mockClient = new Client(); + ClientDTO expectedClientDTO = ClientDTO.builder() + .clientId(clientId) + .clientName(clientName) + .organizationIpaCode(organizationIpaCode) + .clientSecret(clientSecretMock) + .build(); + + Mockito.when(clientServiceMock.getClientByClientId(clientId)).thenReturn(Optional.of(mockClient)); + Mockito.when(clientMapperMock.mapToDTO(mockClient)).thenReturn(expectedClientDTO); + // When + ClientDTO actualClientDTO = service.authorizeCredentials(clientId, clientSecretMock); + // Then + Assertions.assertEquals(expectedClientDTO, actualClientDTO); + } + + @Test + void givenUnexpectedClientIdCredentialsWhenVerifyCredentialsThenClientUnauthorizedException() { + // Given + String clientId = "UNEXPECTED_CLIENT_ID"; + String clientSecretMock = UUID.randomUUID().toString(); + + Mockito.when(clientServiceMock.getClientByClientId(clientId)).thenThrow(new ClientUnauthorizedException("error")); + // When, Then + Assertions.assertThrows(ClientUnauthorizedException.class, () -> service.authorizeCredentials(clientId, clientSecretMock)); + } + + @Test + void givenUnexpectedClientSecretCredentialsWhenVerifyCredentialsThenClientUnauthorizedException() { + // Given + String organizationIpaCode = "IPA_TEST_2"; + String clientName = "SERVICE_001"; + String clientId = organizationIpaCode + clientName; + String clientSecret = UUID.randomUUID().toString(); + + Client mockClient = new Client(); + ClientDTO expectedClientDTO = ClientDTO.builder() + .clientId(clientId) + .clientName(clientName) + .organizationIpaCode(organizationIpaCode) + .clientSecret(UUID.randomUUID().toString()) + .build(); + + Mockito.when(clientServiceMock.getClientByClientId(clientId)).thenReturn(Optional.of(mockClient)); + Mockito.when(clientMapperMock.mapToDTO(mockClient)).thenReturn(expectedClientDTO); + + // When, Then + Assertions.assertThrows(ClientUnauthorizedException.class, () -> service.authorizeCredentials(clientId, clientSecret)); + } +} diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/ClientCredentialsServiceTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/ClientCredentialsServiceTest.java index daa2bcdc..1a25c12f 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/ClientCredentialsServiceTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/ClientCredentialsServiceTest.java @@ -16,13 +16,13 @@ class ClientCredentialsServiceTest { @Mock private ValidateClientCredentialsService validateClientCredentialsServiceMock; @Mock - private AuthorizeClientCredentialsRequestService authorizeClientCredentialsRequestService; + private AuthorizeClientCredentialsRequestService authorizeClientCredentialsRequestServiceMock; private ClientCredentialService service; @BeforeEach void init() { - service = new ClientCredentialServiceImpl(validateClientCredentialsServiceMock, authorizeClientCredentialsRequestService); + service = new ClientCredentialServiceImpl(validateClientCredentialsServiceMock, authorizeClientCredentialsRequestServiceMock); } @Test @@ -33,7 +33,7 @@ void givenValidTokenWhenPostTokenThenSuccess(){ String clientSecret="CLIENT_SECRET"; Mockito.doNothing().when(validateClientCredentialsServiceMock).validate(scope, clientSecret); - Mockito.doReturn(new ClientDTO()).when(authorizeClientCredentialsRequestService).authorizeCredentials(clientId, clientSecret); + Mockito.doReturn(new ClientDTO()).when(authorizeClientCredentialsRequestServiceMock).authorizeCredentials(clientId, clientSecret); AccessToken expectedAccessToken = AccessToken.builder().accessToken("accessToken").build(); //When AccessToken result = service.postToken(clientId, scope, clientSecret); diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/ClientServiceTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/ClientServiceTest.java index adbf58b9..4020d07e 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/ClientServiceTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/ClientServiceTest.java @@ -1,6 +1,5 @@ package it.gov.pagopa.payhub.auth.service.a2a; -import it.gov.pagopa.payhub.auth.exception.custom.ClientUnauthorizedException; import it.gov.pagopa.payhub.auth.mapper.ClientMapper; import it.gov.pagopa.payhub.auth.model.Client; import it.gov.pagopa.payhub.auth.service.a2a.registration.ClientRegistrationService; @@ -118,61 +117,4 @@ void givenClientIdWhenGetClientByClientIdThenInvokeClientService() { Assertions.assertEquals(Optional.of(expectedClient), result); } - @Test - void givenRightCredentialsWhenVerifyCredentialsThenOk() { - // Given - String organizationIpaCode = "IPA_TEST_2"; - String clientName = "SERVICE_001"; - String clientId = organizationIpaCode + clientName; - String clientSecretMock = UUID.randomUUID().toString(); - - Client mockClient = new Client(); - ClientDTO expectedClientDTO = ClientDTO.builder() - .clientId(clientId) - .clientName(clientName) - .organizationIpaCode(organizationIpaCode) - .clientSecret(clientSecretMock) - .build(); - - Mockito.when(clientRetrieverServiceMock.getClientByClientId(clientId)).thenReturn(Optional.of(mockClient)); - Mockito.when(clientMapperMock.mapToDTO(mockClient)).thenReturn(expectedClientDTO); - // When - ClientDTO actualClientDTO = service.authorizeCredentials(clientId, clientSecretMock); - // Then - Assertions.assertEquals(expectedClientDTO, actualClientDTO); - } - - @Test - void givenUnexpectedClientIdCredentialsWhenVerifyCredentialsThenClientUnauthorizedException() { - // Given - String clientId = "UNEXPECTED_CLIENT_ID"; - String clientSecretMock = UUID.randomUUID().toString(); - - Mockito.when(clientRetrieverServiceMock.getClientByClientId(clientId)).thenThrow(new ClientUnauthorizedException("error")); - // When, Then - Assertions.assertThrows(ClientUnauthorizedException.class, () -> service.authorizeCredentials(clientId, clientSecretMock)); - } - - @Test - void givenUnexpectedClientSecretCredentialsWhenVerifyCredentialsThenClientUnauthorizedException() { - // Given - String organizationIpaCode = "IPA_TEST_2"; - String clientName = "SERVICE_001"; - String clientId = organizationIpaCode + clientName; - String clientSecret = UUID.randomUUID().toString(); - - Client mockClient = new Client(); - ClientDTO expectedClientDTO = ClientDTO.builder() - .clientId(clientId) - .clientName(clientName) - .organizationIpaCode(organizationIpaCode) - .clientSecret(UUID.randomUUID().toString()) - .build(); - - Mockito.when(clientRetrieverServiceMock.getClientByClientId(clientId)).thenReturn(Optional.of(mockClient)); - Mockito.when(clientMapperMock.mapToDTO(mockClient)).thenReturn(expectedClientDTO); - - // When, Then - Assertions.assertThrows(ClientUnauthorizedException.class, () -> service.authorizeCredentials(clientId, clientSecret)); - } } diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/ValidateClientCredentialsServiceTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/ValidateClientCredentialsServiceTest.java index 6095f20d..e9fb7727 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/ValidateClientCredentialsServiceTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/ValidateClientCredentialsServiceTest.java @@ -1,6 +1,6 @@ package it.gov.pagopa.payhub.auth.service.a2a; -import it.gov.pagopa.payhub.auth.exception.custom.ClientUnauthorizedException; +import it.gov.pagopa.payhub.auth.exception.custom.InvalidExchangeRequestException; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -26,13 +26,13 @@ void givenValidRequestThenOk() { @Test void givenInvalidScopeThenInvalidExchangeRequestException() { - assertThrows(ClientUnauthorizedException.class, () -> + assertThrows(InvalidExchangeRequestException.class, () -> service.validate( "UNEXPECTED_SCOPE", ALLOWED_CLIENT_SECRET)); } @Test void givenNullClientSecretThenInvalidExchangeRequestException() { - assertThrows(ClientUnauthorizedException.class, () -> + assertThrows(InvalidExchangeRequestException.class, () -> service.validate(ValidateClientCredentialsService.ALLOWED_SCOPE, null)); }