Skip to content

Commit aa55d25

Browse files
Throw exception on invalid deviceId during logout rather than silent pass
1 parent 9976bad commit aa55d25

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/main/java/com/accolite/pru/health/AuthApp/advice/AuthControllerAdvice.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.accolite.pru.health.AuthApp.exception.TokenRefreshException;
1212
import com.accolite.pru.health.AuthApp.exception.UpdatePasswordException;
1313
import com.accolite.pru.health.AuthApp.exception.UserLoginException;
14+
import com.accolite.pru.health.AuthApp.exception.UserLogoutException;
1415
import com.accolite.pru.health.AuthApp.exception.UserRegistrationException;
1516
import com.accolite.pru.health.AuthApp.model.payload.ApiResponse;
1617
import org.apache.log4j.Logger;
@@ -216,4 +217,14 @@ public ApiResponse handleTokenRefreshException(TokenRefreshException ex) {
216217
return apiResponse;
217218
}
218219

220+
@ExceptionHandler(value = UserLogoutException.class)
221+
@ResponseStatus(HttpStatus.EXPECTATION_FAILED)
222+
@ResponseBody
223+
public ApiResponse handleUserLogoutException(UserLogoutException ex) {
224+
ApiResponse apiResponse = new ApiResponse();
225+
apiResponse.setSuccess(false);
226+
apiResponse.setData(ex.getMessage());
227+
return apiResponse;
228+
}
229+
219230
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.accolite.pru.health.AuthApp.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
@ResponseStatus(HttpStatus.EXPECTATION_FAILED)
7+
public class UserLogoutException extends RuntimeException {
8+
9+
private String user;
10+
private String message;
11+
12+
public UserLogoutException(String user, String message) {
13+
super(String.format("Couldn't log out device [%s]: [%s])", user, message));
14+
this.user = user;
15+
this.message = message;
16+
}
17+
}

src/main/java/com/accolite/pru/health/AuthApp/service/UserService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.accolite.pru.health.AuthApp.service;
22

3+
import com.accolite.pru.health.AuthApp.exception.UserLogoutException;
34
import com.accolite.pru.health.AuthApp.model.CustomUserDetails;
45
import com.accolite.pru.health.AuthApp.model.Role;
56
import com.accolite.pru.health.AuthApp.model.RoleName;
@@ -125,6 +126,8 @@ private Set<Role> getRolesForNewUser(Boolean isAdmin) {
125126
public void logoutUser(CustomUserDetails customUserDetails, LogOutRequest logOutRequest) {
126127
String deviceId = logOutRequest.getDeviceInfo().getDeviceId();
127128
Optional<UserDevice> userDeviceOpt = userDeviceService.findByDeviceId(deviceId);
129+
userDeviceOpt.orElseThrow(() -> new UserLogoutException(logOutRequest.getDeviceInfo().getDeviceId(), "" +
130+
"Invalid device Id supplied. No matching user device found"));
128131
logger.info("Removing refresh token associated with device [" + userDeviceOpt + "]");
129132
userDeviceOpt.map(UserDevice::getRefreshToken)
130133
.map(RefreshToken::getId)

0 commit comments

Comments
 (0)