1
1
package org .mitre .openid .connect .view ;
2
2
3
- import java .util .List ;
3
+ import java .util .HashSet ;
4
4
import java .util .Map .Entry ;
5
5
import java .util .Set ;
6
6
@@ -14,29 +14,6 @@ public class UserInfoSerializer {
14
14
15
15
private static ScopeClaimTranslationService translator = new ScopeClaimTranslationService ();
16
16
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
-
40
17
/**
41
18
* Build a JSON response according to the request object received.
42
19
*
@@ -51,32 +28,43 @@ public static JsonObject filterByScope(UserInfo ui, Set<String> scope) {
51
28
*/
52
29
public static JsonObject toJsonFromRequestObj (UserInfo ui , Set <String > scope , JsonObject authorizedClaims , JsonObject requestedClaims ) {
53
30
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
-
60
31
// get the base object
61
32
JsonObject obj = ui .toJson ();
62
33
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 > ();
66
37
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
+ }
69
43
}
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
+
71
51
// Filter claims by performing a manual intersection of claims that are allowed by the given scope, requested, and authorized.
72
52
// We cannot use Sets.intersection() or similar because Entry<> objects will evaluate to being unequal if their values are
73
53
// different, whereas we are only interested in matching the Entry<>'s key values.
74
54
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
78
65
}
79
66
}
67
+
80
68
return result ;
81
69
}
82
70
}
0 commit comments