From 87bc149ea147b170fc35f5e81a2aef50f2ec3e31 Mon Sep 17 00:00:00 2001 From: "antonio.torre" Date: Wed, 8 Jan 2025 16:12:54 +0100 Subject: [PATCH] fix auth dependency --- .../service/auth/AuthorizationService.java | 50 ---------- .../auth/AuthorizationServiceTest.java | 95 ------------------- 2 files changed, 145 deletions(-) delete mode 100644 src/main/java/it/gov/pagopa/payhub/pdnd/service/auth/AuthorizationService.java delete mode 100644 src/test/java/it/gov/pagopa/payhub/pdnd/service/auth/AuthorizationServiceTest.java diff --git a/src/main/java/it/gov/pagopa/payhub/pdnd/service/auth/AuthorizationService.java b/src/main/java/it/gov/pagopa/payhub/pdnd/service/auth/AuthorizationService.java deleted file mode 100644 index d4a77c5..0000000 --- a/src/main/java/it/gov/pagopa/payhub/pdnd/service/auth/AuthorizationService.java +++ /dev/null @@ -1,50 +0,0 @@ -package it.gov.pagopa.payhub.pdnd.service.auth; - -import it.gov.pagopa.payhub.pdnd.dto.auth.UserInfoDTO; -import it.gov.pagopa.payhub.pdnd.exception.custom.InvalidAccessTokenException; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.web.client.RestTemplateBuilder; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Service; -import org.springframework.web.client.HttpStatusCodeException; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.DefaultUriBuilderFactory; - -@Service -@Slf4j -public class AuthorizationService { - - private final RestTemplate restTemplate; - - public AuthorizationService(@Value("${app.auth.base-url}") String authServerBaseUrl, - RestTemplateBuilder restTemplateBuilder) { - DefaultUriBuilderFactory ubf = new DefaultUriBuilderFactory(authServerBaseUrl); - ubf.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.VALUES_ONLY); - this.restTemplate = restTemplateBuilder - .uriTemplateHandler(ubf) - .build(); - } - - public UserInfoDTO validateToken(String accessToken){ - log.info("Requesting validate token"); - try{ - HttpHeaders headers = new HttpHeaders(); - headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken); - return restTemplate.exchange("/auth/userinfo", HttpMethod.GET, new HttpEntity<>(headers), UserInfoDTO.class).getBody(); - } catch (HttpStatusCodeException ex){ - String errorMessage; - if(HttpStatus.UNAUTHORIZED.equals(ex.getStatusCode())){ - errorMessage="Bad Access Token provided"; - log.info(errorMessage); - } else { - errorMessage="Something gone wrong while validate token"; - log.error(errorMessage, ex); - } - throw new InvalidAccessTokenException(errorMessage); - } - } -} diff --git a/src/test/java/it/gov/pagopa/payhub/pdnd/service/auth/AuthorizationServiceTest.java b/src/test/java/it/gov/pagopa/payhub/pdnd/service/auth/AuthorizationServiceTest.java deleted file mode 100644 index df3e0b7..0000000 --- a/src/test/java/it/gov/pagopa/payhub/pdnd/service/auth/AuthorizationServiceTest.java +++ /dev/null @@ -1,95 +0,0 @@ -package it.gov.pagopa.payhub.pdnd.service.auth; - -import com.github.tomakehurst.wiremock.WireMockServer; -import com.maciejwalkowiak.wiremock.spring.ConfigureWireMock; -import com.maciejwalkowiak.wiremock.spring.EnableWireMock; -import com.maciejwalkowiak.wiremock.spring.InjectWireMock; - -import it.gov.pagopa.payhub.pdnd.dto.auth.UserInfoDTO; -import it.gov.pagopa.payhub.pdnd.dto.auth.UserOrganizationRolesDTO; -import it.gov.pagopa.payhub.pdnd.exception.custom.InvalidAccessTokenException; -import java.util.List; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.boot.web.client.RestTemplateBuilder; -import org.springframework.context.annotation.Bean; - -@SpringBootTest( - classes = {AuthorizationService.class, AuthorizationServiceTest.AuthorizationServiceTestConfiguration.class}, - webEnvironment = SpringBootTest.WebEnvironment.NONE) -@EnableWireMock({ - @ConfigureWireMock(name = "auth-server", properties = "app.auth.base-url") -}) -@EnableConfigurationProperties -class AuthorizationServiceTest { - - @Autowired - private AuthorizationService authorizationService; - - @InjectWireMock(value = "auth-server") - private WireMockServer wireMockServer; - - @TestConfiguration - public static class AuthorizationServiceTestConfiguration { - @Bean - public RestTemplateBuilder restTemplateBuilder() { - return new RestTemplateBuilder(); - } - } - - @Test - void givenValidAccessTokenWhenValidateTokenThenOk() { - // When - UserInfoDTO result = authorizationService.validateToken("ACCESSTOKEN"); - - // Then - Assertions.assertEquals( - UserInfoDTO.builder() - .userId("e1d9c534-86a9-4039-80da-8aa7a33ac9e7") - .fiscalCode("DMEMPY15L21L736U") - .familyName("demo") - .name("demo") - .issuer("https://dev.selfcare.pagopa.it") - .organizationAccess("SELC_99999999990") - .organizations(List.of(UserOrganizationRolesDTO.builder() - .operatorId("133e9c1b-dfc5-43ea-98a7-f64f30613074") - .organizationIpaCode("SELC_99999999990") - .roles(List.of("ROLE_ADMIN")) - .build())) - .build(), - result - ); - } - - @Test - void givenInvalidAccessTokenWhenValidateTokenThenInvalidAccessTokenException() { - // When - InvalidAccessTokenException result = Assertions.assertThrows(InvalidAccessTokenException.class, - () -> authorizationService.validateToken("INVALIDACCESSTOKEN")); - - // Then - Assertions.assertEquals( - "Bad Access Token provided", - result.getMessage() - ); - } - - @Test - void givenUnexpectedErrorWhenValidateTokenThenIDInvalidAccessTokenException() { - // When - InvalidAccessTokenException result = Assertions.assertThrows(InvalidAccessTokenException.class, - () -> authorizationService.validateToken("EXCEPTION")); - - // Then - Assertions.assertEquals( - "Something gone wrong while validate token", - result.getMessage() - ); - } - - -} \ No newline at end of file