2121import java .time .OffsetDateTime ;
2222import java .util .HashSet ;
2323import java .util .List ;
24+ import java .util .Map ;
2425import java .util .Optional ;
2526import java .util .Set ;
2627import java .util .stream .Collectors ;
5556import org .apache .syncope .core .persistence .api .entity .policy .AccountPolicy ;
5657import org .apache .syncope .core .persistence .api .entity .policy .PasswordPolicy ;
5758import org .apache .syncope .core .persistence .api .entity .user .User ;
59+ import org .apache .syncope .core .persistence .api .utils .RealmUtils .ManagerRealm ;
60+ import org .apache .syncope .core .persistence .api .utils .RealmUtils .NormalizedRealms ;
5861import org .apache .syncope .core .provisioning .api .UserProvisioningManager ;
5962import org .apache .syncope .core .provisioning .api .data .UserDataBinder ;
6063import org .apache .syncope .core .provisioning .api .jexl .TemplateUtils ;
6467import org .apache .syncope .core .spring .policy .PasswordPolicyException ;
6568import org .apache .syncope .core .spring .security .AuthContextUtils ;
6669import org .apache .syncope .core .spring .security .SecurityProperties ;
70+ import org .apache .syncope .core .spring .security .SyncopeGrantedAuthority ;
6771import org .apache .syncope .core .spring .security .throttle .PasswordResetRequestThrottler ;
6872import org .apache .syncope .core .spring .security .throttle .ThrottlerAttempts ;
6973import org .springframework .security .access .prepost .PreAuthorize ;
@@ -81,6 +85,8 @@ protected static void throwMfaWasSet(final String username) {
8185 throw sce ;
8286 }
8387
88+ protected final SecurityProperties securityProperties ;
89+
8490 protected final DelegationDAO delegationDAO ;
8591
8692 protected final AccessTokenDAO accessTokenDAO ;
@@ -115,6 +121,7 @@ public UserSelfLogic(
115121 provisioningManager ,
116122 encryptorManager ,
117123 confParamOps );
124+ this .securityProperties = securityProperties ;
118125 this .delegationDAO = delegationDAO ;
119126 this .accessTokenDAO = accessTokenDAO ;
120127 this .resourceDAO = resourceDAO ;
@@ -131,9 +138,34 @@ public UserSelfLogic(
131138 public Self read () {
132139 UserTO authenticated = binder .getAuthenticatedUserTO ();
133140
141+ Map <String , Set <String >> authorizations ;
142+ if (securityProperties .getAnonymousUser ().equals (AuthContextUtils .getUsername ())) {
143+ authorizations = AuthContextUtils .getAuthorizations ();
144+ } else {
145+ // group manager Realms together, to avoid unwanted enumeration of managed anys
146+ authorizations = AuthContextUtils .getAuthorities ().stream ().
147+ map (authority -> {
148+ NormalizedRealms normalized = NormalizedRealms .of (authority .getRealms ());
149+
150+ SyncopeGrantedAuthority updated = new SyncopeGrantedAuthority (authority .getAuthority ());
151+ updated .addRealms (normalized .realms ());
152+
153+ normalized .managerRealms ().stream ().
154+ map (ManagerRealm ::of ).
155+ flatMap (Optional ::stream ).
156+ filter (r -> !updated .getRealms ().contains (r .realmPath ())).
157+ map (r -> r .realmPath () + '@' ).
158+ forEach (updated ::addRealm );
159+
160+ return updated ;
161+ }).
162+ filter (authority -> !authority .getRealms ().isEmpty ()).
163+ collect (Collectors .toMap (SyncopeGrantedAuthority ::getAuthority , SyncopeGrantedAuthority ::getRealms ));
164+ }
165+
134166 return new Self (
135167 authenticated ,
136- POJOHelper .serialize (AuthContextUtils . getAuthorizations () ),
168+ POJOHelper .serialize (authorizations ),
137169 POJOHelper .serialize (delegationDAO .findValidDelegating (authenticated .getKey (), OffsetDateTime .now ())));
138170 }
139171
0 commit comments