From 67dfca16392464411354be86740a3c46815e2a4b Mon Sep 17 00:00:00 2001 From: tracyboehrer Date: Thu, 23 Apr 2020 15:00:02 -0500 Subject: [PATCH] Adding OAuthClient to TurnState --- .../com/microsoft/bot/builder/BotAdapter.java | 5 ++ .../bot/builder/BotFrameworkAdapter.java | 59 ++++++++----------- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotAdapter.java b/libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotAdapter.java index 7bd25821..7a4a4cf5 100644 --- a/libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotAdapter.java +++ b/libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotAdapter.java @@ -44,6 +44,11 @@ public abstract class BotAdapter { */ public static final String OAUTH_SCOPE_KEY = "Microsoft.Bot.Builder.BotAdapter.OAuthScope"; + /** + * Key to store bot oauth client. + */ + public static final String OAUTH_CLIENT_KEY = "OAuthClient"; + /** * The collection of middleware in the adapter's pipeline. */ diff --git a/libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotFrameworkAdapter.java b/libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotFrameworkAdapter.java index da560d2e..d628aec3 100644 --- a/libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotFrameworkAdapter.java +++ b/libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotFrameworkAdapter.java @@ -327,10 +327,7 @@ public class BotFrameworkAdapter extends BotAdapter BotCallbackHandler callback ) { return continueConversation( - claimsIdentity, - reference, - getBotFrameworkOAuthScope(), - callback + claimsIdentity, reference, getBotFrameworkOAuthScope(), callback ); } @@ -380,9 +377,7 @@ public class BotFrameworkAdapter extends BotAdapter context.getTurnState().add(OAUTH_SCOPE_KEY, audience); pipelineResult = createConnectorClient( - reference.getServiceUrl(), - claimsIdentity, - audience + reference.getServiceUrl(), claimsIdentity, audience ).thenCompose(connectorClient -> { context.getTurnState().add(CONNECTOR_CLIENT_KEY, connectorClient); return runPipeline(context, callback); @@ -430,11 +425,7 @@ public class BotFrameworkAdapter extends BotAdapter BotAssert.activityNotNull(activity); return JwtTokenValidation.authenticateRequest( - activity, - authHeader, - credentialProvider, - channelProvider, - authConfiguration + activity, authHeader, credentialProvider, channelProvider, authConfiguration ).thenCompose(claimsIdentity -> processActivity(claimsIdentity, activity, callback)); } @@ -897,7 +888,7 @@ public class BotFrameworkAdapter extends BotAdapter || StringUtils.isEmpty(context.getActivity().getFrom().getId()) ) { throw new IllegalArgumentException( - "BotFrameworkAdapter.GetuserToken(): missing from or from.id" + "BotFrameworkAdapter.getUserToken(): missing from or from.id" ); } @@ -908,10 +899,8 @@ public class BotFrameworkAdapter extends BotAdapter return createOAuthClient(context, null).thenCompose(oAuthClient -> { return oAuthClient.getUserToken() .getToken( - context.getActivity().getFrom().getId(), - connectionName, - context.getActivity().getChannelId(), - magicCode + context.getActivity().getFrom().getId(), connectionName, + context.getActivity().getChannelId(), magicCode ); }); } @@ -1053,8 +1042,7 @@ public class BotFrameworkAdapter extends BotAdapter return createOAuthClient(context, null).thenCompose(oAuthClient -> { return oAuthClient.getUserToken() .signOut( - context.getActivity().getFrom().getId(), - connectionName, + context.getActivity().getFrom().getId(), connectionName, context.getActivity().getChannelId() ); }).thenApply(signOutResult -> null); @@ -1269,11 +1257,7 @@ public class BotFrameworkAdapter extends BotAdapter } return createConversation( - channelId, - serviceUrl, - credentials, - conversationParameters, - callback + channelId, serviceUrl, credentials, conversationParameters, callback ); } @@ -1300,7 +1284,6 @@ public class BotFrameworkAdapter extends BotAdapter .equalsIgnoreCase(turnContext.getActivity().getChannelId(), Channels.EMULATOR) && credentialProvider.isAuthenticationDisabled().join() ) { - OAuthClientConfig.emulateOAuthCards = true; } @@ -1310,17 +1293,26 @@ public class BotFrameworkAdapter extends BotAdapter ? oAuthAppCredentials : getAppCredentials(appId, oAuthScope).join(); + OAuthClient oAuthClient = new RestOAuthClient( + OAuthClientConfig.emulateOAuthCards + ? turnContext.getActivity().getServiceUrl() + : OAuthClientConfig.OAUTHENDPOINT, + credentials + ); + + // adding the oAuthClient into the TurnState + // TokenResolver.cs will use it get the correct credentials to poll for + // token for streaming scenario + turnContext.getTurnState().add(BotAdapter.OAUTH_CLIENT_KEY, oAuthClient); + if (OAuthClientConfig.emulateOAuthCards) { // do not join task - we want this to run in the background. - OAuthClient oAuthClient = - new RestOAuthClient(turnContext.getActivity().getServiceUrl(), credentials); return OAuthClientConfig .sendEmulateOAuthCards(oAuthClient, OAuthClientConfig.emulateOAuthCards) .thenApply(result -> oAuthClient); } - return CompletableFuture - .completedFuture(new RestOAuthClient(OAuthClientConfig.OAUTHENDPOINT, credentials)); + return CompletableFuture.completedFuture(oAuthClient); } /** @@ -1394,8 +1386,7 @@ public class BotFrameworkAdapter extends BotAdapter CompletableFuture result = new CompletableFuture<>(); String clientKey = keyForConnectorClient( - serviceUrl, - usingAppCredentials != null ? usingAppCredentials.getAppId() : null, + serviceUrl, usingAppCredentials != null ? usingAppCredentials.getAppId() : null, usingAppCredentials != null ? usingAppCredentials.oAuthScope() : null ); @@ -1553,7 +1544,8 @@ public class BotFrameworkAdapter extends BotAdapter } /** - * Get the AppCredentials cache. For unit testing. + * Get the AppCredentials cache. For unit testing. + * * @return The AppCredentials cache. */ protected Map getCredentialsCache() { @@ -1561,7 +1553,8 @@ public class BotFrameworkAdapter extends BotAdapter } /** - * Get the ConnectorClient cache. For unit testing. + * Get the ConnectorClient cache. For unit testing. + * * @return The ConnectorClient cache. */ protected Map getConnectorClientCache() {