-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from bcgov/feature/java-17-upgrade
Java 17 upgrade
- Loading branch information
Showing
24 changed files
with
426 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...t-utils-api/src/main/java/ca/bc/gov/open/jag/documentutils/security/JwtAuthConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package ca.bc.gov.open.jag.documentutils.security; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.security.authentication.AbstractAuthenticationToken; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; | ||
import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
@Component | ||
public class JwtAuthConverter implements Converter<Jwt, AbstractAuthenticationToken> { | ||
|
||
private final JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = | ||
new JwtGrantedAuthoritiesConverter(); | ||
|
||
public static final String KEYCLOAK_PRINCIPLE_ATTRIBUTE = "preferred_username"; | ||
public static final String KEYCLOAK_RESOURCE_ATTRIBUTE = "resource_access"; | ||
public static final String KEYCLOAK_ROLE_ATTRIBUTE = "roles"; | ||
|
||
@Value("${jwt.auth.converter.resource-id}") | ||
private String resourceId; | ||
|
||
@Override | ||
public AbstractAuthenticationToken convert(@NonNull Jwt jwt) { | ||
Collection<GrantedAuthority> authorities = Stream.concat( | ||
jwtGrantedAuthoritiesConverter.convert(jwt).stream(), | ||
extractResourceRoles(jwt).stream() | ||
).collect(Collectors.toSet()); | ||
|
||
return new JwtAuthenticationToken( | ||
jwt, | ||
authorities, | ||
getPrincipleClaimName(jwt) | ||
); | ||
} | ||
|
||
private String getPrincipleClaimName(Jwt jwt) { | ||
return jwt.getClaim(KEYCLOAK_PRINCIPLE_ATTRIBUTE); | ||
} | ||
|
||
private Collection<? extends GrantedAuthority> extractResourceRoles(Jwt jwt) { | ||
Map<String, Object> resourceAccess; | ||
Map<String, Object> resource; | ||
Collection<String> resourceRoles; | ||
|
||
Collection<? extends GrantedAuthority> resourceRoles1; | ||
if (jwt.getClaim(KEYCLOAK_RESOURCE_ATTRIBUTE) == null) { | ||
return Set.of(); | ||
} | ||
resourceAccess = jwt.getClaim(KEYCLOAK_RESOURCE_ATTRIBUTE); | ||
|
||
if (resourceAccess.get(resourceId) == null) { | ||
return Set.of(); | ||
} | ||
resource = (Map<String, Object>) resourceAccess.get(resourceId); | ||
|
||
resourceRoles = (Collection<String>) resource.get(KEYCLOAK_ROLE_ATTRIBUTE); | ||
return resourceRoles | ||
.stream() | ||
.map(role -> new SimpleGrantedAuthority("ROLE_" + role)) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
Oops, something went wrong.