Skip to content

Commit 7a4366c

Browse files
author
Justin Richer
committed
collapsed two serialization functions into one
1 parent d919e2e commit 7a4366c

File tree

2 files changed

+28
-44
lines changed

2 files changed

+28
-44
lines changed
Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.mitre.openid.connect.view;
22

3-
import java.util.List;
3+
import java.util.HashSet;
44
import java.util.Map.Entry;
55
import java.util.Set;
66

@@ -14,29 +14,6 @@ public class UserInfoSerializer {
1414

1515
private static ScopeClaimTranslationService translator = new ScopeClaimTranslationService();
1616

17-
/**
18-
* Filter the UserInfo object by scope, using our ScopeClaimTranslationService to determine
19-
* which claims are allowed for each given scope.
20-
*
21-
* @param ui the UserInfo to filter
22-
* @param scope the allowed scopes to filter by
23-
* @return the filtered JsonObject result
24-
*/
25-
public static JsonObject filterByScope(UserInfo ui, Set<String> scope) {
26-
27-
JsonObject uiJson = ui.toJson();
28-
List<String> filteredClaims = translator.getClaimsForScopeSet(scope);
29-
JsonObject result = new JsonObject();
30-
31-
for (String claim : filteredClaims) {
32-
if (uiJson.has(claim)) {
33-
result.add(claim, uiJson.get(claim));
34-
}
35-
}
36-
37-
return result;
38-
}
39-
4017
/**
4118
* Build a JSON response according to the request object received.
4219
*
@@ -51,32 +28,43 @@ public static JsonObject filterByScope(UserInfo ui, Set<String> scope) {
5128
*/
5229
public static JsonObject toJsonFromRequestObj(UserInfo ui, Set<String> scope, JsonObject authorizedClaims, JsonObject requestedClaims) {
5330

54-
// Only proceed if we have both requested claims and authorized claims list. Otherwise just return
55-
// the scope-filtered claim set.
56-
if (requestedClaims == null || authorizedClaims == null) {
57-
return filterByScope(ui, scope);
58-
}
59-
6031
// get the base object
6132
JsonObject obj = ui.toJson();
6233

63-
List<String> allowedByScope = translator.getClaimsForScopeSet(scope);
64-
JsonObject userinfoAuthorized = authorizedClaims.getAsJsonObject().get("userinfo").getAsJsonObject();
65-
JsonObject userinfoRequested = requestedClaims.getAsJsonObject().get("userinfo").getAsJsonObject();
34+
Set<String> allowedByScope = translator.getClaimsForScopeSet(scope);
35+
Set<String> authorizedByClaims = new HashSet<String>();
36+
Set<String> requestedByClaims = new HashSet<String>();
6637

67-
if (userinfoAuthorized == null || !userinfoAuthorized.isJsonObject()) {
68-
return obj;
38+
if (authorizedClaims != null) {
39+
JsonObject userinfoAuthorized = authorizedClaims.getAsJsonObject().get("userinfo").getAsJsonObject();
40+
for (Entry<String, JsonElement> entry : userinfoAuthorized.getAsJsonObject().entrySet()) {
41+
authorizedByClaims.add(entry.getKey());
42+
}
6943
}
70-
44+
if (requestedClaims != null) {
45+
JsonObject userinfoRequested = requestedClaims.getAsJsonObject().get("userinfo").getAsJsonObject();
46+
for (Entry<String, JsonElement> entry : userinfoRequested.getAsJsonObject().entrySet()) {
47+
requestedByClaims.add(entry.getKey());
48+
}
49+
}
50+
7151
// Filter claims by performing a manual intersection of claims that are allowed by the given scope, requested, and authorized.
7252
// We cannot use Sets.intersection() or similar because Entry<> objects will evaluate to being unequal if their values are
7353
// different, whereas we are only interested in matching the Entry<>'s key values.
7454
JsonObject result = new JsonObject();
75-
for (Entry<String, JsonElement> entry : userinfoAuthorized.getAsJsonObject().entrySet()) {
76-
if (userinfoRequested.has(entry.getKey()) && allowedByScope.contains(entry.getKey())) {
77-
result.add(entry.getKey(), entry.getValue());
55+
for (Entry<String, JsonElement> entry : obj.entrySet()) {
56+
57+
if (allowedByScope.contains(entry.getKey())
58+
|| authorizedByClaims.contains(entry.getKey())) {
59+
// it's allowed either by scope or by the authorized claims (either way is fine with us)
60+
61+
if (requestedByClaims.isEmpty() || requestedByClaims.contains(entry.getKey())) {
62+
// the requested claims are empty (so we allow all), or they're not empty and this claim was specifically asked for
63+
result.add(entry.getKey(), entry.getValue());
64+
} // otherwise there were specific claims requested and this wasn't one of them
7865
}
7966
}
67+
8068
return result;
8169
}
8270
}

openid-connect-server/src/main/java/org/mitre/openid/connect/view/UserInfoView.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,7 @@ protected void renderMergedOutputModel(Map<String, Object> model, HttpServletReq
108108
if (model.get("requestedClaims") != null) {
109109
requestedClaims = jsonParser.parse((String) model.get("requestedClaims")).getAsJsonObject();
110110
}
111-
if (authorizedClaims != null || requestedClaims != null) {
112-
gson.toJson(UserInfoSerializer.toJsonFromRequestObj(userInfo, scope, authorizedClaims, requestedClaims), out);
113-
} else {
114-
gson.toJson(UserInfoSerializer.filterByScope(userInfo, scope), out);
115-
}
111+
gson.toJson(UserInfoSerializer.toJsonFromRequestObj(userInfo, scope, authorizedClaims, requestedClaims), out);
116112

117113
} catch (IOException e) {
118114

0 commit comments

Comments
 (0)