Skip to content

Commit 7542277

Browse files
committed
- moves some of the resuable methods for auth provider in a common ancestor
1 parent 4a379b6 commit 7542277

2 files changed

Lines changed: 28 additions & 11 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.microsoft.graph.authentication;
2+
3+
import java.net.URL;
4+
import java.util.Arrays;
5+
import java.util.HashSet;
6+
import java.util.Locale;
7+
8+
import javax.annotation.Nonnull;
9+
10+
/**
11+
* Provides basic common methods for all authentication providers
12+
*/
13+
public abstract class BaseAuthenticationProvider<T> implements IAuthenticationProvider<T> {
14+
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"));
15+
/**
16+
* Determines whether a request should be authenticated or not based on it's url.
17+
* If you're implementing a custom provider, call that method first before getting the token
18+
* @param requestUrl request URL that is about to be executed
19+
* @return whether a token should be attached to this request
20+
*/
21+
protected boolean ShouldAuthenticateRequest(@Nonnull final URL requestUrl) {
22+
if(requestUrl == null || !requestUrl.getProtocol().toLowerCase(Locale.ROOT).equals("https"))
23+
return false;
24+
final String hostName = requestUrl.getHost().toLowerCase(Locale.getDefault());
25+
return validGraphHostNames.contains(hostName);
26+
}
27+
}

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212
import java.net.URL;
1313
import java.time.Duration;
1414
import java.util.Arrays;
15-
import java.util.HashSet;
1615
import java.util.List;
17-
import java.util.Locale;
1816

1917
import javax.annotation.Nonnull;
2018

2119
/**
2220
* An implementation of the Authentication Provider with Azure-identity
2321
*/
24-
public class TokenCredentialAuthProvider implements IAuthenticationProvider<Request> {
22+
public class TokenCredentialAuthProvider extends BaseAuthenticationProvider<Request> {
2523
/** The bearer value for the authorization request header, contains a space */
2624
protected static final String BEARER = "Bearer ";
2725
/** The authorization request header name */
@@ -117,14 +115,6 @@ public Request authenticateRequest(@Nonnull final Request request) throws Authen
117115
}
118116
}
119117

120-
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"));
121-
private boolean ShouldAuthenticateRequest(@Nonnull final URL requestUrl) {
122-
if(requestUrl == null || !requestUrl.getProtocol().toLowerCase(Locale.ROOT).equals("https"))
123-
return false;
124-
final String hostName = requestUrl.getHost().toLowerCase(Locale.getDefault());
125-
return validGraphHostNames.contains(hostName);
126-
}
127-
128118
/**
129119
* Returns an AccessToken as a string
130120
*

0 commit comments

Comments
 (0)