Skip to content

Commit

Permalink
Fix code warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
krusche committed Nov 21, 2024
1 parent 4d43b60 commit 3a026e1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
10 changes: 5 additions & 5 deletions server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ dependencies {
testImplementation "org.mockito:mockito-core:5.14.2"
testImplementation "org.mockito:mockito-junit-jupiter:5.14.2"

testImplementation "org.testcontainers:testcontainers:${testcontainer_version}"
testImplementation "org.testcontainers:junit-jupiter:${testcontainer_version}"
testImplementation "org.testcontainers:jdbc:${testcontainer_version}"
testImplementation "org.testcontainers:database-commons:${testcontainer_version}"
testImplementation "org.testcontainers:postgresql:${testcontainer_version}"
testImplementation "org.testcontainers:testcontainers:${test_container_version}"
testImplementation "org.testcontainers:junit-jupiter:${test_container_version}"
testImplementation "org.testcontainers:jdbc:${test_container_version}"
testImplementation "org.testcontainers:database-commons:${test_container_version}"
testImplementation "org.testcontainers:postgresql:${test_container_version}"

// TODO: for some reason an update of org.junit.jupiter:junit-jupiter-engine to 5.11.x breaks the tests
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.10.5"
Expand Down
2 changes: 1 addition & 1 deletion server/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
rootProject.name=Thesis Track
spring_boot_version=3.3.6
netty_version=4.1.115.Final
testcontainer_version=1.20.4
test_container_version=1.20.4
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package thesistrack.ls1.security;

import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
Expand All @@ -26,7 +28,8 @@ public JwtAuthConverter(JwtAuthConfig config) {
}

@Override
public AbstractAuthenticationToken convert(Jwt jwt) {
@Nullable
public AbstractAuthenticationToken convert(@NonNull Jwt jwt) {
Collection<GrantedAuthority> authorities = Stream.concat(
jwtGrantedAuthoritiesConverter.convert(jwt).stream(),
extractResourceRoles(jwt).stream()).collect(Collectors.toSet());
Expand All @@ -35,19 +38,27 @@ public AbstractAuthenticationToken convert(Jwt jwt) {
}

private Collection<? extends GrantedAuthority> extractResourceRoles(Jwt jwt) {
Map<String, Object> resourceAccess = jwt.getClaim("resource_access");
// Retrieve the resource access claim as a nested map structure
Map<String, Map<String, Collection<String>>> resourceAccess = jwt.getClaim("resource_access");
if (resourceAccess == null) {
return Set.of();
}

Map<String, Object> resource;
Collection<String> resourceRoles;
// Get the client-specific resource and its roles
var resourceObject = resourceAccess.get(config.getClientId());
if (resourceObject == null) {
return Set.of();
}

if (resourceAccess == null
|| (resource = (Map<String, Object>) resourceAccess.get(config.getClientId())) == null
|| (resourceRoles = (Collection<String>) resource.get("roles")) == null) {
// Get the roles from the resource object
var resourceRoles = resourceObject.get("roles");
if (resourceRoles == null) {
return Set.of();
}

// Convert roles into GrantedAuthority objects
return resourceRoles.stream()
.map(role -> new SimpleGrantedAuthority("ROLE_" + role))
.collect(Collectors.toSet());
}
}
}

0 comments on commit 3a026e1

Please sign in to comment.