From fde93aa3ab1e17f30eab6fd1e5832321d06c81ce Mon Sep 17 00:00:00 2001 From: tracyboehrer Date: Fri, 15 Jan 2021 08:44:07 -0600 Subject: [PATCH] Fixes Unauthorized error when calling ContinueConversation --- .../bot/builder/BotFrameworkAdapter.java | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) 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 9274bb97a..012d67fc7 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 @@ -286,10 +286,6 @@ public CompletableFuture continueConversation( ConversationReference reference, BotCallbackHandler callback ) { - if (StringUtils.isEmpty(botAppId)) { - throw new IllegalArgumentException("botAppId"); - } - if (reference == null) { throw new IllegalArgumentException("reference"); } @@ -298,14 +294,14 @@ public CompletableFuture continueConversation( throw new IllegalArgumentException("callback"); } + botAppId = botAppId == null ? "" : botAppId; + // Hand craft Claims Identity. - HashMap claims = new HashMap() { - { - // Adding claims for both Emulator and Channel. - put(AuthenticationConstants.AUDIENCE_CLAIM, botAppId); - put(AuthenticationConstants.APPID_CLAIM, botAppId); - } - }; + // Adding claims for both Emulator and Channel. + HashMap claims = new HashMap(); + claims.put(AuthenticationConstants.AUDIENCE_CLAIM, botAppId); + claims.put(AuthenticationConstants.APPID_CLAIM, botAppId); + ClaimsIdentity claimsIdentity = new ClaimsIdentity("ExternalBearer", claims); String audience = getBotFrameworkOAuthScope(); @@ -382,12 +378,22 @@ public CompletableFuture continueConversation( context.getTurnState().add(BOT_IDENTITY_KEY, claimsIdentity); context.getTurnState().add(OAUTH_SCOPE_KEY, audience); - pipelineResult = createConnectorClient( - reference.getServiceUrl(), claimsIdentity, audience - ).thenCompose(connectorClient -> { - context.getTurnState().add(CONNECTOR_CLIENT_KEY, connectorClient); - return runPipeline(context, callback); - }); + String appIdFromClaims = JwtTokenValidation.getAppIdFromClaims(claimsIdentity.claims()); + return credentialProvider.isValidAppId(appIdFromClaims) + .thenCompose(isValidAppId -> { + // If we receive a valid app id in the incoming token claims, add the + // channel service URL to the trusted services list so we can send messages back. + if (!StringUtils.isEmpty(appIdFromClaims) && isValidAppId) { + AppCredentials.trustServiceUrl(reference.getServiceUrl()); + } + + return createConnectorClient( + reference.getServiceUrl(), claimsIdentity, audience + ).thenCompose(connectorClient -> { + context.getTurnState().add(CONNECTOR_CLIENT_KEY, connectorClient); + return runPipeline(context, callback); + }); + }); } catch (Exception e) { pipelineResult.completeExceptionally(e); }