Skip to content

Commit ae53946

Browse files
committed
add custom hosts to BaseAuthenticationProvider w/out breaking
1 parent f4b1549 commit ae53946

File tree

3 files changed

+30
-33
lines changed

3 files changed

+30
-33
lines changed

src/main/java/com/microsoft/graph/authentication/BaseAuthenticationProvider.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,43 @@
33
import java.net.URL;
44
import java.util.Arrays;
55
import java.util.HashSet;
6-
import java.util.List;
76
import java.util.Locale;
87

98
import javax.annotation.Nonnull;
10-
import javax.annotation.Nullable;
119

1210
/**
1311
* Provides basic common methods for all authentication providers
1412
*/
1513
public abstract class BaseAuthenticationProvider implements IAuthenticationProvider {
1614
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", "canary.graph.microsoft.com"));
15+
private HashSet<String> customHosts;
16+
17+
/**
18+
* Allow the user to add custom hosts by passing in Array
19+
* @param customHosts custom hosts passed in by user.
20+
*/
21+
public void setCustomHosts(String[] customHosts) {
22+
if(this.customHosts == null){
23+
this.customHosts = new HashSet<String>();
24+
}
25+
for(String host: customHosts){
26+
this.customHosts.add(host.toLowerCase(Locale.ROOT));
27+
}
28+
}
29+
/**
30+
* Get the custom hosts set by user.
31+
* @return the custom hosts set by user.
32+
*/
33+
public HashSet<String> getCustomHosts(){
34+
return (HashSet<String>) this.customHosts.clone();
35+
}
1736
/**
1837
* Determines whether a request should be authenticated or not based on it's url.
1938
* If you're implementing a custom provider, call that method first before getting the token
2039
* @param requestUrl request URL that is about to be executed
2140
* @return whether a token should be attached to this request
2241
*/
23-
protected boolean shouldAuthenticateRequestWithUrl(@Nonnull final URL requestUrl, @Nullable final List<String> customHosts) {
42+
protected boolean shouldAuthenticateRequestWithUrl(@Nonnull final URL requestUrl) {
2443
if (requestUrl == null || !requestUrl.getProtocol().toLowerCase(Locale.ROOT).equals("https"))
2544
return false;
2645
final String hostName = requestUrl.getHost().toLowerCase(Locale.ROOT);

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

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ public class TokenCredentialAuthProvider extends BaseAuthenticationProvider {
1919
private final TokenCredential tokenCredential;
2020
/** Context options which can be optionally set by the user */
2121
private final TokenRequestContext context;
22-
/** Custom hosts which can be optionally set by the user */
23-
private List<String> customHosts = null;
2422
/** Default scope to use when no scopes are provided */
2523
private static final String DEFAULT_GRAPH_SCOPE = "https://graph.microsoft.com/.default";
2624

@@ -48,35 +46,14 @@ public TokenCredentialAuthProvider(@Nonnull final List<String> scopes, @Nonnull
4846
this.tokenCredential = Objects.requireNonNull(tokenCredential, "tokenCredential parameter cannot be null.");
4947
}
5048

51-
/**
52-
* Creates an Authentication provider using a TokenCredential and a list of custom hosts
53-
* @param tokenCredential Credential object inheriting the TokenCredential interface used to instantiate the Auth Provider.
54-
* @param customHosts the user defined custom hosts.
55-
*/
56-
public TokenCredentialAuthProvider(@Nonnull final TokenCredential tokenCredential, @Nonnull final List<String> customHosts){
57-
this(Collections.singletonList(DEFAULT_GRAPH_SCOPE), tokenCredential);
58-
this.customHosts = customHosts;
59-
}
60-
61-
/**
62-
* Creates an Authentication provider using a TokenCredential, a list of scopes, and a list of custom hosts.
63-
* @param scopes Specified desired scopes of the Auth Provider
64-
* @param tokenCredential Credential object inheriting the TokenCredential interface used to instantiate the Auth Provider
65-
* @param customHosts the user defined custom hosts.
66-
*/
67-
public TokenCredentialAuthProvider(@Nonnull final List<String> scopes, @Nonnull final TokenCredential tokenCredential, @Nonnull final List<String> customHosts) {
68-
this(scopes, tokenCredential);
69-
this.customHosts = customHosts;
70-
}
71-
7249
/**
7350
* Returns an AccessToken as a string
7451
*
7552
* @return String representing the retrieved AccessToken
7653
*/
7754
@Nonnull
7855
public CompletableFuture<String> getAuthorizationTokenAsync(@Nonnull final URL requestUrl) {
79-
if(shouldAuthenticateRequestWithUrl(Objects.requireNonNull(requestUrl, "requestUrl parameter cannot be null"), customHosts))
56+
if(shouldAuthenticateRequestWithUrl(Objects.requireNonNull(requestUrl, "requestUrl parameter cannot be null")))
8057
return this.tokenCredential
8158
.getToken(this.context)
8259
.toFuture()

src/test/java/com/microsoft/graph/authentication/BaseAuthenticationProviderTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void providerAddsTokenOnAllNationalAndPublicClouds() throws MalformedURLE
2828
// Arrange
2929
final URL url = new URL("https://"+ hostName);
3030
// Act
31-
final boolean result = authProvider.shouldAuthenticateRequestWithUrl(url, null);
31+
final boolean result = authProvider.shouldAuthenticateRequestWithUrl(url);
3232

3333
// Assert
3434
assertTrue(result);
@@ -40,7 +40,7 @@ public void providerDoesNotAddTokenOnInvalidDomains() throws MalformedURLExcepti
4040
final URL url = new URL("https://localhost");
4141

4242
//Act
43-
final boolean result = authProvider.shouldAuthenticateRequestWithUrl(url, null);
43+
final boolean result = authProvider.shouldAuthenticateRequestWithUrl(url);
4444

4545
//Assert
4646
assertFalse(result);
@@ -51,7 +51,7 @@ public void providerDoesNotAddTokenOnInvalidProtocols() throws MalformedURLExcep
5151
final URL url = new URL("http://graph.microsoft.com");
5252

5353
//Act
54-
final boolean result = authProvider.shouldAuthenticateRequestWithUrl(url, null);
54+
final boolean result = authProvider.shouldAuthenticateRequestWithUrl(url);
5555

5656
//Assert
5757
assertFalse(result);
@@ -62,18 +62,19 @@ public void providerDoesNotAddTokenOnNullUrls() throws MalformedURLException {
6262
final URL url = (URL)null;
6363

6464
//Act
65-
final boolean result = authProvider.shouldAuthenticateRequestWithUrl(url, null);
65+
final boolean result = authProvider.shouldAuthenticateRequestWithUrl(url);
6666

6767
//Assert
6868
assertFalse(result);
6969
}
7070
@Test
7171
public void providerAddsTokenToCustomHosts() throws MalformedURLException {
7272
//Arrange
73-
final URL url = new URL("https://localhost");
73+
final URL url = new URL("https://localhost.com");
74+
authProvider.setCustomHosts(new String[]{"localHost.com"});
7475

7576
//Act
76-
final boolean result = authProvider.shouldAuthenticateRequestWithUrl(url, Arrays.asList("localhost"));
77+
final boolean result = authProvider.shouldAuthenticateRequestWithUrl(url);
7778

7879
//Assert
7980
assertTrue(result);

0 commit comments

Comments
 (0)