11package com .microsoft .graph .authentication ;
22
3- import com .azure .core .credential .AccessToken ;
43import com .azure .core .credential .TokenCredential ;
54import com .azure .core .credential .TokenRequestContext ;
6- import com .microsoft .graph .exceptions .AuthenticationException ;
7- import com .microsoft .graph .http .IHttpRequest ;
8-
9- import okhttp3 .HttpUrl ;
10- import okhttp3 .Request ;
115
126import java .net .URL ;
13- import java .time .Duration ;
147import java .util .Arrays ;
158import java .util .List ;
9+ import java .util .concurrent .CompletableFuture ;
1610
1711import javax .annotation .Nonnull ;
1812
1913/**
2014 * An implementation of the Authentication Provider with Azure-identity
2115 */
22- public class TokenCredentialAuthProvider extends BaseAuthenticationProvider <Request > {
23- /** The bearer value for the authorization request header, contains a space */
24- protected static final String BEARER = "Bearer " ;
25- /** The authorization request header name */
26- protected static final String AUTHORIZATION_HEADER = "Authorization" ;
27-
16+ public class TokenCredentialAuthProvider extends BaseAuthenticationProvider {
2817 /** TokenCredential expected from user */
2918 private final TokenCredential tokenCredential ;
3019 /** Context options which can be optionally set by the user */
3120 private final TokenRequestContext context ;
32- /** maximum delay to wait for token obtention */
33- private final Duration tokenBlockTimeout ;
3421 /** Default scope to use when no scopes are provided */
3522 private static final String DEFAULT_GRAPH_SCOPE = "https://graph.microsoft.com/.default" ;
36- /** Default timeout for token obtention, set to 10 minutes in case of interactive flows to give time to the end user */
37- private static final long DEFAULT_TOKEN_TIMEOUT = 10 ;
3823 /**
3924 * Creates an Authentication provider using a passed in TokenCredential
4025 *
@@ -44,88 +29,38 @@ public TokenCredentialAuthProvider(@Nonnull final TokenCredential tokenCredentia
4429 this (Arrays .asList (new String [] {DEFAULT_GRAPH_SCOPE }), tokenCredential );
4530 }
4631
47- /**
48- * Creates an Authentication provider using a TokenCredential and list of scopes with default timeout (10 minutes)
49- *
50- * @param tokenCredential Credential object inheriting the TokenCredential interface used to instantiate the Auth Provider
51- * @param scopes Specified desired scopes of the Auth Provider
52- */
53- public TokenCredentialAuthProvider (@ Nonnull final List <String > scopes , @ Nonnull final TokenCredential tokenCredential ) {
54- this (scopes , Duration .ofMinutes (DEFAULT_TOKEN_TIMEOUT ), tokenCredential );
55- }
56-
5732 /**
5833 * Creates an Authentication provider using a TokenCredential and list of scopes
5934 *
6035 * @param tokenCredential Credential object inheriting the TokenCredential interface used to instantiate the Auth Provider
6136 * @param scopes Specified desired scopes of the Auth Provider
62- * @param tokenObtentionTimeout Maximum time to wait for token obtention. Default 10 minutes. Use lower value on application with stable connectivity and no user interactions.
6337 */
64- public TokenCredentialAuthProvider (@ Nonnull final List <String > scopes , @ Nonnull final Duration tokenObtentionTimeout , @ Nonnull final TokenCredential tokenCredential ) {
38+ public TokenCredentialAuthProvider (@ Nonnull final List <String > scopes , @ Nonnull final TokenCredential tokenCredential ) {
6539 if (tokenCredential == null ) {
6640 throw new IllegalArgumentException ("tokenCredential parameter cannot be null." );
6741 }
68- if (tokenObtentionTimeout == null ) {
69- throw new IllegalArgumentException ("tokenObtentionTimout parameter cannot be null" );
70- }
7142 if (scopes == null || scopes .isEmpty ()) {
7243 throw new IllegalArgumentException ("scopes parameter cannot be null or empty" );
7344 }
7445 this .context = new TokenRequestContext ();
7546 this .context .setScopes (scopes );
7647 this .tokenCredential = tokenCredential ;
77- this .tokenBlockTimeout = tokenObtentionTimeout ;
78- }
79-
80- /**
81- * Authenticates the request
82- *
83- * @param request the request to authenticate
84- */
85- @ Override
86- public void authenticateRequest (@ Nonnull final IHttpRequest request ) throws AuthenticationException {
87- if (request == null ) {
88- throw new IllegalArgumentException ("request parameter cannot be null." );
89- }
90- final URL requestUrl = request .getRequestUrl ();
91- if (requestUrl != null && ShouldAuthenticateRequest (requestUrl )) {
92- request .addHeader (AUTHORIZATION_HEADER , BEARER + getAccessToken ());
93- }
94- }
95-
96- /**
97- * Authenticates the request
98- *
99- * @param request the request to authenticate
100- * @return Request with Authorization header added to it
101- */
102- @ Override
103- @ Nonnull
104- public Request authenticateRequest (@ Nonnull final Request request ) throws AuthenticationException {
105- if (request == null ) {
106- throw new IllegalArgumentException ("request parameter cannot be null." );
107- }
108- final HttpUrl requestUrl = request .url ();
109- if (requestUrl != null && ShouldAuthenticateRequest (requestUrl .url ())) {
110- return request .newBuilder ()
111- .addHeader (AUTHORIZATION_HEADER , BEARER + getAccessToken ())
112- .build ();
113- } else {
114- return request ;
115- }
11648 }
11749
11850 /**
11951 * Returns an AccessToken as a string
12052 *
12153 * @return String representing the retrieved AccessToken
12254 */
123- private String getAccessToken () throws AuthenticationException {
124- try {
125- final AccessToken token = this .tokenCredential .getToken (this .context ).block (this .tokenBlockTimeout );
126- return token .getToken ();
127- } catch (RuntimeException e ) {
128- throw new AuthenticationException ("Authentication failed or timed out." , e );
129- }
55+ public CompletableFuture <String > getAuthorizationTokenAsync (@ Nonnull final URL requestUrl ) {
56+ if (requestUrl == null )
57+ throw new IllegalArgumentException ("requestUrl parameter cannot be null" );
58+ else if (shouldAuthenticateRequestWithUrl (requestUrl ))
59+ return this .tokenCredential
60+ .getToken (this .context )
61+ .toFuture ()
62+ .thenApply (resp -> resp .getToken ());
63+ else
64+ return CompletableFuture .completedFuture ((String )null );
13065 }
13166}
0 commit comments