Skip to content

Commit e5f8b53

Browse files
committed
Remove com.nimbusds's HTTPRequest, ClientAuthentication, and related classes
1 parent 8e3b9d9 commit e5f8b53

File tree

5 files changed

+38
-62
lines changed

5 files changed

+38
-62
lines changed

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

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

44
package com.microsoft.aad.msal4j;
55

6+
import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
7+
import net.minidev.json.JSONObject;
8+
import com.nimbusds.oauth2.sdk.ParseException;
9+
610
import java.util.Arrays;
711
import java.util.HashMap;
812
import java.util.List;
@@ -46,14 +50,26 @@ public void addHeaders(Map<String, List<String>> responseHeaders) {
4650
}
4751
}
4852

49-
private void addHeader(final String name, final String... values) {
53+
void addHeader(final String name, final String... values) {
5054
if (values != null && values.length > 0) {
5155
headers.put(name, Arrays.asList(values));
5256
} else {
5357
headers.remove(name);
5458
}
5559
}
5660

61+
List<String> getHeader(String key) {
62+
return headers.get(key);
63+
}
64+
65+
JSONObject getBodyAsJson() {
66+
try {
67+
return JSONObjectUtils.parse(this.body());
68+
} catch (ParseException e) {
69+
throw new RuntimeException(e);
70+
}
71+
}
72+
5773
public int statusCode() {
5874
return this.statusCode;
5975
}

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

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
package com.microsoft.aad.msal4j;
55

6-
import com.nimbusds.oauth2.sdk.http.HTTPResponse;
7-
86
import java.util.Arrays;
97
import java.util.HashSet;
108
import java.util.Set;
@@ -14,37 +12,6 @@ class MsalServiceExceptionFactory {
1412
private MsalServiceExceptionFactory() {
1513
}
1614

17-
static MsalServiceException fromHttpResponse(HTTPResponse httpResponse) {
18-
19-
String responseContent = httpResponse.getContent();
20-
if (responseContent == null || StringHelper.isBlank(responseContent)) {
21-
return new MsalServiceException(
22-
String.format(
23-
"Unknown Service Exception. Service returned status code %s",
24-
httpResponse.getStatusCode()),
25-
AuthenticationErrorCode.UNKNOWN);
26-
}
27-
28-
ErrorResponse errorResponse = JsonHelper.convertJsonToObject(
29-
responseContent,
30-
ErrorResponse.class);
31-
32-
errorResponse.statusCode(httpResponse.getStatusCode());
33-
errorResponse.statusMessage(httpResponse.getStatusMessage());
34-
35-
if (errorResponse.error() != null &&
36-
errorResponse.error().equalsIgnoreCase(AuthenticationErrorCode.INVALID_GRANT)) {
37-
38-
if (isInteractionRequired(errorResponse.subError)) {
39-
return new MsalInteractionRequiredException(errorResponse, httpResponse.getHeaderMap());
40-
}
41-
}
42-
43-
return new MsalServiceException(
44-
errorResponse,
45-
httpResponse.getHeaderMap());
46-
}
47-
4815
static MsalServiceException fromHttpResponse(IHttpResponse response) {
4916
String responseBody = response.body();
5017
if (StringHelper.isBlank(responseBody)) {

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

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33

44
package com.microsoft.aad.msal4j;
55

6-
import com.nimbusds.oauth2.sdk.ParseException;
7-
import com.nimbusds.oauth2.sdk.http.HTTPResponse;
8-
96
import java.io.IOException;
107
import java.net.URI;
118
import java.net.URISyntaxException;
@@ -35,7 +32,7 @@ class OAuthHttpRequest {
3532
this.serviceBundle = serviceBundle;
3633
}
3734

38-
public HTTPResponse send() throws IOException {
35+
public HttpResponse send() throws IOException {
3936

4037
Map<String, String> httpHeaders = configureHttpHeaders();
4138
HttpRequest httpRequest = new HttpRequest(
@@ -64,28 +61,24 @@ private Map<String, String> configureHttpHeaders() {
6461
return httpHeaders;
6562
}
6663

67-
private HTTPResponse createOauthHttpResponseFromHttpResponse(IHttpResponse httpResponse)
64+
private HttpResponse createOauthHttpResponseFromHttpResponse(IHttpResponse httpResponse)
6865
throws IOException {
6966

70-
final HTTPResponse response = new HTTPResponse(httpResponse.statusCode());
67+
final HttpResponse response = new HttpResponse();
68+
response.statusCode(httpResponse.statusCode());
7169

7270
final String location = HttpUtils.headerValue(httpResponse.headers(), "Location");
7371
if (!StringHelper.isBlank(location)) {
7472
try {
75-
response.setLocation(new URI(location));
73+
response.addHeader("Location", new URI(location).toString());
7674
} catch (URISyntaxException e) {
7775
throw new IOException("Invalid location URI " + location, e);
7876
}
7977
}
8078

81-
try {
82-
String contentType = HttpUtils.headerValue(httpResponse.headers(), "Content-Type");
83-
if (!StringHelper.isBlank(contentType)) {
84-
response.setContentType(contentType);
85-
}
86-
} catch (final ParseException e) {
87-
throw new IOException("Couldn't parse Content-Type header: "
88-
+ e.getMessage(), e);
79+
String contentType = HttpUtils.headerValue(httpResponse.headers(), "Content-Type");
80+
if (!StringHelper.isBlank(contentType)) {
81+
response.addHeader("Content-Type", contentType);
8982
}
9083

9184
Map<String, List<String>> headers = httpResponse.headers();
@@ -95,14 +88,14 @@ private HTTPResponse createOauthHttpResponseFromHttpResponse(IHttpResponse httpR
9588
continue;
9689
}
9790

98-
String headerValue = response.getHeaderValue(header.getKey());
99-
if (headerValue == null || StringHelper.isBlank(headerValue)) {
100-
response.setHeader(header.getKey(), header.getValue().toArray(new String[0]));
91+
List<String> headerValue = response.getHeader((header.getKey()));
92+
if (headerValue == null) {
93+
response.addHeader(header.getKey(), header.getValue().toArray(new String[0]));
10194
}
10295
}
10396

10497
if (!StringHelper.isBlank(httpResponse.body())) {
105-
response.setContent(httpResponse.body());
98+
response.body(httpResponse.body());
10699
}
107100
return response;
108101
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import com.nimbusds.oauth2.sdk.ParseException;
77
import com.nimbusds.oauth2.sdk.SerializeException;
8-
import com.nimbusds.oauth2.sdk.http.HTTPResponse;
98
import com.nimbusds.oauth2.sdk.util.URLUtils;
109
import com.nimbusds.openid.connect.sdk.token.OIDCTokens;
1110
import org.slf4j.Logger;
@@ -37,7 +36,7 @@ AuthenticationResult executeTokenRequest() throws ParseException, IOException {
3736

3837
log.debug("Sending token request to: {}", requestAuthority.canonicalAuthorityUrl());
3938
OAuthHttpRequest oAuthHttpRequest = createOauthHttpRequest();
40-
HTTPResponse oauthHttpResponse = oAuthHttpRequest.send();
39+
HttpResponse oauthHttpResponse = oAuthHttpRequest.send();
4140
return createAuthenticationResultFromOauthHttpResponse(oauthHttpResponse);
4241
}
4342

@@ -116,10 +115,10 @@ private void addJWTBearerAssertionParams(Map<String, List<String>> queryParamete
116115
}
117116

118117
private AuthenticationResult createAuthenticationResultFromOauthHttpResponse(
119-
HTTPResponse oauthHttpResponse) throws ParseException {
118+
HttpResponse oauthHttpResponse) throws ParseException {
120119
AuthenticationResult result;
121120

122-
if (oauthHttpResponse.getStatusCode() == HTTPResponse.SC_OK) {
121+
if (oauthHttpResponse.statusCode() == HttpHelper.HTTP_STATUS_200) {
123122
final TokenResponse response = TokenResponse.parseHttpResponse(oauthHttpResponse);
124123

125124
OIDCTokens tokens = response.getOIDCTokens();
@@ -180,7 +179,7 @@ private AuthenticationResult createAuthenticationResultFromOauthHttpResponse(
180179

181180
} else {
182181
// http codes indicating that STS did not log request
183-
if (oauthHttpResponse.getStatusCode() == HttpHelper.HTTP_STATUS_429 || oauthHttpResponse.getStatusCode() >= HttpHelper.HTTP_STATUS_500) {
182+
if (oauthHttpResponse.statusCode() == HttpHelper.HTTP_STATUS_429 || oauthHttpResponse.statusCode() >= HttpHelper.HTTP_STATUS_500) {
184183
serviceBundle.getServerSideTelemetry().previousRequests.putAll(
185184
serviceBundle.getServerSideTelemetry().previousRequestInProgress);
186185
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package com.microsoft.aad.msal4j;
55

66
import com.nimbusds.oauth2.sdk.ParseException;
7-
import com.nimbusds.oauth2.sdk.http.HTTPResponse;
87
import com.nimbusds.oauth2.sdk.token.AccessToken;
98
import com.nimbusds.oauth2.sdk.token.RefreshToken;
109
import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
@@ -33,11 +32,13 @@ class TokenResponse extends OIDCTokenResponse {
3332
this.foci = foci;
3433
}
3534

36-
static TokenResponse parseHttpResponse(final HTTPResponse httpResponse) throws ParseException {
35+
static TokenResponse parseHttpResponse(final HttpResponse httpResponse) throws ParseException {
3736

38-
httpResponse.ensureStatusCode(HTTPResponse.SC_OK);
37+
if (httpResponse.statusCode() != HttpHelper.HTTP_STATUS_200) {
38+
throw MsalServiceExceptionFactory.fromHttpResponse(httpResponse);
39+
}
3940

40-
final JSONObject jsonObject = httpResponse.getContentAsJSONObject();
41+
final JSONObject jsonObject = httpResponse.getBodyAsJson();
4142

4243
return parseJsonObject(jsonObject);
4344
}

0 commit comments

Comments
 (0)