@@ -60,10 +60,11 @@ public UserInfo load(UserInfoCacheKey key) {
6060 long perunUserId = key .getUserId ();
6161 Set <String > attributes = constructAttributes (key .getScopes ());
6262 Map <String , PerunAttributeValue > userAttributeValues = fetchUserAttributes (perunUserId , attributes );
63+ String sub = extractSub (userAttributeValues , perunUserId , false );
6364
6465 ClaimSourceProduceContext .ClaimSourceProduceContextBuilder builder = ClaimSourceProduceContext .builder ()
6566 .perunUserId (perunUserId )
66- .sub (ui . getSub () )
67+ .sub (sub )
6768 .attrValues (userAttributeValues )
6869 .scopes (key .getScopes ())
6970 .client (key .getClient ())
@@ -97,11 +98,9 @@ private Map<String, PerunAttributeValue> fetchUserAttributes(long perunUserId, S
9798 }
9899
99100 private Set <String > constructAttributes (Set <String > requestedScopes ) {
100- Set <String > attributes = new HashSet <>();
101+ // always try to fetch sub, as it might be needed in further claims i.e. GA4GH processing
102+ Set <String > attributes = new HashSet <>(openidMappings .getAttrNames ());
101103 if (requestedScopes != null && !requestedScopes .isEmpty ()) {
102- if (requestedScopes .contains (OPENID )) {
103- attributes .addAll (openidMappings .getAttrNames ());
104- }
105104 if (requestedScopes .contains (PROFILE )) {
106105 attributes .addAll (profileMappings .getAttrNames ());
107106 }
@@ -182,17 +181,31 @@ private void processStandardScopes(ClaimSourceProduceContext ctx, PerunUserInfo
182181
183182 private void processOpenid (Map <String , PerunAttributeValue > userAttributeValues , long perunUserId ,
184183 PerunUserInfo ui ) {
184+ ui .setSub (extractSub (userAttributeValues , perunUserId , true ));
185+ ui .setId (perunUserId );
186+ }
187+
188+ private String extractSub (Map <String , PerunAttributeValue > userAttributeValues , long perunUserId , boolean failOnNoSub ) {
185189 JsonNode subJson = extractJsonValue (openidMappings .getSub (), userAttributeValues );
186190 if (subJson != null && !subJson .isNull () && StringUtils .hasText (subJson .asText ())) {
191+ String sub = subJson .asText ();
187192 if (subModifiers != null ) {
188193 subJson = modifyClaims (subModifiers , subJson );
189- if (subJson .asText () == null || !StringUtils .hasText (subJson .asText ())) {
194+ if (failOnNoSub && ( subJson .asText () == null || !StringUtils .hasText (subJson .asText () ))) {
190195 throw new RuntimeException ("Sub has no value after modification for username " + perunUserId );
196+ } else {
197+ sub = subJson .asText ();
191198 }
192199 }
193- ui .setSub (subJson .asText ());
200+ if (sub != null && StringUtils .hasText (sub )) {
201+ return sub ;
202+ }
203+ }
204+ if (failOnNoSub ) {
205+ throw new RuntimeException ("Sub has no value for username " + perunUserId );
206+ } else {
207+ return null ;
194208 }
195- ui .setId (perunUserId );
196209 }
197210
198211 private void processProfile (Map <String , PerunAttributeValue > userAttributeValues , PerunUserInfo ui ) {
0 commit comments