Skip to content

Commit 27c5e61

Browse files
committed
Refactor OauthAuthorityExtractor and improve logging
1 parent 3fdbfba commit 27c5e61

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

api/src/main/java/io/kafbat/ui/service/rbac/extractor/OauthAuthorityExtractor.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,7 @@ public class OauthAuthorityExtractor implements ProviderAuthorityExtractor {
2626

2727
@Override
2828
public boolean isApplicable(String provider, Map<String, String> customParams) {
29-
var typeMatch = OAUTH.equalsIgnoreCase(provider) || OAUTH.equalsIgnoreCase(customParams.get(TYPE));
30-
31-
if (!typeMatch) {
32-
return false;
33-
}
34-
35-
var containsRolesFieldNameParam = customParams.containsKey(ROLES_FIELD_PARAM_NAME);
36-
if (!containsRolesFieldNameParam) {
37-
log.debug("Provider [{}] doesn't contain a roles field param name, mapping won't be performed", provider);
38-
return false;
39-
}
40-
41-
return true;
29+
return OAUTH.equalsIgnoreCase(provider) || OAUTH.equalsIgnoreCase(customParams.get(TYPE));
4230
}
4331

4432
@Override
@@ -60,15 +48,25 @@ public Mono<Set<String>> extract(AccessControlService acs, Object value, Map<Str
6048
}
6149

6250
private Set<String> extractUsernameRoles(AccessControlService acs, DefaultOAuth2User principal) {
63-
return acs.getRoles()
51+
var principalName = principal.getName();
52+
53+
log.debug("Principal name is: [{}]", principalName);
54+
55+
var roles = acs.getRoles()
6456
.stream()
6557
.filter(r -> r.getSubjects()
6658
.stream()
6759
.filter(s -> s.getProvider().equals(Provider.OAUTH))
6860
.filter(s -> s.getType().equals("user"))
69-
.anyMatch(s -> s.getValue().equals(principal.getName())))
61+
.peek(s -> log.trace("[{}] matches [{}]? [{}]", s.getValue(), principalName,
62+
s.getValue().equalsIgnoreCase(principalName)))
63+
.anyMatch(s -> s.getValue().equalsIgnoreCase(principalName)))
7064
.map(Role::getName)
7165
.collect(Collectors.toSet());
66+
67+
log.debug("Matched roles by username: [{}]", String.join(", ", roles));
68+
69+
return roles;
7270
}
7371

7472
private Set<String> extractRoles(AccessControlService acs, DefaultOAuth2User principal,
@@ -77,7 +75,17 @@ private Set<String> extractRoles(AccessControlService acs, DefaultOAuth2User pri
7775
Assert.notNull(provider, "provider is null");
7876
var rolesFieldName = provider.getCustomParams().get(ROLES_FIELD_PARAM_NAME);
7977

78+
if (rolesFieldName == null) {
79+
log.warn("Provider [{}] doesn't contain a roles field param name, won't map roles", provider);
80+
return Collections.emptySet();
81+
}
82+
8083
var principalRoles = convertRoles(principal.getAttribute(rolesFieldName));
84+
if (principalRoles.isEmpty()) {
85+
log.debug("Principal [{}] doesn't have any roles, nothing to do", principal.getName());
86+
return Collections.emptySet();
87+
}
88+
8189
log.debug("Token's groups: [{}]", String.join(",", principalRoles));
8290

8391
Set<String> roles = acs.getRoles()
@@ -94,15 +102,15 @@ private Set<String> extractRoles(AccessControlService acs, DefaultOAuth2User pri
94102
.map(Role::getName)
95103
.collect(Collectors.toSet());
96104

97-
log.debug("Matched roles: [{}]", String.join(", ", roles));
105+
log.debug("Matched group roles: [{}]", String.join(", ", roles));
98106

99107
return roles;
100108
}
101109

102110
@SuppressWarnings("unchecked")
103111
private Collection<String> convertRoles(Object roles) {
104112
if (roles == null) {
105-
log.debug("Param missing from attributes, skipping");
113+
log.warn("Param missing in attributes, nothing to do");
106114
return Collections.emptySet();
107115
}
108116

@@ -112,7 +120,7 @@ private Collection<String> convertRoles(Object roles) {
112120
}
113121

114122
if (!(roles instanceof String)) {
115-
log.debug("The field is not a string, skipping");
123+
log.trace("The field is not a string, skipping");
116124
return Collections.emptySet();
117125
}
118126

0 commit comments

Comments
 (0)