Skip to content

Commit f4b1549

Browse files
committed
add ability to add custom hosts to TokenCredentialAuthProvider
1 parent 21b1d75 commit f4b1549

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import java.net.URL;
44
import java.util.Arrays;
55
import java.util.HashSet;
6+
import java.util.List;
67
import java.util.Locale;
78

89
import javax.annotation.Nonnull;
10+
import javax.annotation.Nullable;
911

1012
/**
1113
* Provides basic common methods for all authentication providers
@@ -18,10 +20,10 @@ public abstract class BaseAuthenticationProvider implements IAuthenticationProvi
1820
* @param requestUrl request URL that is about to be executed
1921
* @return whether a token should be attached to this request
2022
*/
21-
protected boolean shouldAuthenticateRequestWithUrl(@Nonnull final URL requestUrl) {
23+
protected boolean shouldAuthenticateRequestWithUrl(@Nonnull final URL requestUrl, @Nullable final List<String> customHosts) {
2224
if (requestUrl == null || !requestUrl.getProtocol().toLowerCase(Locale.ROOT).equals("https"))
2325
return false;
2426
final String hostName = requestUrl.getHost().toLowerCase(Locale.ROOT);
25-
return validGraphHostNames.contains(hostName);
27+
return customHosts == null ? (validGraphHostNames.contains(hostName)) : (customHosts.contains(hostName));
2628
}
2729
}

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ 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;
2224
/** Default scope to use when no scopes are provided */
2325
private static final String DEFAULT_GRAPH_SCOPE = "https://graph.microsoft.com/.default";
26+
2427
/**
2528
* Creates an Authentication provider using a passed in TokenCredential
2629
*
@@ -45,14 +48,35 @@ public TokenCredentialAuthProvider(@Nonnull final List<String> scopes, @Nonnull
4548
this.tokenCredential = Objects.requireNonNull(tokenCredential, "tokenCredential parameter cannot be null.");
4649
}
4750

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+
4872
/**
4973
* Returns an AccessToken as a string
5074
*
5175
* @return String representing the retrieved AccessToken
5276
*/
5377
@Nonnull
5478
public CompletableFuture<String> getAuthorizationTokenAsync(@Nonnull final URL requestUrl) {
55-
if(shouldAuthenticateRequestWithUrl(Objects.requireNonNull(requestUrl, "requestUrl parameter cannot be null")))
79+
if(shouldAuthenticateRequestWithUrl(Objects.requireNonNull(requestUrl, "requestUrl parameter cannot be null"), customHosts))
5680
return this.tokenCredential
5781
.getToken(this.context)
5882
.toFuture()

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

Lines changed: 15 additions & 4 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);
31+
final boolean result = authProvider.shouldAuthenticateRequestWithUrl(url, null);
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);
43+
final boolean result = authProvider.shouldAuthenticateRequestWithUrl(url, null);
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);
54+
final boolean result = authProvider.shouldAuthenticateRequestWithUrl(url, null);
5555

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

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

6767
//Assert
6868
assertFalse(result);
6969
}
70+
@Test
71+
public void providerAddsTokenToCustomHosts() throws MalformedURLException {
72+
//Arrange
73+
final URL url = new URL("https://localhost");
74+
75+
//Act
76+
final boolean result = authProvider.shouldAuthenticateRequestWithUrl(url, Arrays.asList("localhost"));
77+
78+
//Assert
79+
assertTrue(result);
80+
}
7081
}

0 commit comments

Comments
 (0)