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

fix: P4ADEV-451 token Revoke controller 2 service #25

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public ResponseEntity<UserInfo> getUserInfo() {
}
}

@Override
public ResponseEntity<Void> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package it.gov.pagopa.payhub.auth.service.logout;

public interface LogoutService {
void logout(String clientId, String token);
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
);
}

Expand Down Expand Up @@ -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);
}
}