Skip to content

Commit

Permalink
Closes #793 Allow API access to Elide for services without user authe…
Browse files Browse the repository at this point in the history
…ntication
  • Loading branch information
bukajsytlos committed Nov 28, 2023
1 parent 5762990 commit 9f50171
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
14 changes: 5 additions & 9 deletions src/main/java/com/faforever/api/security/AuditService.java
Expand Up @@ -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());

Check warning on line 25 in src/main/java/com/faforever/api/security/AuditService.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L25 was not covered by tests
} else {
throw new RuntimeException();
}
})
)
.orElseGet(() -> MessageFormat.format("{0} [invoked by Annonymous user]", message));
log.info(extendedMessage);
}
Expand Down
Expand Up @@ -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.
Expand All @@ -18,24 +19,18 @@ public AbstractAuthenticationToken convert(Jwt source) {
List<FafRole> 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)
.<FafAuthenticationToken>map(username -> new FafUserAuthenticationToken(Integer.parseInt(subject), username, scopes, roles))
.orElseGet(() -> new FafServiceAuthenticationToken(subject, scopes));

Check warning on line 24 in src/main/java/com/faforever/api/security/FafAuthenticationConverter.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/api/security/FafAuthenticationConverter.java#L21-L24

Added lines #L21 - L24 were not covered by tests
}

private String extractSubject(Jwt source) {
return source.getSubject();

Check warning on line 28 in src/main/java/com/faforever/api/security/FafAuthenticationConverter.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L28 was not covered by tests
}

private String extractUsername(Jwt source) {
private Optional<String> extractUsername(Jwt source) {
Map<String, Object> ext = source.getClaim("ext");
String username = (String) ext.getOrDefault("username", "[undefined]");
return username;
return Optional.ofNullable((String) ext.get("username"));

Check warning on line 33 in src/main/java/com/faforever/api/security/FafAuthenticationConverter.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L33 was not covered by tests
}

private List<FafScope> extractScopes(Jwt source) {
Expand Down

0 comments on commit 9f50171

Please sign in to comment.