|
3 | 3 |
|
4 | 4 | package com.microsoft.aad.msal4j;
|
5 | 5 |
|
| 6 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 7 | +import com.fasterxml.jackson.databind.JsonNode; |
6 | 8 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 9 | +import com.fasterxml.jackson.databind.ObjectReader; |
7 | 10 | import com.fasterxml.jackson.databind.node.ObjectNode;
|
8 | 11 | import lombok.Getter;
|
9 | 12 | import lombok.Setter;
|
| 13 | + |
| 14 | +import java.util.Iterator; |
| 15 | +import java.io.IOException; |
10 | 16 | import java.util.ArrayList;
|
11 | 17 | import java.util.List;
|
12 | 18 |
|
@@ -34,6 +40,16 @@ public void requestClaimInIdToken(String claim, RequestedClaimAdditionalInfo req
|
34 | 40 | idTokenRequestedClaims.add(new RequestedClaim(claim, requestedClaimAdditionalInfo));
|
35 | 41 | }
|
36 | 42 |
|
| 43 | + /** |
| 44 | + * Inserts a claim into the list of claims to be added to the "userinfo" section of an OIDC claims request |
| 45 | + * |
| 46 | + * @param claim the name of the claim to be requested |
| 47 | + * @param requestedClaimAdditionalInfo additional information about the claim being requested |
| 48 | + */ |
| 49 | + protected void requestClaimInUserInfo(String claim, RequestedClaimAdditionalInfo requestedClaimAdditionalInfo) { |
| 50 | + userInfoRequestedClaims.add(new RequestedClaim(claim, requestedClaimAdditionalInfo)); |
| 51 | + } |
| 52 | + |
37 | 53 | /**
|
38 | 54 | * Inserts a claim into the list of claims to be added to the "access_token" section of an OIDC claims request
|
39 | 55 | *
|
@@ -70,9 +86,62 @@ private ObjectNode convertClaimsToObjectNode(List<RequestedClaim> claims) {
|
70 | 86 | ObjectMapper mapper = new ObjectMapper();
|
71 | 87 | ObjectNode claimsNode = mapper.createObjectNode();
|
72 | 88 |
|
73 |
| - for (RequestedClaim claim: claims) { |
| 89 | + for (RequestedClaim claim : claims) { |
74 | 90 | claimsNode.setAll((ObjectNode) mapper.valueToTree(claim));
|
75 | 91 | }
|
76 | 92 | return claimsNode;
|
77 | 93 | }
|
| 94 | + |
| 95 | + /** |
| 96 | + * Creates an instance of ClaimsRequest from a JSON-formatted String which follows the specification for the OIDC claims request parameter |
| 97 | + * |
| 98 | + * @param claims a String following JSON formatting |
| 99 | + * @return a ClaimsRequest instance |
| 100 | + */ |
| 101 | + public static ClaimsRequest formatAsClaimsRequest(String claims) { |
| 102 | + try { |
| 103 | + ClaimsRequest cr = new ClaimsRequest(); |
| 104 | + |
| 105 | + ObjectMapper mapper = new ObjectMapper(); |
| 106 | + ObjectReader reader = mapper.readerFor(new TypeReference<List<String>>() {}); |
| 107 | + |
| 108 | + JsonNode jsonClaims = mapper.readTree(claims); |
| 109 | + |
| 110 | + addClaimsFromJsonNode(jsonClaims.get("id_token"), "id_token", cr, reader); |
| 111 | + addClaimsFromJsonNode(jsonClaims.get("userinfo"), "userinfo", cr, reader); |
| 112 | + addClaimsFromJsonNode(jsonClaims.get("access_token"), "access_token", cr, reader); |
| 113 | + |
| 114 | + return cr; |
| 115 | + } catch (IOException e) { |
| 116 | + throw new MsalClientException("Could not convert string to ClaimsRequest: " + e.getMessage(), AuthenticationErrorCode.INVALID_JSON); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private static void addClaimsFromJsonNode(JsonNode claims, String group, ClaimsRequest cr, ObjectReader reader) throws IOException { |
| 121 | + Iterator<String> claimsIterator; |
| 122 | + |
| 123 | + if (claims != null) { |
| 124 | + claimsIterator = claims.fieldNames(); |
| 125 | + while (claimsIterator.hasNext()) { |
| 126 | + String claim = claimsIterator.next(); |
| 127 | + Boolean essential = null; |
| 128 | + String value = null; |
| 129 | + List<String> values = null; |
| 130 | + RequestedClaimAdditionalInfo claimInfo = null; |
| 131 | + |
| 132 | + if (claims.get(claim).has("essential")) essential = claims.get(claim).get("essential").asBoolean(); |
| 133 | + if (claims.get(claim).has("value")) value = claims.get(claim).get("value").textValue(); |
| 134 | + if (claims.get(claim).has("values")) values = reader.readValue(claims.get(claim).get("values")); |
| 135 | + |
| 136 | + //'null' is a valid value for RequestedClaimAdditionalInfo, so only initialize it if one of the parameters is not null |
| 137 | + if (essential != null || value != null || values != null) { |
| 138 | + claimInfo = new RequestedClaimAdditionalInfo(essential == null ? false : essential, value, values); |
| 139 | + } |
| 140 | + |
| 141 | + if (group.equals("id_token")) cr.requestClaimInIdToken(claim, claimInfo); |
| 142 | + if (group.equals("userinfo")) cr.requestClaimInUserInfo(claim, claimInfo); |
| 143 | + if (group.equals("access_token")) cr.requestClaimInAccessToken(claim, claimInfo); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
78 | 147 | }
|
0 commit comments