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

Commit 4ce8677

Browse files
authored
Merge pull request #478 from microsoft/trboehre/oauth
Adding OAuthClient to TurnState
2 parents fb21b86 + 67dfca1 commit 4ce8677

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public abstract class BotAdapter {
4444
*/
4545
public static final String OAUTH_SCOPE_KEY = "Microsoft.Bot.Builder.BotAdapter.OAuthScope";
4646

47+
/**
48+
* Key to store bot oauth client.
49+
*/
50+
public static final String OAUTH_CLIENT_KEY = "OAuthClient";
51+
4752
/**
4853
* The collection of middleware in the adapter's pipeline.
4954
*/

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

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,7 @@ public CompletableFuture<Void> continueConversation(
327327
BotCallbackHandler callback
328328
) {
329329
return continueConversation(
330-
claimsIdentity,
331-
reference,
332-
getBotFrameworkOAuthScope(),
333-
callback
330+
claimsIdentity, reference, getBotFrameworkOAuthScope(), callback
334331
);
335332
}
336333

@@ -380,9 +377,7 @@ public CompletableFuture<Void> continueConversation(
380377
context.getTurnState().add(OAUTH_SCOPE_KEY, audience);
381378

382379
pipelineResult = createConnectorClient(
383-
reference.getServiceUrl(),
384-
claimsIdentity,
385-
audience
380+
reference.getServiceUrl(), claimsIdentity, audience
386381
).thenCompose(connectorClient -> {
387382
context.getTurnState().add(CONNECTOR_CLIENT_KEY, connectorClient);
388383
return runPipeline(context, callback);
@@ -430,11 +425,7 @@ public CompletableFuture<InvokeResponse> processActivity(
430425
BotAssert.activityNotNull(activity);
431426

432427
return JwtTokenValidation.authenticateRequest(
433-
activity,
434-
authHeader,
435-
credentialProvider,
436-
channelProvider,
437-
authConfiguration
428+
activity, authHeader, credentialProvider, channelProvider, authConfiguration
438429
).thenCompose(claimsIdentity -> processActivity(claimsIdentity, activity, callback));
439430
}
440431

@@ -897,7 +888,7 @@ public CompletableFuture<TokenResponse> getUserToken(
897888
|| StringUtils.isEmpty(context.getActivity().getFrom().getId())
898889
) {
899890
throw new IllegalArgumentException(
900-
"BotFrameworkAdapter.GetuserToken(): missing from or from.id"
891+
"BotFrameworkAdapter.getUserToken(): missing from or from.id"
901892
);
902893
}
903894

@@ -908,10 +899,8 @@ public CompletableFuture<TokenResponse> getUserToken(
908899
return createOAuthClient(context, null).thenCompose(oAuthClient -> {
909900
return oAuthClient.getUserToken()
910901
.getToken(
911-
context.getActivity().getFrom().getId(),
912-
connectionName,
913-
context.getActivity().getChannelId(),
914-
magicCode
902+
context.getActivity().getFrom().getId(), connectionName,
903+
context.getActivity().getChannelId(), magicCode
915904
);
916905
});
917906
}
@@ -1053,8 +1042,7 @@ public CompletableFuture<Void> signOutUser(
10531042
return createOAuthClient(context, null).thenCompose(oAuthClient -> {
10541043
return oAuthClient.getUserToken()
10551044
.signOut(
1056-
context.getActivity().getFrom().getId(),
1057-
connectionName,
1045+
context.getActivity().getFrom().getId(), connectionName,
10581046
context.getActivity().getChannelId()
10591047
);
10601048
}).thenApply(signOutResult -> null);
@@ -1269,11 +1257,7 @@ public CompletableFuture<Void> createConversation(
12691257
}
12701258

12711259
return createConversation(
1272-
channelId,
1273-
serviceUrl,
1274-
credentials,
1275-
conversationParameters,
1276-
callback
1260+
channelId, serviceUrl, credentials, conversationParameters, callback
12771261
);
12781262
}
12791263

@@ -1300,7 +1284,6 @@ protected CompletableFuture<OAuthClient> createOAuthClient(
13001284
.equalsIgnoreCase(turnContext.getActivity().getChannelId(), Channels.EMULATOR)
13011285
&& credentialProvider.isAuthenticationDisabled().join()
13021286
) {
1303-
13041287
OAuthClientConfig.emulateOAuthCards = true;
13051288
}
13061289

@@ -1310,17 +1293,26 @@ protected CompletableFuture<OAuthClient> createOAuthClient(
13101293
? oAuthAppCredentials
13111294
: getAppCredentials(appId, oAuthScope).join();
13121295

1296+
OAuthClient oAuthClient = new RestOAuthClient(
1297+
OAuthClientConfig.emulateOAuthCards
1298+
? turnContext.getActivity().getServiceUrl()
1299+
: OAuthClientConfig.OAUTHENDPOINT,
1300+
credentials
1301+
);
1302+
1303+
// adding the oAuthClient into the TurnState
1304+
// TokenResolver.cs will use it get the correct credentials to poll for
1305+
// token for streaming scenario
1306+
turnContext.getTurnState().add(BotAdapter.OAUTH_CLIENT_KEY, oAuthClient);
1307+
13131308
if (OAuthClientConfig.emulateOAuthCards) {
13141309
// do not join task - we want this to run in the background.
1315-
OAuthClient oAuthClient =
1316-
new RestOAuthClient(turnContext.getActivity().getServiceUrl(), credentials);
13171310
return OAuthClientConfig
13181311
.sendEmulateOAuthCards(oAuthClient, OAuthClientConfig.emulateOAuthCards)
13191312
.thenApply(result -> oAuthClient);
13201313
}
13211314

1322-
return CompletableFuture
1323-
.completedFuture(new RestOAuthClient(OAuthClientConfig.OAUTHENDPOINT, credentials));
1315+
return CompletableFuture.completedFuture(oAuthClient);
13241316
}
13251317

13261318
/**
@@ -1394,8 +1386,7 @@ protected CompletableFuture<ConnectorClient> getOrCreateConnectorClient(
13941386
CompletableFuture<ConnectorClient> result = new CompletableFuture<>();
13951387

13961388
String clientKey = keyForConnectorClient(
1397-
serviceUrl,
1398-
usingAppCredentials != null ? usingAppCredentials.getAppId() : null,
1389+
serviceUrl, usingAppCredentials != null ? usingAppCredentials.getAppId() : null,
13991390
usingAppCredentials != null ? usingAppCredentials.oAuthScope() : null
14001391
);
14011392

@@ -1553,15 +1544,17 @@ public CompletableFuture<Void> onTurn(TurnContext turnContext, NextDelegate next
15531544
}
15541545

15551546
/**
1556-
* Get the AppCredentials cache. For unit testing.
1547+
* Get the AppCredentials cache. For unit testing.
1548+
*
15571549
* @return The AppCredentials cache.
15581550
*/
15591551
protected Map<String, AppCredentials> getCredentialsCache() {
15601552
return Collections.unmodifiableMap(appCredentialMap);
15611553
}
15621554

15631555
/**
1564-
* Get the ConnectorClient cache. For unit testing.
1556+
* Get the ConnectorClient cache. For unit testing.
1557+
*
15651558
* @return The ConnectorClient cache.
15661559
*/
15671560
protected Map<String, ConnectorClient> getConnectorClientCache() {

0 commit comments

Comments
 (0)