Revert "Refactor getAuthenticatedUserUuid - extract getAuthentication method for improved null handling and clarity"

This reverts commit 4b466f314e.
This commit is contained in:
akastijn 2025-10-24 21:59:57 +02:00
parent c5ed657d3e
commit 5974ec1dba

View File

@ -25,14 +25,16 @@ public class AuthenticatedUuid {
* @throws ResponseStatusException with 401 status if authentication is invalid * @throws ResponseStatusException with 401 status if authentication is invalid
*/ */
public UUID getAuthenticatedUserUuid() { public UUID getAuthenticatedUserUuid() {
Authentication authentication = getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication.getPrincipal() instanceof Jwt jwt)) { if (authentication == null || !(authentication.getPrincipal() instanceof Jwt jwt)) {
log.error("Authentication principal is not a JWT {}", authentication.getPrincipal() instanceof JWT); log.error("Authentication principal is null {} or not a JWT {}",
authentication == null, authentication == null ?
"null" : authentication.getPrincipal() instanceof JWT);
if (unsecured) { if (unsecured) {
return UUID.fromString("55e46bc3-2a29-4c53-850f-dbd944dc5c5f"); 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(); String stringUuid = jwt.getSubject();
@ -43,13 +45,4 @@ public class AuthenticatedUuid {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid UUID format"); 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;
}
} }