Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: P4ADEV-1303-modify-GetUserInfo-to-handle-system-user #105

Merged
merged 10 commits into from
Oct 25, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import it.gov.pagopa.payhub.auth.mapper.ClientMapper;
import it.gov.pagopa.payhub.model.generated.ClientDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AuthorizeClientCredentialsRequestService {
private static final String PIATTAFORMA_UNITARIA_CLIENT_ID_PREFIX = "piattaforma-unitaria_";
private static final String PIATTAFORMA_UNITARIA = "piattaforma-unitaria";
private static final String SEPARATOR = "_";
private final ClientService clientService;
private final ClientMapper clientMapper;
private final String piattaformaUnitariaClientSecret;
Expand All @@ -25,7 +27,7 @@ public AuthorizeClientCredentialsRequestService(
}

public ClientDTO authorizeCredentials(String clientId, String clientSecret) {
if (clientId.startsWith(PIATTAFORMA_UNITARIA_CLIENT_ID_PREFIX)) {
if (clientId.startsWith(PIATTAFORMA_UNITARIA + SEPARATOR)) {
macacia marked this conversation as resolved.
Show resolved Hide resolved
return authorizePiattaformaUnitariaCredentials(clientId, clientSecret);
}
return authorizeSilCredentials(clientId, clientSecret);
Expand All @@ -41,11 +43,10 @@ private ClientDTO authorizeSilCredentials(String clientId, String clientSecret)
private ClientDTO authorizePiattaformaUnitariaCredentials(String clientId, String clientSecret) {
if (!clientSecret.equals(piattaformaUnitariaClientSecret))
throw new ClientUnauthorizedException("Unauthorized client for piattaforma-unitaria client-credentials");
String[] splittedClientId = clientId.split("_");
return ClientDTO.builder()
.clientId(clientId)
.clientName(splittedClientId[0])
.organizationIpaCode(splittedClientId[1])
.clientName(PIATTAFORMA_UNITARIA)
.organizationIpaCode(StringUtils.substringAfter(clientId, PIATTAFORMA_UNITARIA + SEPARATOR))
macacia marked this conversation as resolved.
Show resolved Hide resolved
.clientSecret(clientSecret)
.build();
}
Expand Down
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import it.gov.pagopa.payhub.auth.model.User;
import it.gov.pagopa.payhub.auth.repository.OperatorsRepository;
import it.gov.pagopa.payhub.auth.repository.UsersRepository;
import it.gov.pagopa.payhub.auth.utils.Constants;
import it.gov.pagopa.payhub.model.generated.UserInfo;
import it.gov.pagopa.payhub.model.generated.UserOrganizationRoles;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;

Expand All @@ -27,26 +29,48 @@ public IamUserInfoDTO2UserInfoMapper(UsersRepository usersRepository, OperatorsR

@Override
public UserInfo apply(IamUserInfoDTO iamUserInfoDTO) {
if (iamUserInfoDTO.isSystemUser()) {
return systemUserMapper(iamUserInfoDTO);
}
return userInfoMapper(iamUserInfoDTO);
}

private UserInfo systemUserMapper(IamUserInfoDTO iamUserInfoDTO) {
return UserInfo.builder()
.userId(iamUserInfoDTO.getUserId())
.mappedExternalUserId(iamUserInfoDTO.getFiscalCode())
macacia marked this conversation as resolved.
Show resolved Hide resolved
.fiscalCode(iamUserInfoDTO.getFiscalCode())
.familyName(iamUserInfoDTO.getFamilyName())
.name(iamUserInfoDTO.getName())
.issuer(iamUserInfoDTO.getIssuer())
.organizations(Collections.singletonList(UserOrganizationRoles.builder()
.organizationIpaCode(iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode())
.roles(Collections.singletonList(Constants.ROLE_ADMIN))
.build()))
.build();
}

private UserInfo userInfoMapper(IamUserInfoDTO iamUserInfoDTO) {
User user = usersRepository.findById(iamUserInfoDTO.getInnerUserId()).orElseThrow(() -> new UserNotFoundException("Cannot found user having inner id:" + iamUserInfoDTO.getInnerUserId()));
List<Operator> userRoles = operatorsRepository.findAllByUserId(iamUserInfoDTO.getInnerUserId());
UserInfo userInfo = UserInfo.builder()
.userId(user.getUserId())
.mappedExternalUserId(user.getMappedExternalUserId())
.fiscalCode(iamUserInfoDTO.getFiscalCode())
.familyName(iamUserInfoDTO.getFamilyName())
.name(iamUserInfoDTO.getName())
.issuer(iamUserInfoDTO.getIssuer())
.organizations(userRoles.stream()
.map(r -> UserOrganizationRoles.builder()
.operatorId(r.getOperatorId())
.organizationIpaCode(r.getOrganizationIpaCode())
.roles(new ArrayList<>(r.getRoles()))
.email(r.getEmail())
.build())
.toList())
.build();

if(iamUserInfoDTO.getOrganizationAccess()!=null){
.userId(user.getUserId())
.mappedExternalUserId(user.getMappedExternalUserId())
.fiscalCode(iamUserInfoDTO.getFiscalCode())
.familyName(iamUserInfoDTO.getFamilyName())
.name(iamUserInfoDTO.getName())
.issuer(iamUserInfoDTO.getIssuer())
.organizations(userRoles.stream()
.map(r -> UserOrganizationRoles.builder()
.operatorId(r.getOperatorId())
.organizationIpaCode(r.getOrganizationIpaCode())
.roles(new ArrayList<>(r.getRoles()))
.email(r.getEmail())
.build())
.toList())
.build();

if(iamUserInfoDTO.getOrganizationAccess() != null){
userInfo.setOrganizationAccess(iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode());
}
return userInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -93,16 +94,16 @@ void givenSystemUserWhenMatcherThenAssertionOk() {
// Given
String clientId = "piattaforma-unitaria_IPA_TEST";
String clientSecret = "SECRET";
String[] splitted = clientId.split("_");

String piattaformaUnitaria = "piattaforma-unitaria";
String separator = "_";
// When
ClientDTO actualClientDTO = service.authorizeCredentials(clientId, clientSecret);
// Then
Assertions.assertEquals(
ClientDTO.builder()
.clientId(clientId)
.clientName(splitted[0])
.organizationIpaCode(splitted[1])
.clientName(piattaformaUnitaria)
.organizationIpaCode(StringUtils.substringAfter(clientId, piattaformaUnitaria + separator))
.clientSecret(clientSecret)
.build(), actualClientDTO);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import it.gov.pagopa.payhub.auth.model.User;
import it.gov.pagopa.payhub.auth.repository.OperatorsRepository;
import it.gov.pagopa.payhub.auth.repository.UsersRepository;
import it.gov.pagopa.payhub.auth.utils.Constants;
import it.gov.pagopa.payhub.model.generated.UserInfo;
import it.gov.pagopa.payhub.model.generated.UserOrganizationRoles;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -136,33 +137,67 @@ void givenNoOrganizationAccessWhenApplyThenOk(){
testApplyOk(iamUserInfo, user, organizationRoles);
}

@Test
void givenSystemUserWhenApplyThenOk(){
// Given
IamUserInfoDTO iamUserInfo = IamUserInfoDTO.builder()
.systemUser(Boolean.TRUE)
.userId("EXTERNALUSERID")
.innerUserId("INNERUSERID")
.fiscalCode("FISCALCODE")
.familyName("FAMILYNAME")
.name("NAME")
.issuer("ISSUER")
.organizationAccess(IamUserOrganizationRolesDTO.builder()
.organizationIpaCode("IPA_CODE")
.roles(Collections.singletonList(Constants.ROLE_ADMIN))
.build())
.build();

testApplyOk(iamUserInfo, null, null);
}

private void testApplyOk(IamUserInfoDTO iamUserInfo, User user, List<Operator> organizationRoles) {
Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.of(user));
Mockito.when(operatorsRepositoryMock.findAllByUserId(user.getUserId())).thenReturn(organizationRoles);

UserInfo userInfo;
if (iamUserInfo.isSystemUser()) {
userInfo = UserInfo.builder()
.userId(iamUserInfo.getUserId())
.mappedExternalUserId(iamUserInfo.getFiscalCode())
.fiscalCode(iamUserInfo.getFiscalCode())
.familyName(iamUserInfo.getFamilyName())
.name(iamUserInfo.getName())
.issuer(iamUserInfo.getIssuer())
.organizations(Collections.singletonList(UserOrganizationRoles.builder()
.organizationIpaCode(iamUserInfo.getOrganizationAccess().getOrganizationIpaCode())
.roles(Collections.singletonList(Constants.ROLE_ADMIN))
.build()))
.build();
} else {
Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.of(user));
Mockito.when(operatorsRepositoryMock.findAllByUserId(user.getUserId())).thenReturn(organizationRoles);
userInfo = UserInfo.builder()
.userId(user.getUserId())
.mappedExternalUserId(user.getMappedExternalUserId())
.fiscalCode(iamUserInfo.getFiscalCode())
.familyName(iamUserInfo.getFamilyName())
.name(iamUserInfo.getName())
.issuer(iamUserInfo.getIssuer())
.organizationAccess(iamUserInfo.getOrganizationAccess()!=null? iamUserInfo.getOrganizationAccess().getOrganizationIpaCode(): null)
.organizations(organizationRoles.stream()
.map(r -> UserOrganizationRoles.builder()
.operatorId(r.getOperatorId())
.organizationIpaCode(r.getOrganizationIpaCode())
.roles(new ArrayList<>(r.getRoles()))
.email(r.getEmail())
.build())
.toList())
.build();
macacia marked this conversation as resolved.
Show resolved Hide resolved
}
// When
UserInfo result = mapper.apply(iamUserInfo);

// Then
Assertions.assertEquals(
UserInfo.builder()
.userId(user.getUserId())
.mappedExternalUserId(user.getMappedExternalUserId())
.fiscalCode(iamUserInfo.getFiscalCode())
.familyName(iamUserInfo.getFamilyName())
.name(iamUserInfo.getName())
.issuer(iamUserInfo.getIssuer())
.organizationAccess(iamUserInfo.getOrganizationAccess()!=null? iamUserInfo.getOrganizationAccess().getOrganizationIpaCode(): null)
.organizations(organizationRoles.stream()
.map(r -> UserOrganizationRoles.builder()
.operatorId(r.getOperatorId())
.organizationIpaCode(r.getOrganizationIpaCode())
.roles(new ArrayList<>(r.getRoles()))
.email(r.getEmail())
.build())
.toList())
.build(),
result
);
Assertions.assertEquals(userInfo, result);
}
}