Skip to content

Commit e644169

Browse files
Avery-DunnSomkaPeRomanNosachevSantiago Gonzalezsangonzal
authored
Add String-to-ClaimsRequest helper method (#344)
* 1.8.1 release (#327) * Exception Improvements (#254) * Add null checks for MsalException error code references * Better exception handling for invalid tokens * Better exception handling for invalid tokens * Sync with changes to Azure-Samples/ms-identity-java-desktop (#259) * extra scopes for consent during authorizaion * typo * minor * HTTPClient default timeouts (#264) * Add default timeouts for DefaultHttpClient * Handle 'stay signed in' confirmation page in DeviceCodeIT tests * Small best-practices changes * append extra scopes as suffix * 1.6.2 release (#268) * fixing integ test * Tenant Profiles (#263) * Classes for tenant profile functionality * Implement tenant profile feature * Tests for tenant profile feature * Simplify tenant profile class structure * 1.6.2 release * Classes for tenant profile redesign * Tests for tenant profile redesign * Adjust sample cached ID tokens to have realistic headers * Redesign how Tenant Pofiles are added to Accounts * New error code for JWT parse exceptions * Add claims and tenant profiles fields to Account * Remove annotation excluding realm field from comparisons * Use more generic token * Remove ID token claims field from Account * Minor changes for clarity * Adjust tests for tenant profile design refactor * Refactor tenant profile structure * Minor fixes * Minor fixes * Minor fixes * Simplify tenant profile class Co-authored-by: SomkaPe <[email protected]> * Improve HTTP client timeouts (#275) * 1.6.2 release (#269) * 1.6.2 release * Make DefaultHttpClient timeouts settable * Refactor timeout names Co-authored-by: SomkaPe <[email protected]> * Bewaters certchain (#276) * Support for certificate chain * 1.7.0 release (#277) * Update DefaultHttpClient.java * Fixed parsing ClientInfo: on some accounts, the server response contained characters that are incorrect for Base64 encoding, but acceptable for Base64URL (#282) * sendX5c api (#285) * refactoring (#287) * refactoring * refactoring * refactoring * Add AcquireTokenSilent tests for B2C and ADFS2019, refactor duplicate code in tests (#293) * Add public constants for cloud endpoints (#298) * Add public constants for cloud endpoints * Add license header * Added javadocs * Removed unneeded test * Make IAccount serializable (#297) * Make IAccount objects serializable * Make AuthenticationResult objects not serializable * Add tenant profile/id claims to auth result (#300) * Add tenant profile/id claims to auth result * Minor fix * treat null password as default one - empty string (#304) * treat null password as default one - empty string * Support for refresh_in (#305) * Support for refresh_in * Tests for refresh_in * Add extra null check * Add test for refreshOn cache persistence * refresh on is optional field (#312) * refresh on optional field * 1.8.0 Release (#313) 1.8.0 release * Fix spelling mistake in Prompt.java * Remove use of Nimbus Oauth2 SDK's CommonContentTypes (#322) * Remove use of Nimbus Oauth2 SDK's CommonContentTypes * Add enum for HTTP content-type constants * Remove use of javax.mail.internet.ContentType * Support for claims request parameter (#315) * ClaimsRequest classes * Support for claims request parameter * Tests for claims request * Use Jackson library for JSON processing * Change access level of userinfo and access_token claims * Better merge tests * Remove ability to set claims in userinfo field * Refactor claims field naming * 1.8.1 release (#326) * Version number updates for 1.8.1 release * Minor rewording Co-authored-by: SomkaPe <[email protected]> Co-authored-by: Roman Nosachev <[email protected]> Co-authored-by: Santiago Gonzalez <[email protected]> Co-authored-by: Santiago Gonzalez <[email protected]> * Add helper method to create a ClaimsRequest from a string Co-authored-by: SomkaPe <[email protected]> Co-authored-by: Roman Nosachev <[email protected]> Co-authored-by: Santiago Gonzalez <[email protected]> Co-authored-by: Santiago Gonzalez <[email protected]>
1 parent 12b876d commit e644169

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/main/java/com/microsoft/aad/msal4j/ClaimsRequest.java

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33

44
package com.microsoft.aad.msal4j;
55

6+
import com.fasterxml.jackson.core.type.TypeReference;
7+
import com.fasterxml.jackson.databind.JsonNode;
68
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import com.fasterxml.jackson.databind.ObjectReader;
710
import com.fasterxml.jackson.databind.node.ObjectNode;
811
import lombok.Getter;
912
import lombok.Setter;
13+
14+
import java.util.Iterator;
15+
import java.io.IOException;
1016
import java.util.ArrayList;
1117
import java.util.List;
1218

@@ -34,6 +40,16 @@ public void requestClaimInIdToken(String claim, RequestedClaimAdditionalInfo req
3440
idTokenRequestedClaims.add(new RequestedClaim(claim, requestedClaimAdditionalInfo));
3541
}
3642

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+
3753
/**
3854
* Inserts a claim into the list of claims to be added to the "access_token" section of an OIDC claims request
3955
*
@@ -70,9 +86,62 @@ private ObjectNode convertClaimsToObjectNode(List<RequestedClaim> claims) {
7086
ObjectMapper mapper = new ObjectMapper();
7187
ObjectNode claimsNode = mapper.createObjectNode();
7288

73-
for (RequestedClaim claim: claims) {
89+
for (RequestedClaim claim : claims) {
7490
claimsNode.setAll((ObjectNode) mapper.valueToTree(claim));
7591
}
7692
return claimsNode;
7793
}
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+
}
78147
}

src/test/java/com/microsoft/aad/msal4j/ClaimsTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,11 @@ public void testClaimsRequest_MergeWithClientCapabilitiesAndClaimsChallenge() th
7171
Assert.assertEquals(mergedClaimsAndChallenge, TestConfiguration.MERGED_CLAIMS_AND_CHALLENGE);
7272
Assert.assertEquals(mergedAll, TestConfiguration.MERGED_CLAIMS_CAPABILITIES_AND_CHALLENGE);
7373
}
74+
75+
@Test
76+
public void testClaimsRequest_StringToClaimsRequest() {
77+
ClaimsRequest cr = ClaimsRequest.formatAsClaimsRequest(TestConfiguration.CLAIMS_CHALLENGE);
78+
79+
Assert.assertEquals(cr.formatAsJSONString(), TestConfiguration.CLAIMS_CHALLENGE);
80+
}
7481
}

0 commit comments

Comments
 (0)