diff --git a/src/main/java/it/gov/pagopa/payhub/auth/controller/AuthControllerImpl.java b/src/main/java/it/gov/pagopa/payhub/auth/controller/AuthControllerImpl.java index 72c120a6..f8e6598e 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/controller/AuthControllerImpl.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/controller/AuthControllerImpl.java @@ -38,6 +38,12 @@ public ResponseEntity getUserInfo() { } } + @Override + public ResponseEntity logout(String clientId, String token) { + authService.logout(clientId, token); + return ResponseEntity.ok(null); + } + private static String getAuthorizationHeader() { return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getHeader(HttpHeaders.AUTHORIZATION); } diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/AuthService.java b/src/main/java/it/gov/pagopa/payhub/auth/service/AuthService.java index a12bfe07..bcdbb7af 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/AuthService.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/AuthService.java @@ -7,4 +7,6 @@ public interface AuthService { AccessToken postToken(String clientId, String grantType, String subjectToken, String subjectIssuer, String subjectTokenType, String scope); UserInfo getUserInfo(String accessToken); + void logout(String clientId, String token); + } diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/AuthServiceImpl.java b/src/main/java/it/gov/pagopa/payhub/auth/service/AuthServiceImpl.java index 8085d65b..41a72839 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/AuthServiceImpl.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/AuthServiceImpl.java @@ -1,6 +1,7 @@ package it.gov.pagopa.payhub.auth.service; import it.gov.pagopa.payhub.auth.service.exchange.ExchangeTokenService; +import it.gov.pagopa.payhub.auth.service.logout.LogoutService; import it.gov.pagopa.payhub.auth.service.user.UserService; import it.gov.pagopa.payhub.model.generated.AccessToken; import it.gov.pagopa.payhub.model.generated.UserInfo; @@ -12,10 +13,12 @@ public class AuthServiceImpl implements AuthService{ private final ExchangeTokenService exchangeTokenService; private final UserService userService; + private final LogoutService logoutService; - public AuthServiceImpl(ExchangeTokenService exchangeTokenService, UserService userService) { + public AuthServiceImpl(ExchangeTokenService exchangeTokenService, UserService userService, LogoutService logoutService) { this.exchangeTokenService = exchangeTokenService; this.userService = userService; + this.logoutService = logoutService; } @Override @@ -27,4 +30,9 @@ public AccessToken postToken(String clientId, String grantType, String subjectTo public UserInfo getUserInfo(String accessToken) { return userService.getUserInfo(accessToken); } + + @Override + public void logout(String clientId, String token) { + logoutService.logout(clientId, token); + } } diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/logout/LogoutService.java b/src/main/java/it/gov/pagopa/payhub/auth/service/logout/LogoutService.java new file mode 100644 index 00000000..bdbde420 --- /dev/null +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/logout/LogoutService.java @@ -0,0 +1,5 @@ +package it.gov.pagopa.payhub.auth.service.logout; + +public interface LogoutService { + void logout(String clientId, String token); +} 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 new file mode 100644 index 00000000..47478774 --- /dev/null +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/logout/LogoutServiceImpl.java @@ -0,0 +1,11 @@ +package it.gov.pagopa.payhub.auth.service.logout; + +import org.springframework.stereotype.Service; + +@Service +public class LogoutServiceImpl implements LogoutService { + @Override + public void logout(String clientId, String token) { + // TODO + } +} 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 084a97ac..c6673724 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 @@ -155,4 +155,26 @@ void givenRequestWitInvalidAuthorizationWhenGetUserInfoThenUnauthorized() throws .header(HttpHeaders.AUTHORIZATION, "Bearer accessToken") ).andExpect(status().isUnauthorized()); } + + @Test + void givenNoClientIdWhenLogoutThenBadRequest() throws Exception { + mockMvc.perform( + post("/payhub/auth/revoke") + .param("token", "token") + ).andExpect(status().isBadRequest()); + } + + @Test + void givenCompleteRequestWhenLogoutThenOk() throws Exception { + String clientId = "CLIENTID"; + String token = "TOKEN"; + + mockMvc.perform( + post("/payhub/auth/revoke") + .param("client_id", clientId) + .param("token", token) + ).andExpect(status().isOk()); + + Mockito.verify(authServiceMock).logout(clientId, token); + } } diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/AuthServiceTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/AuthServiceTest.java index 49c5c341..84e4a403 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/AuthServiceTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/AuthServiceTest.java @@ -1,6 +1,7 @@ package it.gov.pagopa.payhub.auth.service; import it.gov.pagopa.payhub.auth.service.exchange.ExchangeTokenService; +import it.gov.pagopa.payhub.auth.service.logout.LogoutService; import it.gov.pagopa.payhub.auth.service.user.UserService; import it.gov.pagopa.payhub.model.generated.AccessToken; import it.gov.pagopa.payhub.model.generated.UserInfo; @@ -20,19 +21,22 @@ class AuthServiceTest { private ExchangeTokenService exchangeTokenServiceMock; @Mock private UserService userServiceMock; + @Mock + private LogoutService logoutServiceMock; private AuthService service; @BeforeEach void init(){ - service = new AuthServiceImpl(exchangeTokenServiceMock, userServiceMock); + service = new AuthServiceImpl(exchangeTokenServiceMock, userServiceMock, logoutServiceMock); } @AfterEach void verifyNotMoreInteractions(){ Mockito.verifyNoMoreInteractions( exchangeTokenServiceMock, - userServiceMock + userServiceMock, + logoutServiceMock ); } @@ -71,4 +75,17 @@ void whenGetUserInfoThenCallUserService(){ // Then Assertions.assertSame(expectedResult, result); } + + @Test + void whenLogoutThenCallLogout(){ + // Given + String clientId = "clientId"; + String accessToken = "accessToken"; + + // When + service.logout(clientId, accessToken); + + // Then + Mockito.verify(logoutServiceMock).logout(clientId, accessToken); + } }