Skip to content

Commit b9dbb18

Browse files
committed
- adds missing defensive programming
- reorders provider ctor params for a better DevX - adds constants with documentation - adds defaults when values are not provided - addresses other comments
1 parent ac1c59f commit b9dbb18

1 file changed

Lines changed: 38 additions & 28 deletions

File tree

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

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.azure.core.credential.TokenRequestContext;
66
import com.microsoft.graph.exceptions.AuthenticationException;
77
import com.microsoft.graph.http.IHttpRequest;
8+
9+
import okhttp3.HttpUrl;
810
import okhttp3.Request;
911

1012
import java.net.MalformedURLException;
@@ -22,42 +24,33 @@
2224
*/
2325
public class TokenCredentialAuthProvider implements IAuthenticationProvider<Request> {
2426

25-
//TokenCredential expected from user
26-
private TokenCredential tokenCredential;
27-
//Context options which can be optionally set by the user
28-
private TokenRequestContext context;
29-
// maximum delay to wait for token obtention
30-
private Duration tokenBlockTimeout;
31-
32-
33-
//TODO: Upon further review from Peter, this should include a Null check in the case that this is the first request
34-
//There should be just one constructor as scopes should probably not be optional
27+
/** TokenCredential expected from user */
28+
private final TokenCredential tokenCredential;
29+
/** Context options which can be optionally set by the user */
30+
private final TokenRequestContext context;
31+
/** maximum delay to wait for token obtention */
32+
private final Duration tokenBlockTimeout;
33+
/** Default scope to use when no scopes are provided */
34+
private static final String DEFAULT_GRAPH_SCOPE = "https://graph.microsoft.com/.default";
35+
/** Default timeout for token obtention, set to 10 minutes in case of interactive flows to give time to the end user */
36+
private static final long DEFAULT_TOKEN_TIMEOUT = 10;
3537
/**
3638
* Creates an Authentication provider using a passed in TokenCredential
3739
*
3840
* @param tokenCredential Credential object inheriting the TokenCredential interface used to instantiate the Auth Provider
39-
* @throws AuthenticationException exception occurs if the TokenCredential parameter is null
4041
*/
41-
public TokenCredentialAuthProvider(@Nonnull final TokenCredential tokenCredential) throws AuthenticationException {
42-
if(tokenCredential == null) {
43-
throw new AuthenticationException(new IllegalArgumentException("tokenCredential parameter cannot be null."));
44-
}
45-
46-
this.tokenCredential = tokenCredential;
47-
this.context = new TokenRequestContext();
48-
this.tokenBlockTimeout = Duration.ofMinutes(10);
42+
public TokenCredentialAuthProvider(@Nonnull final TokenCredential tokenCredential) {
43+
this(tokenCredential, Arrays.asList(new String[] {DEFAULT_GRAPH_SCOPE}));
4944
}
5045

5146
/**
5247
* Creates an Authentication provider using a TokenCredential and list of scopes
5348
*
5449
* @param tokenCredential Credential object inheriting the TokenCredential interface used to instantiate the Auth Provider
5550
* @param scopes Specified desired scopes of the Auth Provider
56-
* @throws AuthenticationException exception occurs if the TokenCredential parameter is null
5751
*/
58-
public TokenCredentialAuthProvider(@Nonnull final List<String> scopes, @Nonnull final TokenCredential tokenCredential) throws AuthenticationException {
59-
this(tokenCredential);
60-
this.context.setScopes(scopes);
52+
public TokenCredentialAuthProvider(@Nonnull final TokenCredential tokenCredential, @Nonnull final List<String> scopes) {
53+
this(tokenCredential, scopes, Duration.ofMinutes(DEFAULT_TOKEN_TIMEOUT));
6154
}
6255

6356
/**
@@ -66,11 +59,20 @@ public TokenCredentialAuthProvider(@Nonnull final List<String> scopes, @Nonnull
6659
* @param tokenCredential Credential object inheriting the TokenCredential interface used to instantiate the Auth Provider
6760
* @param scopes Specified desired scopes of the Auth Provider
6861
* @param tokenObtentionTimeout Maximum time to wait for token obtention. Default 10 minutes. Use lower value on application with stable connectivity and no user interactions.
69-
* @throws AuthenticationException exception occurs if the TokenCredential parameter is null
7062
*/
71-
public TokenCredentialAuthProvider(@Nonnull final List<String> scopes, @Nonnull final Duration tokenObtentionTimeout, @Nonnull final TokenCredential tokenCredential) throws AuthenticationException {
72-
this(tokenCredential);
63+
public TokenCredentialAuthProvider(@Nonnull final TokenCredential tokenCredential, @Nonnull final List<String> scopes, @Nonnull final Duration tokenObtentionTimeout) {
64+
if(tokenCredential == null) {
65+
throw new IllegalArgumentException("tokenCredential parameter cannot be null.");
66+
}
67+
if(tokenObtentionTimeout == null) {
68+
throw new IllegalArgumentException("tokenObtentionTimout parameter cannot be null");
69+
}
70+
if(scopes == null || scopes.isEmpty()) {
71+
throw new IllegalArgumentException("scopes parameter cannot be null or empty");
72+
}
73+
this.context = new TokenRequestContext();
7374
this.context.setScopes(scopes);
75+
this.tokenCredential = tokenCredential;
7476
this.tokenBlockTimeout = tokenObtentionTimeout;
7577
}
7678

@@ -81,7 +83,11 @@ public TokenCredentialAuthProvider(@Nonnull final List<String> scopes, @Nonnull
8183
*/
8284
@Override
8385
public void authenticateRequest(@Nonnull final IHttpRequest request) throws AuthenticationException {
84-
if(ShouldAuthenticateRequest(request.getRequestUrl().toString())) {
86+
if(request == null) {
87+
throw new IllegalArgumentException("request parameter cannot be null.");
88+
}
89+
final URL requestUrl = request.getRequestUrl();
90+
if(requestUrl != null && ShouldAuthenticateRequest(requestUrl.toString())) {
8591
request.addHeader(InternalAuthConstants.AUTHORIZATION_HEADER, InternalAuthConstants.BEARER + getAccessToken());
8692
}
8793
}
@@ -95,7 +101,11 @@ public void authenticateRequest(@Nonnull final IHttpRequest request) throws Auth
95101
@Override
96102
@Nonnull
97103
public Request authenticateRequest(@Nonnull final Request request) throws AuthenticationException {
98-
if(ShouldAuthenticateRequest(request.url().toString())) {
104+
if(request == null) {
105+
throw new IllegalArgumentException("request parameter cannot be null.");
106+
}
107+
final HttpUrl requestUrl = request.url();
108+
if(requestUrl != null && ShouldAuthenticateRequest(requestUrl.toString())) {
99109
return request.newBuilder()
100110
.addHeader(InternalAuthConstants.AUTHORIZATION_HEADER, InternalAuthConstants.BEARER + getAccessToken())
101111
.build();

0 commit comments

Comments
 (0)