Skip to content

Commit

Permalink
P4ADEV-1275 resolved change request
Browse files Browse the repository at this point in the history
  • Loading branch information
macacia committed Oct 18, 2024
1 parent c9fdd9b commit 572af93
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 113 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ public interface ClientService {
String getClientSecret(String organizationIpaCode, String clientId);
List<ClientNoSecretDTO> getClients(String organizationIpaCode);
Optional<Client> getClientByClientId(String clientId);
ClientDTO authorizeCredentials(String clientId, String clientSecret);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,4 @@ public Optional<Client> 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"));
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
}

Expand Down

0 comments on commit 572af93

Please sign in to comment.