@@ -60,10 +60,11 @@ public UserInfo load(UserInfoCacheKey key) {
60
60
long perunUserId = key .getUserId ();
61
61
Set <String > attributes = constructAttributes (key .getScopes ());
62
62
Map <String , PerunAttributeValue > userAttributeValues = fetchUserAttributes (perunUserId , attributes );
63
+ String sub = extractSub (userAttributeValues , perunUserId , false );
63
64
64
65
ClaimSourceProduceContext .ClaimSourceProduceContextBuilder builder = ClaimSourceProduceContext .builder ()
65
66
.perunUserId (perunUserId )
66
- .sub (ui . getSub () )
67
+ .sub (sub )
67
68
.attrValues (userAttributeValues )
68
69
.scopes (key .getScopes ())
69
70
.client (key .getClient ())
@@ -97,11 +98,9 @@ private Map<String, PerunAttributeValue> fetchUserAttributes(long perunUserId, S
97
98
}
98
99
99
100
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 ());
101
103
if (requestedScopes != null && !requestedScopes .isEmpty ()) {
102
- if (requestedScopes .contains (OPENID )) {
103
- attributes .addAll (openidMappings .getAttrNames ());
104
- }
105
104
if (requestedScopes .contains (PROFILE )) {
106
105
attributes .addAll (profileMappings .getAttrNames ());
107
106
}
@@ -182,17 +181,31 @@ private void processStandardScopes(ClaimSourceProduceContext ctx, PerunUserInfo
182
181
183
182
private void processOpenid (Map <String , PerunAttributeValue > userAttributeValues , long perunUserId ,
184
183
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 ) {
185
189
JsonNode subJson = extractJsonValue (openidMappings .getSub (), userAttributeValues );
186
190
if (subJson != null && !subJson .isNull () && StringUtils .hasText (subJson .asText ())) {
191
+ String sub = subJson .asText ();
187
192
if (subModifiers != null ) {
188
193
subJson = modifyClaims (subModifiers , subJson );
189
- if (subJson .asText () == null || !StringUtils .hasText (subJson .asText ())) {
194
+ if (failOnNoSub && ( subJson .asText () == null || !StringUtils .hasText (subJson .asText () ))) {
190
195
throw new RuntimeException ("Sub has no value after modification for username " + perunUserId );
196
+ } else {
197
+ sub = subJson .asText ();
191
198
}
192
199
}
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 ;
194
208
}
195
- ui .setId (perunUserId );
196
209
}
197
210
198
211
private void processProfile (Map <String , PerunAttributeValue > userAttributeValues , PerunUserInfo ui ) {
0 commit comments