@@ -26,19 +26,7 @@ public class OauthAuthorityExtractor implements ProviderAuthorityExtractor {
26
26
27
27
@ Override
28
28
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 ));
42
30
}
43
31
44
32
@ Override
@@ -60,15 +48,25 @@ public Mono<Set<String>> extract(AccessControlService acs, Object value, Map<Str
60
48
}
61
49
62
50
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 ()
64
56
.stream ()
65
57
.filter (r -> r .getSubjects ()
66
58
.stream ()
67
59
.filter (s -> s .getProvider ().equals (Provider .OAUTH ))
68
60
.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 )))
70
64
.map (Role ::getName )
71
65
.collect (Collectors .toSet ());
66
+
67
+ log .debug ("Matched roles by username: [{}]" , String .join (", " , roles ));
68
+
69
+ return roles ;
72
70
}
73
71
74
72
private Set <String > extractRoles (AccessControlService acs , DefaultOAuth2User principal ,
@@ -77,7 +75,17 @@ private Set<String> extractRoles(AccessControlService acs, DefaultOAuth2User pri
77
75
Assert .notNull (provider , "provider is null" );
78
76
var rolesFieldName = provider .getCustomParams ().get (ROLES_FIELD_PARAM_NAME );
79
77
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
+
80
83
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
+
81
89
log .debug ("Token's groups: [{}]" , String .join ("," , principalRoles ));
82
90
83
91
Set <String > roles = acs .getRoles ()
@@ -94,15 +102,15 @@ private Set<String> extractRoles(AccessControlService acs, DefaultOAuth2User pri
94
102
.map (Role ::getName )
95
103
.collect (Collectors .toSet ());
96
104
97
- log .debug ("Matched roles: [{}]" , String .join (", " , roles ));
105
+ log .debug ("Matched group roles: [{}]" , String .join (", " , roles ));
98
106
99
107
return roles ;
100
108
}
101
109
102
110
@ SuppressWarnings ("unchecked" )
103
111
private Collection <String > convertRoles (Object roles ) {
104
112
if (roles == null ) {
105
- log .debug ("Param missing from attributes, skipping " );
113
+ log .warn ("Param missing in attributes, nothing to do " );
106
114
return Collections .emptySet ();
107
115
}
108
116
@@ -112,7 +120,7 @@ private Collection<String> convertRoles(Object roles) {
112
120
}
113
121
114
122
if (!(roles instanceof String )) {
115
- log .debug ("The field is not a string, skipping" );
123
+ log .trace ("The field is not a string, skipping" );
116
124
return Collections .emptySet ();
117
125
}
118
126
0 commit comments