Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 0ca0f8b

Browse files
committed
Caching OAuthClient
1 parent a84bb9c commit 0ca0f8b

3 files changed

Lines changed: 33 additions & 42 deletions

File tree

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotFrameworkAdapter.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public class BotFrameworkAdapter extends BotAdapter
124124
*/
125125
private Map<String, ConnectorClient> connectorClients = new ConcurrentHashMap<>();
126126

127+
/**
128+
* OAuthClient cache.
129+
*/
130+
private Map<String, OAuthClient> oAuthClients = new ConcurrentHashMap<>();
131+
127132
/**
128133
* Initializes a new instance of the {@link BotFrameworkAdapter} class, using a
129134
* credential provider.
@@ -1287,31 +1292,37 @@ protected CompletableFuture<OAuthClient> createOAuthClient(
12871292
}
12881293

12891294
String appId = getBotAppId(turnContext);
1295+
String cacheKey = appId + (oAuthAppCredentials != null ? oAuthAppCredentials.getAppId() : "");
12901296
String oAuthScope = getBotFrameworkOAuthScope();
12911297
AppCredentials credentials = oAuthAppCredentials != null
12921298
? oAuthAppCredentials
12931299
: getAppCredentials(appId, oAuthScope).join();
12941300

1295-
OAuthClient oAuthClient = new RestOAuthClient(
1296-
OAuthClientConfig.emulateOAuthCards
1297-
? turnContext.getActivity().getServiceUrl()
1298-
: OAuthClientConfig.OAUTHENDPOINT,
1299-
credentials
1300-
);
1301+
OAuthClient client = oAuthClients.computeIfAbsent(cacheKey, key -> {
1302+
OAuthClient oAuthClient = new RestOAuthClient(
1303+
OAuthClientConfig.emulateOAuthCards
1304+
? turnContext.getActivity().getServiceUrl()
1305+
: OAuthClientConfig.OAUTHENDPOINT,
1306+
credentials
1307+
);
1308+
1309+
if (OAuthClientConfig.emulateOAuthCards) {
1310+
// do not join task - we want this to run in the background.
1311+
OAuthClientConfig
1312+
.sendEmulateOAuthCards(oAuthClient, OAuthClientConfig.emulateOAuthCards);
1313+
}
1314+
1315+
return oAuthClient;
1316+
});
13011317

13021318
// adding the oAuthClient into the TurnState
13031319
// TokenResolver.cs will use it get the correct credentials to poll for
13041320
// token for streaming scenario
1305-
turnContext.getTurnState().add(BotAdapter.OAUTH_CLIENT_KEY, oAuthClient);
1306-
1307-
if (OAuthClientConfig.emulateOAuthCards) {
1308-
// do not join task - we want this to run in the background.
1309-
return OAuthClientConfig
1310-
.sendEmulateOAuthCards(oAuthClient, OAuthClientConfig.emulateOAuthCards)
1311-
.thenApply(result -> oAuthClient);
1321+
if (turnContext.getTurnState().get(BotAdapter.OAUTH_CLIENT_KEY) == null) {
1322+
turnContext.getTurnState().add(BotAdapter.OAUTH_CLIENT_KEY, client);
13121323
}
13131324

1314-
return CompletableFuture.completedFuture(oAuthClient);
1325+
return CompletableFuture.completedFuture(client);
13151326
}
13161327

13171328
/**

samples/52.teams-messaging-extensions-search-auth-config/src/main/java/com/microsoft/bot/sample/teamssearchauth/SimpleAuthProvider.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

samples/52.teams-messaging-extensions-search-auth-config/src/main/java/com/microsoft/bot/sample/teamssearchauth/SimpleGraphClient.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
14
package com.microsoft.bot.sample.teamssearchauth;
25

36

@@ -14,7 +17,6 @@
1417
import java.util.List;
1518

1619
public class SimpleGraphClient {
17-
1820
private String token;
1921
public SimpleGraphClient(String token) {
2022
this.token = token;
@@ -34,18 +36,18 @@ public List<Message> searchMailInbox(String search) {
3436
return message.getCurrentPage().subList(0, 10);
3537
}
3638

37-
private IGraphServiceClient getAuthenticatedClient () {
38-
39+
private IGraphServiceClient getAuthenticatedClient() {
3940
// Create default logger to only log errors
4041
DefaultLogger logger = new DefaultLogger();
4142
logger.setLoggingLevel(LoggerLevel.ERROR);
4243

43-
SimpleAuthProvider authProvider = new SimpleAuthProvider(this.token);
4444
// Build a Graph client
4545
return GraphServiceClient.builder()
46-
.authenticationProvider(authProvider)
46+
.authenticationProvider(request -> {
47+
// Add the access token in the Authorization header
48+
request.addHeader("Authorization", "Bearer " + SimpleGraphClient.this.token);
49+
})
4750
.logger(logger)
4851
.buildClient();
4952
}
50-
5153
}

0 commit comments

Comments
 (0)