4949import org .apache .syncope .core .persistence .api .dao .UserDAO ;
5050import org .apache .syncope .core .persistence .api .dao .search .AttrCond ;
5151import org .apache .syncope .core .persistence .api .dao .search .SearchCond ;
52+ import org .apache .syncope .core .persistence .api .entity .AccessToken ;
5253import org .apache .syncope .core .persistence .api .entity .Delegation ;
5354import org .apache .syncope .core .persistence .api .entity .DynRealm ;
5455import org .apache .syncope .core .persistence .api .entity .ExternalResource ;
@@ -164,7 +165,7 @@ public JWTSSOProvider getJWTSSOProvider(final String issuer) {
164165
165166 return jwtSSOProviders .stream ().filter (provider -> issuer .equals (provider .getIssuer ())).findFirst ().
166167 orElseThrow (() -> new AuthenticationCredentialsNotFoundException (
167- "Could not find any registered JWTSSOProvider for issuer " + issuer ));
168+ "Could not find any registered JWTSSOProvider for issuer " + issuer ));
168169 }
169170
170171 protected String getDelegationKey (final SyncopeAuthenticationDetails details , final String delegatedKey ) {
@@ -184,7 +185,7 @@ protected String getDelegationKey(final SyncopeAuthenticationDetails details, fi
184185
185186 return delegationDAO .findValidFor (delegatingKey , delegatedKey , OffsetDateTime .now ()).
186187 orElseThrow (() -> new SessionAuthenticationException (
187- "Delegation by " + delegatingKey + " was requested but none found" ));
188+ "Delegation by " + delegatingKey + " was requested but none found" ));
188189 }
189190
190191 /**
@@ -277,10 +278,10 @@ protected boolean usernamePasswordAuthentication(final User user, final String p
277278 try {
278279 Provision provision = resource .getProvisionByAnyType (AnyTypeKind .USER .name ()).
279280 orElseThrow (() -> new AccountNotFoundException (
280- "Unable to locate provision for user type " + AnyTypeKind .USER .name ()));
281+ "Unable to locate provision for user type " + AnyTypeKind .USER .name ()));
281282 connObjectKey = mappingManager .getConnObjectKeyValue (user , resource , provision ).
282283 orElseThrow (() -> new AccountNotFoundException (
283- "Unable to locate conn object key value for " + AnyTypeKind .USER .name ()));
284+ "Unable to locate conn object key value for " + AnyTypeKind .USER .name ()));
284285 Uid uid = connectorManager .getConnector (resource ).authenticate (connObjectKey , password , null );
285286 if (uid != null ) {
286287 authenticated = true ;
@@ -356,31 +357,31 @@ protected Set<SyncopeGrantedAuthority> getUserAuthorities(final User user) {
356357 userDAO .findAllRoles (user ).stream ().
357358 filter (role -> !RoleDAO .GROUP_OWNER_ROLE .equals (role .getKey ())).
358359 forEach (role -> role .getEntitlements ().forEach (entitlement -> {
359- Set <String > realms = Optional .ofNullable (entForRealms .get (entitlement )).orElseGet (() -> {
360- Set <String > r = new HashSet <>();
361- entForRealms .put (entitlement , r );
362- return r ;
363- });
364-
365- realms .addAll (role .getRealms ().stream ().map (Realm ::getFullPath ).collect (Collectors .toSet ()));
366- if (!entitlement .endsWith ("_CREATE" ) && !entitlement .endsWith ("_DELETE" )) {
367- realms .addAll (role .getDynRealms ().stream ().map (DynRealm ::getKey ).toList ());
368- }
369- }));
370-
371- // Give group entitlements for owned groups
372- groupDAO .findOwnedByUser (user .getKey ()).
373- forEach (g -> roleDAO .findById (RoleDAO .GROUP_OWNER_ROLE ).ifPresentOrElse (
374- groupOwnerRole -> groupOwnerRole .getEntitlements ().forEach (entitlement -> {
375360 Set <String > realms = Optional .ofNullable (entForRealms .get (entitlement )).orElseGet (() -> {
376- HashSet <String > r = new HashSet <>();
361+ Set <String > r = new HashSet <>();
377362 entForRealms .put (entitlement , r );
378363 return r ;
379364 });
380365
381- realms .add (new RealmUtils .GroupOwnerRealm (g .getRealm ().getFullPath (), g .getKey ()).output ());
382- }),
383- () -> LOG .warn ("Role {} was not found" , RoleDAO .GROUP_OWNER_ROLE )));
366+ realms .addAll (role .getRealms ().stream ().map (Realm ::getFullPath ).collect (Collectors .toSet ()));
367+ if (!entitlement .endsWith ("_CREATE" ) && !entitlement .endsWith ("_DELETE" )) {
368+ realms .addAll (role .getDynRealms ().stream ().map (DynRealm ::getKey ).toList ());
369+ }
370+ }));
371+
372+ // Give group entitlements for owned groups
373+ groupDAO .findOwnedByUser (user .getKey ()).
374+ forEach (g -> roleDAO .findById (RoleDAO .GROUP_OWNER_ROLE ).ifPresentOrElse (
375+ groupOwnerRole -> groupOwnerRole .getEntitlements ().forEach (entitlement -> {
376+ Set <String > realms = Optional .ofNullable (entForRealms .get (entitlement )).orElseGet (() -> {
377+ HashSet <String > r = new HashSet <>();
378+ entForRealms .put (entitlement , r );
379+ return r ;
380+ });
381+
382+ realms .add (new RealmUtils .GroupOwnerRealm (g .getRealm ().getFullPath (), g .getKey ()).output ());
383+ }),
384+ () -> LOG .warn ("Role {} was not found" , RoleDAO .GROUP_OWNER_ROLE )));
384385
385386 return buildAuthorities (entForRealms );
386387 }
@@ -390,17 +391,17 @@ protected Set<SyncopeGrantedAuthority> getDelegatedAuthorities(final Delegation
390391
391392 delegation .getRoles ().stream ().filter (role -> !RoleDAO .GROUP_OWNER_ROLE .equals (role .getKey ())).
392393 forEach (role -> role .getEntitlements ().forEach (entitlement -> {
393- Set <String > realms = Optional .ofNullable (entForRealms .get (entitlement )).orElseGet (() -> {
394- HashSet <String > r = new HashSet <>();
395- entForRealms .put (entitlement , r );
396- return r ;
397- });
398-
399- realms .addAll (role .getRealms ().stream ().map (Realm ::getFullPath ).collect (Collectors .toSet ()));
400- if (!entitlement .endsWith ("_CREATE" ) && !entitlement .endsWith ("_DELETE" )) {
401- realms .addAll (role .getDynRealms ().stream ().map (DynRealm ::getKey ).toList ());
402- }
403- }));
394+ Set <String > realms = Optional .ofNullable (entForRealms .get (entitlement )).orElseGet (() -> {
395+ HashSet <String > r = new HashSet <>();
396+ entForRealms .put (entitlement , r );
397+ return r ;
398+ });
399+
400+ realms .addAll (role .getRealms ().stream ().map (Realm ::getFullPath ).collect (Collectors .toSet ()));
401+ if (!entitlement .endsWith ("_CREATE" ) && !entitlement .endsWith ("_DELETE" )) {
402+ realms .addAll (role .getDynRealms ().stream ().map (DynRealm ::getKey ).toList ());
403+ }
404+ }));
404405
405406 return buildAuthorities (entForRealms );
406407 }
@@ -416,15 +417,15 @@ public Set<SyncopeGrantedAuthority> getAuthorities(final String username, final
416417 } else if (delegationKey != null ) {
417418 Delegation delegation = delegationDAO .findById (delegationKey ).
418419 orElseThrow (() -> new UsernameNotFoundException (
419- "Could not find delegation " + delegationKey ));
420+ "Could not find delegation " + delegationKey ));
420421
421422 authorities = delegation .getRoles ().isEmpty ()
422423 ? getUserAuthorities (delegation .getDelegating ())
423424 : getDelegatedAuthorities (delegation );
424425 } else {
425426 User user = userDAO .findByUsername (username ).
426427 orElseThrow (() -> new UsernameNotFoundException (
427- "Could not find any user with username " + username ));
428+ "Could not find any user with username " + username ));
428429
429430 authorities = getUserAuthorities (user );
430431 }
@@ -438,18 +439,21 @@ public JWTAuthResult authenticate(final JWTAuthentication authentication) {
438439 Set <SyncopeGrantedAuthority > authorities ;
439440
440441 if (securityProperties .getAdminUser ().equals (authentication .getClaims ().getSubject ())) {
441- accessTokenDAO .findById (authentication .getClaims ().getJWTID ()).
442+ AccessToken accessToken = accessTokenDAO .findById (authentication .getClaims ().getJWTID ()).
442443 orElseThrow (() -> new AuthenticationCredentialsNotFoundException (
443- "Could not find an Access Token for JWT " + authentication .getClaims ().getJWTID ()));
444+ "Could not find an Access Token for JWT " + authentication .getClaims ().getJWTID ()));
445+ if (!securityProperties .getAdminUser ().equals (accessToken .getOwner ())) {
446+ throw new AuthenticationCredentialsNotFoundException ("Access Token owner does not match JWT subject" );
447+ }
444448
445449 username = securityProperties .getAdminUser ();
446450 authorities = getAdminAuthorities ();
447451 } else {
448452 JWTSSOProvider jwtSSOProvider = getJWTSSOProvider (authentication .getClaims ().getIssuer ());
449453 JWTSSOProvider .ResolvedClaims resolved = jwtSSOProvider .resolve (authentication .getClaims ()).
450454 orElseThrow (() -> new AuthenticationCredentialsNotFoundException (
451- "Could not find User " + authentication .getClaims ().getSubject ()
452- + " for JWT " + authentication .getClaims ().getJWTID ()));
455+ "Could not find User " + authentication .getClaims ().getSubject ()
456+ + " for JWT " + authentication .getClaims ().getJWTID ()));
453457
454458 User user = resolved .user ();
455459 String delegationKey = getDelegationKey (authentication .getDetails (), user .getKey ());
0 commit comments