Skip to content

Commit

Permalink
P4ADEV-1101 refetored according to revision changes
Browse files Browse the repository at this point in the history
  • Loading branch information
macacia committed Oct 8, 2024
1 parent 358e145 commit fc6146a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public ClientDTO registerClient(String clientName, String organizationIpaCode) {

@Override
public String getClientSecret(String organizationIpaCode, String clientId) {
byte[] clientSecret = clientRetrieverService.getClientSecret(organizationIpaCode, clientId);
return dataCipherService.decrypt(clientSecret);
return clientRetrieverService.getClientSecret(organizationIpaCode, clientId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@
import it.gov.pagopa.payhub.auth.exception.custom.ClientNotFoundException;
import it.gov.pagopa.payhub.auth.model.Client;
import it.gov.pagopa.payhub.auth.repository.ClientRepository;
import it.gov.pagopa.payhub.auth.service.DataCipherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ClientRetrieverService {

private final DataCipherService dataCipherService;

private final ClientRepository clientRepository;

public ClientRetrieverService(ClientRepository clientRepository) {
public ClientRetrieverService(DataCipherService dataCipherService, ClientRepository clientRepository) {
this.dataCipherService = dataCipherService;
this.clientRepository = clientRepository;
}

public byte[] getClientSecret(String organizationIpaCode, String clientId) {
public String getClientSecret(String organizationIpaCode, String clientId) {
return clientRepository.findById(clientId)
.filter(client -> client.getOrganizationIpaCode().equals(organizationIpaCode))
.map(Client::getClientSecret)
.map(dataCipherService::decrypt)
.orElseThrow(() -> new ClientNotFoundException("Client not found"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import java.util.List;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.doReturn;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
Expand Down Expand Up @@ -313,4 +317,34 @@ void givenRequestUnauthorizedWhenGetUserInfoThenException() throws Exception {
).andExpect(status().isUnauthorized());
}
//end region

@Test
void givenAuthorizedUserWhenGetClientSecretThenOk() throws Exception {
String uuidRandomForClientSecret = UUID.randomUUID().toString();
String organizationIpaCode = "IPA_TEST_2";
String clientId = "CLIENTID";

UserInfo expectedUser = UserInfo.builder()
.userId("USERID")
.organizationAccess(organizationIpaCode)
.organizations(List.of(UserOrganizationRoles.builder()
.organizationIpaCode(organizationIpaCode)
.roles(List.of(Constants.ROLE_ADMIN))
.build()))
.build();

Mockito.when(authnServiceMock.getUserInfo("accessToken"))
.thenReturn(expectedUser);

doReturn(uuidRandomForClientSecret)
.when(authzServiceMock).getClientSecret(organizationIpaCode, clientId);

MvcResult result = mockMvc.perform(
get("/payhub/auth/clients/{organizationIpaCode}/{clientId}", organizationIpaCode, clientId)
.header(HttpHeaders.AUTHORIZATION, "Bearer accessToken")
).andExpect(status().isOk())
.andReturn();

assertEquals(uuidRandomForClientSecret, result.getResponse().getContentAsString());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package it.gov.pagopa.payhub.auth.service.a2a.retrieve;

import it.gov.pagopa.payhub.auth.exception.custom.ClientNotFoundException;
import it.gov.pagopa.payhub.auth.exception.custom.UserNotFoundException;
import it.gov.pagopa.payhub.auth.model.Client;
import it.gov.pagopa.payhub.auth.repository.ClientRepository;
import it.gov.pagopa.payhub.auth.service.DataCipherService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -18,26 +21,47 @@ class ClientRetrieverServiceTest {
@Mock
private ClientRepository clientRepositoryMock;

@Mock
private DataCipherService dataCipherService;

@InjectMocks
private ClientRetrieverService service;

@Test
void givenGetClientIdWhenReturnStoredClientSecretThenInvokeClientRetrieverService(){
void givenGetClientIdWhenDecryptThenInvokeClientRetrieverService(){
// Given
String organizationIpaCode = "organizationIpaCode";
String clientName = "clientName";
String clientId = organizationIpaCode + clientName;
byte[] encryptedClientSecret = new byte[16];
new Random().nextBytes(encryptedClientSecret);
Client storedClient = new Client(clientId, clientName, organizationIpaCode, encryptedClientSecret);
String expectedClientSecretPlain = "expectedClientSecretPlain";

Mockito.when(clientRepositoryMock.findById(clientId)).thenReturn(Optional.of(storedClient));
Mockito.when(dataCipherService.decrypt(encryptedClientSecret)).thenReturn(expectedClientSecretPlain);

// When
byte[] result = service.getClientSecret(organizationIpaCode, clientId);
String result = service.getClientSecret(organizationIpaCode, clientId);

// Then
Assertions.assertSame(encryptedClientSecret, result);
Assertions.assertSame(expectedClientSecretPlain, result);
}

@Test
void givenNotClientIdWhenDecryptThenClientNotFoundException(){
// Given
String organizationIpaCode = "organizationIpaCode";
String clientName = "clientName";
String clientId = organizationIpaCode + clientName;
byte[] encryptedClientSecret = new byte[16];
new Random().nextBytes(encryptedClientSecret);
Client storedClient = new Client(clientId, clientName, organizationIpaCode, encryptedClientSecret);

Mockito.when(clientRepositoryMock.findById(storedClient.getClientId())).thenReturn(Optional.empty());

// When, Then
Assertions.assertThrows(ClientNotFoundException.class, () -> dataCipherService.decrypt(encryptedClientSecret));
}

}

0 comments on commit fc6146a

Please sign in to comment.