diff --git a/openapi/p4pa-auth.openapi.yaml b/openapi/p4pa-auth.openapi.yaml index 728f46aa..c3e81420 100644 --- a/openapi/p4pa-auth.openapi.yaml +++ b/openapi/p4pa-auth.openapi.yaml @@ -120,6 +120,8 @@ paths: description: OK '400': description: Invalid request + '401': + description: Invalid client_id security: - BearerAuth: [] components: diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/exchange/ValidateExternalTokenService.java b/src/main/java/it/gov/pagopa/payhub/auth/service/exchange/ValidateExternalTokenService.java index 718ff10f..9da9cf28 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/exchange/ValidateExternalTokenService.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/exchange/ValidateExternalTokenService.java @@ -43,7 +43,7 @@ public Map validate(String clientId, String grantType, String sub return claims; } - private void validateClient(String clientId) { + public void validateClient(String clientId) { if (!ALLOWED_CLIENT_ID.equals(clientId)){ throw new InvalidExchangeClientException("Invalid clientId " + clientId); } diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/logout/LogoutServiceImpl.java b/src/main/java/it/gov/pagopa/payhub/auth/service/logout/LogoutServiceImpl.java index 47478774..2de6d519 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/logout/LogoutServiceImpl.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/logout/LogoutServiceImpl.java @@ -1,11 +1,23 @@ package it.gov.pagopa.payhub.auth.service.logout; +import it.gov.pagopa.payhub.auth.service.TokenStoreService; +import it.gov.pagopa.payhub.auth.service.exchange.ValidateExternalTokenService; import org.springframework.stereotype.Service; @Service public class LogoutServiceImpl implements LogoutService { + + private final ValidateExternalTokenService validateExternalTokenService; + private final TokenStoreService tokenStoreService; + + public LogoutServiceImpl(ValidateExternalTokenService validateExternalTokenService, TokenStoreService tokenStoreService) { + this.validateExternalTokenService = validateExternalTokenService; + this.tokenStoreService = tokenStoreService; + } + @Override public void logout(String clientId, String token) { - // TODO + validateExternalTokenService.validateClient(clientId); + tokenStoreService.delete(token); } } diff --git a/src/test/java/it/gov/pagopa/payhub/auth/controller/AuthControllerTest.java b/src/test/java/it/gov/pagopa/payhub/auth/controller/AuthControllerTest.java index c6673724..4c27c078 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/controller/AuthControllerTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/controller/AuthControllerTest.java @@ -165,16 +165,30 @@ void givenNoClientIdWhenLogoutThenBadRequest() throws Exception { } @Test - void givenCompleteRequestWhenLogoutThenOk() throws Exception { + void givenInvalidClientIdWhenLogoutThenBadRequest() throws Exception { + mockMvc.perform( + post("/payhub/auth/revoke") + .param("token", "token") + ).andExpect(status().isBadRequest()); + } + + @Test + void givenCompleteRequestWhenLogoutThenInvalidClientError() throws Exception { String clientId = "CLIENTID"; String token = "TOKEN"; - mockMvc.perform( + Mockito.doThrow(new InvalidExchangeClientException("")) + .when(authServiceMock).logout(clientId, token); + + MvcResult result = mockMvc.perform( post("/payhub/auth/revoke") .param("client_id", clientId) .param("token", token) - ).andExpect(status().isOk()); + ).andExpect(status().isUnauthorized()).andReturn(); - Mockito.verify(authServiceMock).logout(clientId, token); + AuthErrorDTO actual = objectMapper.readValue(result.getResponse().getContentAsString(), + AuthErrorDTO.class); + assertEquals(AuthErrorDTO.ErrorEnum.INVALID_CLIENT, actual.getError()); + assertEquals("", actual.getErrorDescription()); } } diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/TokenStoreServiceTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/TokenStoreServiceTest.java index 5d806f40..c30684f8 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/TokenStoreServiceTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/TokenStoreServiceTest.java @@ -22,7 +22,7 @@ void givenClaimsWhenSaveThenReturnThem(){ } @Test - void givenAccessTokenWhenSaveThenNull(){ + void givenAccessTokenWhenLoadThenNull(){ // Given String accessToken = "AccessToken"; diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/logout/LogoutServiceTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/logout/LogoutServiceTest.java new file mode 100644 index 00000000..c29cf4d8 --- /dev/null +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/logout/LogoutServiceTest.java @@ -0,0 +1,57 @@ +package it.gov.pagopa.payhub.auth.service.logout; + +import it.gov.pagopa.payhub.auth.exception.custom.InvalidExchangeClientException; +import it.gov.pagopa.payhub.auth.service.TokenStoreService; +import it.gov.pagopa.payhub.auth.service.exchange.ValidateExternalTokenService; +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; + +@ExtendWith(MockitoExtension.class) +class LogoutServiceTest { + + @Mock + private ValidateExternalTokenService validateExternalTokenServiceMock; + @Mock + private TokenStoreService tokenStoreServiceMock; + + private LogoutService service; + + @BeforeEach + void init(){ + service = new LogoutServiceImpl(validateExternalTokenServiceMock, tokenStoreServiceMock); + } + + @Test + void givenInvalidClientIdWhenLogoutThenInvalidClientException(){ + // Given + String clientId = "clientId"; + String token = "token"; + + InvalidExchangeClientException expectedException = new InvalidExchangeClientException(""); + Mockito.doThrow(expectedException) + .when(validateExternalTokenServiceMock).validateClient(clientId); + + // When, Then + InvalidExchangeClientException exception = Assertions.assertThrows(InvalidExchangeClientException.class, () -> service.logout(clientId, token)); + Assertions.assertSame(expectedException, exception); + } + + @Test + void givenCompleteRequestWhenLogoutThenOk(){ + // Given + String clientId = "clientId"; + String token = "token"; + + // When + service.logout(clientId, token); + + // Then + Mockito.verify(validateExternalTokenServiceMock).validateClient(clientId); + Mockito.verify(tokenStoreServiceMock).delete(token); + } +}