Support custom role claim#1492
Conversation
| JwtOptionsConfig( | ||
| String issuer, | ||
| String audience, | ||
| String guarded, |
There was a problem hiding this comment.
Let's go with roles instead of guarded.
| .map(Arrays::asList) | ||
| .orElse(null); | ||
| List<String> roles = null; | ||
| String path = (this.roles != null && !this.roles.isEmpty()) ? this.roles : "scope"; |
There was a problem hiding this comment.
Schema should prevent empty string value if not permitted here and this code can then be simplified to remove the check.
The value of roles should be defaulted in the builder during parse to avoid implementing the defaulting logic here where options are being used.
| List<String> roles = null; | ||
| String path = (this.roles != null && !this.roles.isEmpty()) ? this.roles : "scope"; | ||
| Object claimObj = claimValue(claims, path); | ||
|
|
||
| if (claimObj instanceof List) | ||
| { | ||
| List<Object> listClaim = (List<Object>) claimObj; | ||
| roles = listClaim.stream() | ||
| .map(Object::toString) | ||
| .map(String::intern) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| else if (claimObj != null) | ||
| { | ||
| roles = Arrays.asList(claimObj.toString().split(SCOPE_VALUE_PATTERN)); | ||
| roles.replaceAll(String::intern); | ||
| } |
There was a problem hiding this comment.
| List<String> roles = null; | |
| String path = (this.roles != null && !this.roles.isEmpty()) ? this.roles : "scope"; | |
| Object claimObj = claimValue(claims, path); | |
| if (claimObj instanceof List) | |
| { | |
| List<Object> listClaim = (List<Object>) claimObj; | |
| roles = listClaim.stream() | |
| .map(Object::toString) | |
| .map(String::intern) | |
| .collect(Collectors.toList()); | |
| } | |
| else if (claimObj != null) | |
| { | |
| roles = Arrays.asList(claimObj.toString().split(SCOPE_VALUE_PATTERN)); | |
| roles.replaceAll(String::intern); | |
| } | |
| Object rolesValue = claimValue(claims, this.roles); | |
| @SuppressWarnings("unchecked") | |
| List<String> rolesValueAsList = (rolesValue instanceof List) | |
| ? (List<String>) rolesValue | |
| : Optional.ofNullable(rolesValue) | |
| .map(Object::toString) | |
| .map(s -> s.split("\\s+")) | |
| .map(Arrays::asList) | |
| .orElse(null); | |
| List<String> roles = rolesValueAsList != null | |
| ? rolesValueAsList.stream() | |
| .map(Object::toString) | |
| .map(String::intern) | |
| .toList() | |
| : null; |
| Map<String, Object> realmAccess = new HashMap<>(); | ||
| realmAccess.put("roles", asList("default-roles-backend", "offline_access", "uma_authorization")); | ||
| claims.setClaim("realm_access", realmAccess); |
There was a problem hiding this comment.
| Map<String, Object> realmAccess = new HashMap<>(); | |
| realmAccess.put("roles", asList("default-roles-backend", "offline_access", "uma_authorization")); | |
| claims.setClaim("realm_access", realmAccess); | |
| claims.setClaim("realm_access", | |
| Map.of("roles", List.of("default-roles-backend", "offline_access", "uma_authorization"))); |
| .inject(identity()) | ||
| .issuer("test issuer") | ||
| .audience("testAudience") | ||
| .roles("scope") |
There was a problem hiding this comment.
This should already default roles to scope in the builder .build() method, so it can be omitted here and still have the value scope in the returned JwtOptionsConfig object.
| .inject(identity()) | ||
| .issuer("test issuer") | ||
| .audience("testAudience") | ||
| .roles("scope") |
| private static final String SCOPE_VALUE_PATTERN = "\\s+"; | ||
| private static final String SCOPE_PATH_PATTERN = "\\."; |
There was a problem hiding this comment.
| private static final String SCOPE_VALUE_PATTERN = "\\s+"; | |
| private static final String SCOPE_PATH_PATTERN = "\\."; | |
| private static final String SPLIT_VALUE_PATTERN = "\\s+"; | |
| private static final String SPLIT_PATH_PATTERN = "\\."; |
|
|
||
| List<String> roles = rolesValueAsList != null | ||
| ? rolesValueAsList.stream() | ||
| .map(Object::toString) |
There was a problem hiding this comment.
Can this line be removed since the type of rolesValueAsList is already known to be List<String>, so individual role members are already known to be of type String?
Fixes #1476