Skip to content

Commit 7d94eed

Browse files
authored
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
1 parent aec1cd0 commit 7d94eed

File tree

6 files changed

+30
-18
lines changed

6 files changed

+30
-18
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88
import java.util.List;
99
import java.util.Map;
1010

11-
import javax.mail.internet.ContentType;
12-
1311
import com.nimbusds.oauth2.sdk.SerializeException;
1412
import com.nimbusds.oauth2.sdk.auth.ClientAuthentication;
1513
import com.nimbusds.oauth2.sdk.auth.ClientAuthenticationMethod;
16-
import com.nimbusds.oauth2.sdk.http.CommonContentTypes;
1714
import com.nimbusds.oauth2.sdk.http.HTTPRequest;
1815
import com.nimbusds.oauth2.sdk.id.ClientID;
1916
import com.nimbusds.oauth2.sdk.util.URLUtils;
@@ -40,15 +37,15 @@ public void applyTo(HTTPRequest httpRequest) throws SerializeException {
4037
if (httpRequest.getMethod() != HTTPRequest.Method.POST)
4138
throw new SerializeException("The HTTP request method must be POST");
4239

43-
ContentType ct = httpRequest.getContentType();
40+
String ct = String.valueOf(httpRequest.getEntityContentType());
4441

4542
if (ct == null)
4643
throw new SerializeException("Missing HTTP Content-Type header");
4744

48-
if (!ct.match(CommonContentTypes.APPLICATION_URLENCODED))
45+
if (!ct.equals(HTTPContentType.ApplicationURLEncoded.contentType))
4946
throw new SerializeException(
5047
"The HTTP Content-Type header must be "
51-
+ CommonContentTypes.APPLICATION_URLENCODED);
48+
+ HTTPContentType.ApplicationURLEncoded.contentType);
5249

5350
Map<String, List<String>> params = httpRequest.getQueryParameters();
5451

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.aad.msal4j;
5+
6+
/**
7+
* Enum containing HTTP Content-Type header values
8+
*/
9+
enum HTTPContentType {
10+
11+
ApplicationURLEncoded("application/x-www-form-urlencoded; charset=UTF-8"),
12+
ApplicationJSON("application/json; charset=UTF-8");
13+
14+
public final String contentType;
15+
16+
HTTPContentType(String contentType) {
17+
this.contentType = contentType;
18+
}
19+
}

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

Lines changed: 1 addition & 2 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.CommonContentTypes;
87
import com.nimbusds.oauth2.sdk.http.HTTPRequest;
98
import com.nimbusds.oauth2.sdk.http.HTTPResponse;
109
import lombok.AccessLevel;
@@ -57,7 +56,7 @@ public HTTPResponse send() throws IOException {
5756
private Map<String, String> configureHttpHeaders(){
5857

5958
Map<String, String> httpHeaders = new HashMap<>(extraHeaderParams);
60-
httpHeaders.put("Content-Type", CommonContentTypes.APPLICATION_URLENCODED.toString());
59+
httpHeaders.put("Content-Type", HTTPContentType.ApplicationURLEncoded.contentType);
6160

6261
if (this.getAuthorization() != null) {
6362
httpHeaders.put("Authorization", this.getAuthorization());

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

Lines changed: 2 additions & 3 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.CommonContentTypes;
98
import com.nimbusds.oauth2.sdk.http.HTTPRequest;
109
import com.nimbusds.oauth2.sdk.http.HTTPResponse;
1110
import com.nimbusds.oauth2.sdk.util.URLUtils;
@@ -45,7 +44,7 @@ AuthenticationResult executeTokenRequest() throws ParseException,
4544
return createAuthenticationResultFromOauthHttpResponse(oauthHttpResponse);
4645
}
4746

48-
OAuthHttpRequest createOauthHttpRequest() throws SerializeException, MalformedURLException {
47+
OAuthHttpRequest createOauthHttpRequest() throws SerializeException, MalformedURLException, ParseException {
4948

5049
if (requestAuthority.tokenEndpointUrl() == null) {
5150
throw new SerializeException("The endpoint URI is not specified");
@@ -57,7 +56,7 @@ OAuthHttpRequest createOauthHttpRequest() throws SerializeException, MalformedUR
5756
msalRequest.headers().getReadonlyHeaderMap(),
5857
msalRequest.requestContext(),
5958
this.serviceBundle);
60-
oauthHttpRequest.setContentType(CommonContentTypes.APPLICATION_URLENCODED);
59+
oauthHttpRequest.setContentType(HTTPContentType.ApplicationURLEncoded.contentType);
6160

6261
final Map<String, List<String>> params = new HashMap<>(msalRequest.msalAuthorizationGrant().toParameters());
6362
if (msalRequest.application().clientCapabilities() != null) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
package com.microsoft.aad.msal4j;
55

6-
import com.nimbusds.oauth2.sdk.http.CommonContentTypes;
76
import com.nimbusds.oauth2.sdk.http.HTTPResponse;
87
import org.easymock.Capture;
98
import org.easymock.EasyMock;
@@ -211,7 +210,7 @@ public void executeAcquireDeviceCode_AuthenticaionPendingErrorReturned_Authentic
211210
"\"correlation_id\":\"ff60101b-cb23-4a52-82cb-9966f466327a\"}";
212211

213212
httpResponse.setContent(content);
214-
httpResponse.setContentType(CommonContentTypes.APPLICATION_JSON);
213+
httpResponse.setContentType(HTTPContentType.ApplicationJSON.contentType);
215214

216215
EasyMock.expect(request.createOauthHttpRequest()).andReturn(msalOAuthHttpRequest).times(1);
217216
EasyMock.expect(msalOAuthHttpRequest.send()).andReturn(httpResponse).times(1);

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.nimbusds.oauth2.sdk.ParseException;
77
import com.nimbusds.oauth2.sdk.SerializeException;
88
import com.nimbusds.oauth2.sdk.TokenErrorResponse;
9-
import com.nimbusds.oauth2.sdk.http.CommonContentTypes;
109
import com.nimbusds.oauth2.sdk.http.HTTPResponse;
1110
import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
1211
import org.easymock.EasyMock;
@@ -49,7 +48,7 @@ public void executeOAuthRequest_SCBadRequestErrorInvalidGrant_InteractionRequire
4948
"\"suberror\":\"basic_action\"," +
5049
"\"claims\":\"" + claims + "\"}";
5150
httpResponse.setContent(content);
52-
httpResponse.setContentType(CommonContentTypes.APPLICATION_JSON);
51+
httpResponse.setContentType(HTTPContentType.ApplicationJSON.contentType);
5352

5453
EasyMock.expect(request.createOauthHttpRequest()).andReturn(msalOAuthHttpRequest).times(1);
5554
EasyMock.expect(msalOAuthHttpRequest.send()).andReturn(httpResponse).times(1);
@@ -88,7 +87,7 @@ public void executeOAuthRequest_SCBadRequestErrorInvalidGrant_SubErrorFilteredSe
8887
"\"suberror\":\"client_mismatch\"," +
8988
"\"claims\":\"" + claims + "\"}";
9089
httpResponse.setContent(content);
91-
httpResponse.setContentType(CommonContentTypes.APPLICATION_JSON);
90+
httpResponse.setContentType(HTTPContentType.ApplicationJSON.contentType);
9291

9392
EasyMock.expect(request.createOauthHttpRequest()).andReturn(msalOAuthHttpRequest).times(1);
9493
EasyMock.expect(msalOAuthHttpRequest.send()).andReturn(httpResponse).times(1);
@@ -154,7 +153,7 @@ public void testConstructor() throws MalformedURLException,
154153

155154
@Test
156155
public void testToOAuthRequestNonEmptyCorrelationId()
157-
throws MalformedURLException, SerializeException, URISyntaxException {
156+
throws MalformedURLException, SerializeException, URISyntaxException, ParseException {
158157

159158
PublicClientApplication app = PublicClientApplication.builder("id").correlationId("corr-id").build();
160159

@@ -183,7 +182,7 @@ public void testToOAuthRequestNonEmptyCorrelationId()
183182
@Test
184183
public void testToOAuthRequestNullCorrelationId_NullClientAuth()
185184
throws MalformedURLException, SerializeException,
186-
URISyntaxException {
185+
URISyntaxException, ParseException {
187186

188187
PublicClientApplication app = PublicClientApplication.builder("id").correlationId("corr-id").build();
189188

0 commit comments

Comments
 (0)