Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.

Commit d7d2a62

Browse files
author
Dominik František Bučík
authored
Merge pull request #123 from dBucik/fix_pccx
fix: 🐛 Fix missing sub in ClaimSourceProduceContext
2 parents fd525b1 + 5eace9f commit d7d2a62

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

perun-oidc-server/src/main/java/cz/muni/ics/oidc/server/ga4gh/Ga4ghPassportAndVisaClaimSource.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ protected void callPermissionsJwtAPI(Ga4ghClaimRepository repo,
162162
ArrayNode passport,
163163
Set<String> linkedIdentities)
164164
{
165+
log.debug("GA4GH: {}", uriVariables);
165166
JsonNode response = callHttpJsonAPI(repo, uriVariables);
166167
if (response != null) {
167168
JsonNode visas = response.path(GA4GH_CLAIM);

perun-oidc-server/src/main/java/cz/muni/ics/oidc/server/userInfo/PerunUserInfoCacheLoader.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)