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 27, 2023
1 parent a536eb9 commit 6abf569
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on: [ push, pull_request ]
jobs:
test:
runs-on: ubuntu-latest
container: eclipse-temurin:17-jdk
container: eclipse-temurin:21-jdk
steps:
- name: Get the version
id: get_version
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ tasks.withType(Test) {

group = 'faforever'

sourceCompatibility = 17
targetCompatibility = 17
sourceCompatibility = 21
targetCompatibility = 21

bootJar.enabled = true
jar.enabled = false
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-rc-4-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
14 changes: 5 additions & 9 deletions src/main/java/com/faforever/api/security/AuditService.java
Original file line number Diff line number Diff line change
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());
} else {
throw new RuntimeException();
}
})
)
.orElseGet(() -> MessageFormat.format("{0} [invoked by Annonymous user]", message));
log.info(extendedMessage);
}
Expand Down
Original file line number Diff line number Diff line change
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));
}

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

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"));
}

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

0 comments on commit 6abf569

Please sign in to comment.