From 5974ec1dba5c8c504e54799758641d905bc69568 Mon Sep 17 00:00:00 2001 From: akastijn Date: Fri, 24 Oct 2025 21:59:57 +0200 Subject: [PATCH] Revert "Refactor `getAuthenticatedUserUuid` - extract `getAuthentication` method for improved null handling and clarity" This reverts commit 4b466f314e23fc9174713ea93565fb11e4d85fae. --- .../data_from_auth/AuthenticatedUuid.java | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) 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; - } }