Skip to content

Commit 734e96e

Browse files
Merge pull request #484 from microsoftgraph/feature/AuthProviderWithHosts
add ability to add custom hosts to TokenCredentialAuthProvider
2 parents 2b0d299 + 9d17a49 commit 734e96e

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,35 @@
66
import java.util.Locale;
77

88
import javax.annotation.Nonnull;
9+
import javax.annotation.Nullable;
910

1011
/**
1112
* Provides basic common methods for all authentication providers
1213
*/
1314
public abstract class BaseAuthenticationProvider implements IAuthenticationProvider {
1415
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"));
16+
private HashSet<String> customHosts;
17+
18+
/**
19+
* Allow the user to add custom hosts by passing in Array
20+
* @param customHosts custom hosts passed in by user.
21+
*/
22+
public void setCustomHosts(@Nonnull String[] customHosts) {
23+
if(this.customHosts == null){
24+
this.customHosts = new HashSet<String>();
25+
}
26+
for(String host: customHosts){
27+
this.customHosts.add(host.toLowerCase(Locale.ROOT));
28+
}
29+
}
30+
/**
31+
* Get the custom hosts set by user.
32+
* @return the custom hosts set by user.
33+
*/
34+
@Nullable
35+
public String[] getCustomHosts(){
36+
return customHosts.toArray(new String[customHosts.size()]);
37+
}
1538
/**
1639
* Determines whether a request should be authenticated or not based on it's url.
1740
* If you're implementing a custom provider, call that method first before getting the token
@@ -22,6 +45,6 @@ protected boolean shouldAuthenticateRequestWithUrl(@Nonnull final URL requestUrl
2245
if (requestUrl == null || !requestUrl.getProtocol().toLowerCase(Locale.ROOT).equals("https"))
2346
return false;
2447
final String hostName = requestUrl.getHost().toLowerCase(Locale.ROOT);
25-
return validGraphHostNames.contains(hostName);
48+
return customHosts == null ? (validGraphHostNames.contains(hostName)) : (customHosts.contains(hostName));
2649
}
2750
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class TokenCredentialAuthProvider extends BaseAuthenticationProvider {
2121
private final TokenRequestContext context;
2222
/** Default scope to use when no scopes are provided */
2323
private static final String DEFAULT_GRAPH_SCOPE = "https://graph.microsoft.com/.default";
24+
2425
/**
2526
* Creates an Authentication provider using a passed in TokenCredential
2627
*

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.microsoft.graph.authentication;
22

3-
import static org.junit.jupiter.api.Assertions.assertFalse;
4-
import static org.junit.jupiter.api.Assertions.assertTrue;
5-
63
import java.net.MalformedURLException;
74
import java.net.URL;
85
import java.util.Arrays;
@@ -11,6 +8,8 @@
118

129
import org.junit.jupiter.api.Test;
1310

11+
import static org.junit.jupiter.api.Assertions.*;
12+
1413
public class BaseAuthenticationProviderTest {
1514
final BaseAuthenticationProvider authProvider = new BaseAuthenticationProvider() {
1615

@@ -67,4 +66,18 @@ public void providerDoesNotAddTokenOnNullUrls() throws MalformedURLException {
6766
//Assert
6867
assertFalse(result);
6968
}
69+
@Test
70+
public void providerAddsTokenToCustomHosts() throws MalformedURLException {
71+
//Arrange
72+
final URL url = new URL("https://localhost.com");
73+
authProvider.setCustomHosts(new String[]{"localHost.com"});
74+
75+
//Act
76+
final boolean result = authProvider.shouldAuthenticateRequestWithUrl(url);
77+
78+
//Assert
79+
assertTrue(result);
80+
assertEquals(authProvider.getCustomHosts().length, 1);
81+
assertEquals(authProvider.getCustomHosts()[0], "localhost.com");
82+
}
7083
}

0 commit comments

Comments
 (0)