diff --git a/src/main/java/com/faforever/api/security/AuditService.java b/src/main/java/com/faforever/api/security/AuditService.java index e0524c8aa..f85a9f10b 100644 --- a/src/main/java/com/faforever/api/security/AuditService.java +++ b/src/main/java/com/faforever/api/security/AuditService.java @@ -17,18 +17,14 @@ public AuditService(UserSupplier userSupplier) { public void logMessage(String message) { final String extendedMessage = userSupplier.get() - .map(fafAuthenticationToken -> { - //move to switch pattern matching with java 21 - if (fafAuthenticationToken instanceof FafUserAuthenticationToken fafUserAuthenticationToken) { - return MessageFormat.format("{0} [invoked by User ''{1}'' with id ''{2}'']", + .map(fafAuthenticationToken -> + switch (fafAuthenticationToken) { + case FafUserAuthenticationToken fafUserAuthenticationToken -> MessageFormat.format("{0} [invoked by User ''{1}'' with id ''{2}'']", message, fafUserAuthenticationToken.getUsername(), fafUserAuthenticationToken.getUserId()); - } else if (fafAuthenticationToken instanceof FafServiceAuthenticationToken fafServiceAuthenticationToken) { - return MessageFormat.format("{0} [invoked by Service ''{1}'']", + case FafServiceAuthenticationToken fafServiceAuthenticationToken -> MessageFormat.format("{0} [invoked by Service ''{1}'']", message, fafServiceAuthenticationToken.getServiceName()); - } else { - throw new RuntimeException(); } - }) + ) .orElseGet(() -> MessageFormat.format("{0} [invoked by Annonymous user]", message)); log.info(extendedMessage); } diff --git a/src/main/java/com/faforever/api/security/FafAuthenticationConverter.java b/src/main/java/com/faforever/api/security/FafAuthenticationConverter.java index 9ee0681ef..7e6973a48 100644 --- a/src/main/java/com/faforever/api/security/FafAuthenticationConverter.java +++ b/src/main/java/com/faforever/api/security/FafAuthenticationConverter.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Map; +import java.util.Optional; /** * Jwt converter that reads scopes + custom FAF roles from the token extension. @@ -18,24 +19,18 @@ public AbstractAuthenticationToken convert(Jwt source) { List roles = extractRoles(source); String subject = extractSubject(source); - - try { - int userId = Integer.parseInt(subject); - String username = extractUsername(source); - return new FafUserAuthenticationToken(userId, username, scopes, roles); - } catch (NumberFormatException e) { - return new FafServiceAuthenticationToken(subject, scopes); - } + return extractUsername(source) + .map(username -> new FafUserAuthenticationToken(Integer.parseInt(subject), username, scopes, roles)) + .orElseGet(() -> new FafServiceAuthenticationToken(subject, scopes)); } private String extractSubject(Jwt source) { return source.getSubject(); } - private String extractUsername(Jwt source) { + private Optional extractUsername(Jwt source) { Map ext = source.getClaim("ext"); - String username = (String) ext.getOrDefault("username", "[undefined]"); - return username; + return Optional.ofNullable((String) ext.get("username")); } private List extractScopes(Jwt source) {