Skip to content

Commit 2c29deb

Browse files
committed
- moves to a URL prototype and adds defensive programming
1 parent ab0cb98 commit 2c29deb

1 file changed

Lines changed: 6 additions & 12 deletions

File tree

‎src/main/java/com/microsoft/graph/authentication/TokenCredentialAuthProvider.java‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import okhttp3.HttpUrl;
1010
import okhttp3.Request;
1111

12-
import java.net.MalformedURLException;
1312
import java.net.URL;
1413
import java.time.Duration;
1514
import java.util.Arrays;
@@ -87,7 +86,7 @@ public void authenticateRequest(@Nonnull final IHttpRequest request) throws Auth
8786
throw new IllegalArgumentException("request parameter cannot be null.");
8887
}
8988
final URL requestUrl = request.getRequestUrl();
90-
if(requestUrl != null && ShouldAuthenticateRequest(requestUrl.toString())) {
89+
if(requestUrl != null && ShouldAuthenticateRequest(requestUrl)) {
9190
request.addHeader(InternalAuthConstants.AUTHORIZATION_HEADER, InternalAuthConstants.BEARER + getAccessToken());
9291
}
9392
}
@@ -105,7 +104,7 @@ public Request authenticateRequest(@Nonnull final Request request) throws Authen
105104
throw new IllegalArgumentException("request parameter cannot be null.");
106105
}
107106
final HttpUrl requestUrl = request.url();
108-
if(requestUrl != null && ShouldAuthenticateRequest(requestUrl.toString())) {
107+
if(requestUrl != null && ShouldAuthenticateRequest(requestUrl.url())) {
109108
return request.newBuilder()
110109
.addHeader(InternalAuthConstants.AUTHORIZATION_HEADER, InternalAuthConstants.BEARER + getAccessToken())
111110
.build();
@@ -115,16 +114,11 @@ public Request authenticateRequest(@Nonnull final Request request) throws Authen
115114
}
116115

117116
private static final HashSet<String> validGraphHostNames = new HashSet<>(Arrays.asList("graph.microsoft.com", "graph.microsoft.us", "dod-graph.microsoft.us", "graph.microsoft.de", "microsoftgraph.chinacloudapi.cn"));
118-
private boolean ShouldAuthenticateRequest(@Nonnull final String requestUrl) {
119-
try {
120-
final URL url = new URL(requestUrl);
121-
if(!url.getProtocol().toLowerCase(Locale.ROOT).equals("https"))
122-
return false;
123-
final String hostName = url.getHost().toLowerCase(Locale.getDefault());
124-
return validGraphHostNames.contains(hostName);
125-
} catch (MalformedURLException ex) {
117+
private boolean ShouldAuthenticateRequest(@Nonnull final URL requestUrl) {
118+
if(requestUrl == null || !requestUrl.getProtocol().toLowerCase(Locale.ROOT).equals("https"))
126119
return false;
127-
}
120+
final String hostName = requestUrl.getHost().toLowerCase(Locale.getDefault());
121+
return validGraphHostNames.contains(hostName);
128122
}
129123

130124
/**

0 commit comments

Comments
 (0)