Skip to content

Commit be2debc

Browse files
committed
Address PR feedback
1 parent 82d0ec8 commit be2debc

File tree

9 files changed

+47
-23
lines changed

9 files changed

+47
-23
lines changed

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/HttpResponse.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,7 @@ List<String> getHeader(String key) {
6363
}
6464

6565
Map<String, String> getBodyAsMap() {
66-
try (JsonReader reader = JsonProviders.createReader(this.body)) {
67-
reader.nextToken();
68-
return reader.readMap(JsonReader::getString);
69-
} catch (IOException e) {
70-
throw new MsalClientException("Could not parse JSON from HttpResponse body: " + e.getMessage(), AuthenticationErrorCode.INVALID_JSON);
71-
}
66+
return JsonHelper.convertJsonToMap(this.body);
7267
}
7368

7469
public int statusCode() {

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/JsonHelper.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.io.IOException;
1818
import java.util.ArrayList;
1919
import java.util.Iterator;
20+
import java.util.Map;
2021
import java.util.Set;
2122

2223
class JsonHelper {
@@ -51,6 +52,16 @@ static <T extends JsonSerializable<T>> T convertJsonStringToJsonSerializableObje
5152
}
5253
}
5354

55+
//Converts a JSON string to a Map<String, String>
56+
static Map<String, String> convertJsonToMap(String jsonString) {
57+
try (JsonReader reader = JsonProviders.createReader(jsonString)) {
58+
reader.nextToken();
59+
return reader.readMap(JsonReader::getString);
60+
} catch (IOException e) {
61+
throw new MsalClientException("Could not parse JSON from HttpResponse body: " + e.getMessage(), AuthenticationErrorCode.INVALID_JSON);
62+
}
63+
}
64+
5465
/**
5566
* Throws exception if given String does not follow JSON syntax
5667
*/

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/CacheFormatTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public void tokenCacheEntitiesFormatTest(String folder) throws URISyntaxExceptio
158158
doReturn(msalOAuthHttpRequest).when(request).createOauthHttpRequest();
159159
doReturn(httpResponse).when(msalOAuthHttpRequest).send();
160160
doReturn(200).when(httpResponse).statusCode();
161-
doReturn(TestHelper.convertJsonToMap((tokenResponse))).when(httpResponse).getBodyAsMap();
161+
doReturn(JsonHelper.convertJsonToMap((tokenResponse))).when(httpResponse).getBodyAsMap();
162162

163163
final AuthenticationResult result = request.executeTokenRequest();
164164

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/RequestThrottlingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private PublicClientApplication getClientApplicationMockedWithOneTokenEndpointRe
9898
switch (responseType) {
9999
case RETRY_AFTER_HEADER:
100100
httpResponse.statusCode(HTTPResponse.SC_OK);
101-
httpResponse.body(TestConfiguration.TOKEN_ENDPOINT_OK_RESPONSE);
101+
httpResponse.body(TestConfiguration.TOKEN_ENDPOINT_OK_RESPONSE_ID_AND_ACCESS);
102102

103103
headers.put("Retry-After", Arrays.asList(THROTTLE_IN_SEC.toString()));
104104
break;

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/TestConfiguration.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public final class TestConfiguration {
4343

4444
public final static String AAD_PREFERRED_NETWORK_ENV_ALIAS = "login.microsoftonline.com";
4545

46-
public final static String TOKEN_ENDPOINT_OK_RESPONSE = "{\"access_token\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6I"
46+
public final static String TOKEN_ENDPOINT_OK_RESPONSE_ID_AND_ACCESS = "{\"access_token\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6I"
4747
+ "k5HVEZ2ZEstZnl0aEV1THdqcHdBSk9NOW4tQSJ9.eyJhdWQiOiJiN2E2NzFkOC1hNDA4LTQyZmYtODZlMC1hYWY0NDdmZDE3YzQiLCJpc3MiOiJod"
4848
+ "HRwczovL3N0cy53aW5kb3dzLm5ldC8zMGJhYTY2Ni04ZGY4LTQ4ZTctOTdlNi03N2NmZDA5OTU5NjMvIiwiaWF0IjoxMzkzODQ0NTA0LCJuYmYiOj"
4949
+ "EzOTM4NDQ1MDQsImV4cCI6MTM5Mzg0ODQwNCwidmVyIjoiMS4wIiwidGlkIjoiMzBiYWE2NjYtOGRmOC00OGU3LTk3ZTYtNzdjZmQwOTk1OTYzIiwi"
@@ -66,6 +66,21 @@ public final class TestConfiguration {
6666
"WQiOiI5ZjQ4ODBkOC04MGJhLTRjNDAtOTdiYy1mN2EyM2M3MDMwODQiLCJ1dGlkIjoiZjY0NWFkOTItZTM4ZC00ZDFhLWI1MTAtZDFiMDlhNzRhOGNhIn0\"" +
6767
"}";
6868

69+
public final static String TOKEN_ENDPOINT_OK_RESPONSE_ACCESS_ONLY = "{\"token_type\":\"Bearer\",\"expires_in\":3600," +
70+
"\"access_token\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5HVEZ2ZEstZnl0aEV1THdqcHdBSk9NOW4tQSJ9" +
71+
".eyJhdWQiOiJiN2E2NzFkOC1hNDA4LTQyZmYtODZlMC1hYWY0NDdmZDE3YzQiLCJpc3MiOiJodRwczovL3N0cy53aW5kb3dzLm5ldC8" +
72+
"zMGJhYTY2Ni04ZGY4LTQ4ZTctOTdlNi03N2NmZDA5OTU5NjMvIiwiaWF0IjoxMzkzODQ0NTA0LCJuYmYiOjEzOTM4NDQ1MDQsImV4cC" +
73+
"I6MTM5Mzg0ODQwNCwidmVyIjoiMS4wIiwidGlkIjoiMzBiYWE2NjYtOGRmOC00OGU3LTk3ZTYtNzdjZmQwOTk1OTYzIiwib2lkIjoiN" +
74+
"GY4NTk5ODktYTJmZi00MTFlLTkwNDgtYzMyMjI0N2FjNjJjIiwidXBuIjoiYWRtaW5AYWFsdGVzdHMub25taWNyb3NvZnQuY29tIiwi" +
75+
"dW5pcXVlX25hbWUiOiJhZG1pbkBhYWx0ZXN0cy5vbm1pY3Jvc29mdC5jb20iLCJzdWIiOiJqQ0ttUENWWEFzblN1MVBQUzRWblo4c2V" +
76+
"ONTR3U3F0cW1RkpGbW14SEF3IiwiZmFtaWx5X25hbWUiOiJBZG1pbiIsImdpdmVuX25hbWUiOiJBREFMVGVzdHMiLCJhcHBpZCI6Ijk" +
77+
"wODNjY2I4LThhNDYtNDNlNy04NDM5LTFkNjk2ZGY5ODRhZSIsImFwcGlkYWNyIjoiMSIsInNjcCI6InVzZXJfaW1wZXJzb25hdGlvbi" +
78+
"IsImFjciI6IjEifQ.lUfDlkLdNGuAGUukgTnS_uWeSFXljbhId1l9PDrr7AwOSbOzogLvO14TaU294T6HeOQ8e0dUAvxEAMvsK_800A" +
79+
"-AGNvbHK363xDjgmu464ye3CQvwq73GoHkzuxILCJKo0DUj0_XsCpQ4TdkPepuhzaGc-zYsfMU1POuIOB87pzW7e_VDpCdxcN1fuk-7" +
80+
"CECPQb8nrO0L8Du8y-TZDdTSe-i_A0Alv48Zll-6tDY9cxfAR0UyYKl_Kf45kuHAphCWwPsjUxv4rGHhgXZPRlKFq7bkXP2Es4ixCQz" +
81+
"b3bVLLrtQaZjkQ1yn37ngJro8NR63EbHHjHTA9lRmf8KIQ\"" +
82+
"}";
83+
6984
public final static String HTTP_ERROR_RESPONSE = "{\"error\":\"invalid_request\",\"error_description\":\"AADSTS90011: Request "
7085
+ "is ambiguous, multiple application identifiers found. Application identifiers: 'd09bb6da-4d46-4a16-880c-7885d8291fb9"
7186
+ ", 216ef81d-f3b2-47d4-ad21-a4df49b56dee'.\r\nTrace ID: 428a1f68-767d-4a1c-ae8e-f710eeaf4e9b\r\nCorrelation ID: 1e0955"

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/TestHelper.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,4 @@ static PrivateKey getPrivateKey() {
190190

191191
return privateKey;
192192
}
193-
194-
static Map<String, String> convertJsonToMap(String jsonString) {
195-
try (JsonReader reader = JsonProviders.createReader(jsonString)) {
196-
reader.nextToken();
197-
return reader.readMap(JsonReader::getString);
198-
} catch (IOException e) {
199-
throw new MsalClientException("Could not parse JSON from HttpResponse body: " + e.getMessage(), AuthenticationErrorCode.INVALID_JSON);
200-
}
201-
}
202193
}

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/TokenRequestExecutorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ void testExecuteOAuth_Success() throws SerializeException, ParseException, MsalE
234234

235235
doReturn(msalOAuthHttpRequest).when(request).createOauthHttpRequest();
236236
doReturn(httpResponse).when(msalOAuthHttpRequest).send();
237-
doReturn(TestHelper.convertJsonToMap(TestConfiguration.TOKEN_ENDPOINT_OK_RESPONSE)).when(httpResponse).getBodyAsMap();
237+
doReturn(JsonHelper.convertJsonToMap(TestConfiguration.TOKEN_ENDPOINT_OK_RESPONSE_ID_AND_ACCESS)).when(httpResponse).getBodyAsMap();
238238

239239
doReturn(200).when(httpResponse).statusCode();
240240

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/TokenResponseTest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,31 @@
1010

1111
import static org.junit.jupiter.api.Assertions.assertEquals;
1212
import static org.junit.jupiter.api.Assertions.assertNotNull;
13+
import static org.junit.jupiter.api.Assertions.assertNull;
1314

1415
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
1516
public class TokenResponseTest {
1617

1718
@Test
18-
void testConstructor() {
19-
final Map<String, String> jsonMap = TestHelper.convertJsonToMap(TestConfiguration.TOKEN_ENDPOINT_OK_RESPONSE);
20-
final TokenResponse response = new TokenResponse(TestHelper.convertJsonToMap(TestConfiguration.TOKEN_ENDPOINT_OK_RESPONSE));
19+
void testConstructor_PublicResponse() {
20+
final Map<String, String> jsonMap = JsonHelper.convertJsonToMap(TestConfiguration.TOKEN_ENDPOINT_OK_RESPONSE_ID_AND_ACCESS);
21+
final TokenResponse response = new TokenResponse(JsonHelper.convertJsonToMap(TestConfiguration.TOKEN_ENDPOINT_OK_RESPONSE_ID_AND_ACCESS));
2122
assertNotNull(response);
2223
assertEquals(jsonMap.get("access_token"), response.accessToken());
2324
assertEquals(jsonMap.get("id_token"), response.idToken());
2425
assertEquals(jsonMap.get("client_info"), response.getClientInfo());
2526
assertEquals(jsonMap.get("scope"), response.getScope());
2627
assertEquals(jsonMap.get("expires_in"), Long.toString(response.getExpiresIn()));
2728
}
29+
30+
@Test
31+
void testConstructor_CLientCredentialResponse() {
32+
final Map<String, String> jsonMap = JsonHelper.convertJsonToMap(TestConfiguration.TOKEN_ENDPOINT_OK_RESPONSE_ACCESS_ONLY);
33+
final TokenResponse response = new TokenResponse(JsonHelper.convertJsonToMap(TestConfiguration.TOKEN_ENDPOINT_OK_RESPONSE_ACCESS_ONLY));
34+
assertNotNull(response);
35+
assertEquals(jsonMap.get("access_token"), response.accessToken());
36+
assertNull(response.idToken());
37+
assertNull(response.getClientInfo());
38+
assertEquals(jsonMap.get("expires_in"), Long.toString(response.getExpiresIn()));
39+
}
2840
}

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/UIRequiredCacheTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private PublicClientApplication getApp_MockedWith_OKTokenEndpointResponse_Invali
8989
IHttpClient httpClientMock = mock(IHttpClient.class);
9090

9191
HttpResponse httpResponse =
92-
getHttpResponse(HTTPResponse.SC_OK, TestConfiguration.TOKEN_ENDPOINT_OK_RESPONSE);
92+
getHttpResponse(HTTPResponse.SC_OK, TestConfiguration.TOKEN_ENDPOINT_OK_RESPONSE_ID_AND_ACCESS);
9393
lenient().doReturn(httpResponse).when(httpClientMock).send(any());
9494

9595
httpResponse = getHttpResponse(HTTPResponse.SC_UNAUTHORIZED,

0 commit comments

Comments
 (0)