diff --git a/backend/src/main/java/com/alttd/altitudeweb/controllers/data_from_auth/AuthenticatedUuid.java b/backend/src/main/java/com/alttd/altitudeweb/controllers/data_from_auth/AuthenticatedUuid.java index 8d5cc52..c5ea419 100644 --- a/backend/src/main/java/com/alttd/altitudeweb/controllers/data_from_auth/AuthenticatedUuid.java +++ b/backend/src/main/java/com/alttd/altitudeweb/controllers/data_from_auth/AuthenticatedUuid.java @@ -25,14 +25,16 @@ public class AuthenticatedUuid { * @throws ResponseStatusException with 401 status if authentication is invalid */ public UUID getAuthenticatedUserUuid() { - Authentication authentication = getAuthentication(); + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - if (!(authentication.getPrincipal() instanceof Jwt jwt)) { - log.error("Authentication principal is not a JWT {}", authentication.getPrincipal() instanceof JWT); + if (authentication == null || !(authentication.getPrincipal() instanceof Jwt jwt)) { + log.error("Authentication principal is null {} or not a JWT {}", + authentication == null, authentication == null ? + "null" : authentication.getPrincipal() instanceof JWT); if (unsecured) { return UUID.fromString("55e46bc3-2a29-4c53-850f-dbd944dc5c5f"); } - throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Authentication should be JWT"); + throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Authentication required"); } String stringUuid = jwt.getSubject(); @@ -43,13 +45,4 @@ public class AuthenticatedUuid { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid UUID format"); } } - - private static Authentication getAuthentication() { - Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - if (authentication == null) { - log.error("Authentication is null"); - throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Authentication required"); - } - return authentication; - } }