Skip to content

Commit 9f50171

Browse files
committed
Closes #793 Allow API access to Elide for services without user authentication
1 parent 5762990 commit 9f50171

2 files changed

Lines changed: 11 additions & 20 deletions

File tree

src/main/java/com/faforever/api/security/AuditService.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,14 @@ public AuditService(UserSupplier userSupplier) {
1717

1818
public void logMessage(String message) {
1919
final String extendedMessage = userSupplier.get()
20-
.map(fafAuthenticationToken -> {
21-
//move to switch pattern matching with java 21
22-
if (fafAuthenticationToken instanceof FafUserAuthenticationToken fafUserAuthenticationToken) {
23-
return MessageFormat.format("{0} [invoked by User ''{1}'' with id ''{2}'']",
20+
.map(fafAuthenticationToken ->
21+
switch (fafAuthenticationToken) {
22+
case FafUserAuthenticationToken fafUserAuthenticationToken -> MessageFormat.format("{0} [invoked by User ''{1}'' with id ''{2}'']",
2423
message, fafUserAuthenticationToken.getUsername(), fafUserAuthenticationToken.getUserId());
25-
} else if (fafAuthenticationToken instanceof FafServiceAuthenticationToken fafServiceAuthenticationToken) {
26-
return MessageFormat.format("{0} [invoked by Service ''{1}'']",
24+
case FafServiceAuthenticationToken fafServiceAuthenticationToken -> MessageFormat.format("{0} [invoked by Service ''{1}'']",
2725
message, fafServiceAuthenticationToken.getServiceName());
28-
} else {
29-
throw new RuntimeException();
3026
}
31-
})
27+
)
3228
.orElseGet(() -> MessageFormat.format("{0} [invoked by Annonymous user]", message));
3329
log.info(extendedMessage);
3430
}

src/main/java/com/faforever/api/security/FafAuthenticationConverter.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.List;
88
import java.util.Map;
9+
import java.util.Optional;
910

1011
/**
1112
* Jwt converter that reads scopes + custom FAF roles from the token extension.
@@ -18,24 +19,18 @@ public AbstractAuthenticationToken convert(Jwt source) {
1819
List<FafRole> roles = extractRoles(source);
1920

2021
String subject = extractSubject(source);
21-
22-
try {
23-
int userId = Integer.parseInt(subject);
24-
String username = extractUsername(source);
25-
return new FafUserAuthenticationToken(userId, username, scopes, roles);
26-
} catch (NumberFormatException e) {
27-
return new FafServiceAuthenticationToken(subject, scopes);
28-
}
22+
return extractUsername(source)
23+
.<FafAuthenticationToken>map(username -> new FafUserAuthenticationToken(Integer.parseInt(subject), username, scopes, roles))
24+
.orElseGet(() -> new FafServiceAuthenticationToken(subject, scopes));
2925
}
3026

3127
private String extractSubject(Jwt source) {
3228
return source.getSubject();
3329
}
3430

35-
private String extractUsername(Jwt source) {
31+
private Optional<String> extractUsername(Jwt source) {
3632
Map<String, Object> ext = source.getClaim("ext");
37-
String username = (String) ext.getOrDefault("username", "[undefined]");
38-
return username;
33+
return Optional.ofNullable((String) ext.get("username"));
3934
}
4035

4136
private List<FafScope> extractScopes(Jwt source) {

0 commit comments

Comments
 (0)