Skip to content

Commit 4621ba8

Browse files
committed
feat: add GraphClientFactory method using TokenCredential
1 parent 0e0f2cd commit 4621ba8

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.microsoft.graph.core.requests;
22

3+
import com.azure.core.credential.TokenCredential;
34
import com.microsoft.graph.core.CoreConstants;
5+
import com.microsoft.graph.core.authentication.AzureIdentityAccessTokenProvider;
46
import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler;
57
import com.microsoft.graph.core.requests.options.GraphClientOption;
68
import com.microsoft.kiota.RequestOption;
@@ -51,6 +53,27 @@ public static OkHttpClient.Builder create(@Nonnull final List<Interceptor> inter
5153
return create(new GraphClientOption(), interceptors.toArray(new Interceptor[0]));
5254
}
5355

56+
/**
57+
* OkHttpClient Builder for Graph with authentication middleware that uses the specified TokenCredential.
58+
* @param tokenCredential the TokenCredential to use for authentication.
59+
* @return an OkHttpClient Builder instance.
60+
*/
61+
@Nonnull
62+
public static OkHttpClient.Builder create(@Nonnull final TokenCredential tokenCredential) {
63+
return create(tokenCredential, new RequestOption[0]);
64+
}
65+
66+
/**
67+
* OkHttpClient Builder for Graph with authentication middleware that uses the specified TokenCredential and RequestOptions to override default graph interceptors.
68+
* @param tokenCredential the TokenCredential to use for authentication.
69+
* @param requestOptions custom request options to override default graph interceptors
70+
* @return an OkHttpClient Builder instance.
71+
*/
72+
@Nonnull
73+
public static OkHttpClient.Builder create(@Nonnull final TokenCredential tokenCredential, @Nonnull final RequestOption[] requestOptions) {
74+
return create(new BaseBearerTokenAuthenticationProvider(new AzureIdentityAccessTokenProvider(tokenCredential)), requestOptions);
75+
}
76+
5477
/**
5578
* OkHttpClient Builder for Graph with specified AuthenticationProvider.
5679
* Adds an AuthorizationHandler to the OkHttpClient Builder.

src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.junit.jupiter.api.Assertions;
2020
import org.junit.jupiter.api.Test;
2121

22+
import com.azure.core.credential.AccessToken;
23+
import com.azure.core.credential.TokenCredential;
2224
import com.microsoft.graph.core.authentication.AzureIdentityAccessTokenProvider;
2325
import com.microsoft.graph.core.authentication.AzureIdentityAuthenticationProvider;
2426
import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler;
@@ -39,6 +41,7 @@
3941
import okhttp3.OkHttpClient;
4042
import okhttp3.Request;
4143
import okhttp3.Response;
44+
import reactor.core.publisher.Mono;
4245

4346
class GraphClientFactoryTest {
4447

@@ -104,6 +107,22 @@ void testCreateWithAuthenticationProvider() throws IOException {
104107
assertEquals("Bearer " + ACCESS_TOKEN_STRING, response.request().header("Authorization"));
105108
}
106109

110+
@Test
111+
void testCreateWithTokenCredential() throws IOException {
112+
final TokenCredential tokenCredential = mock(TokenCredential.class);
113+
when(tokenCredential.getTokenSync(any())).thenReturn(new AccessToken(ACCESS_TOKEN_STRING, null));
114+
when(tokenCredential.getToken(any())).thenReturn(Mono.just(new AccessToken(ACCESS_TOKEN_STRING, null)));
115+
116+
final OkHttpClient graphClient = GraphClientFactory.create(tokenCredential).addInterceptor(new MockResponseHandler()).build();
117+
Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/me").build();
118+
Response response = graphClient.newCall(request).execute();
119+
120+
assertEquals(200, response.code());
121+
assertNotNull(response.request());
122+
assertTrue(response.request().headers().names().contains("Authorization"));
123+
assertEquals("Bearer " + ACCESS_TOKEN_STRING, response.request().header("Authorization"));
124+
}
125+
107126
@Test
108127
void testCreateWithAuthenticationProviderAndCustomRequestOptions() throws IOException {
109128
final BaseBearerTokenAuthenticationProvider mockAuthenticationProvider =

0 commit comments

Comments
 (0)