From 980b5a908a7096763041870f0892c3c77a2c5c00 Mon Sep 17 00:00:00 2001 From: tracyboehrer Date: Fri, 17 Apr 2020 10:43:45 -0500 Subject: [PATCH] CallerId is getting set on incoming Activity --- .../bot/builder/BotFrameworkAdapter.java | 60 +++++++++++++------ .../bot/schema/CallerIdConstants.java | 30 ++++++++++ 2 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 libraries/bot-schema/src/main/java/com/microsoft/bot/schema/CallerIdConstants.java 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 fa0b90968..5e3d27812 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 @@ -30,6 +30,7 @@ import com.microsoft.bot.schema.AadResourceUrls; import com.microsoft.bot.schema.Activity; import com.microsoft.bot.schema.ActivityTypes; +import com.microsoft.bot.schema.CallerIdConstants; import com.microsoft.bot.schema.ChannelAccount; import com.microsoft.bot.schema.ConversationAccount; import com.microsoft.bot.schema.ConversationParameters; @@ -297,10 +298,8 @@ public CompletableFuture continueConversation( CompletableFuture pipelineResult = new CompletableFuture<>(); - try (TurnContextImpl context = new TurnContextImpl( - this, - reference.getContinuationActivity() - )) { + try (TurnContextImpl context = + new TurnContextImpl(this, reference.getContinuationActivity())) { // Hand craft Claims Identity. HashMap claims = new HashMap() { { @@ -388,6 +387,7 @@ public CompletableFuture processActivity( CompletableFuture pipelineResult = new CompletableFuture<>(); try (TurnContextImpl context = new TurnContextImpl(this, activity)) { + activity.setCallerId(generateCallerId(identity).join()); context.getTurnState().add(BOT_IDENTITY_KEY, identity); pipelineResult = createConnectorClient(activity.getServiceUrl(), identity) @@ -425,6 +425,32 @@ public CompletableFuture processActivity( return pipelineResult; } + @SuppressWarnings({"PMD"}) + private CompletableFuture generateCallerId(ClaimsIdentity claimsIdentity) { + return credentialProvider.isAuthenticationDisabled() + .thenApply( + is_auth_enabled -> { + // Is the bot accepting all incoming messages? + if (!is_auth_enabled) { + return null; + } + + // Is the activity from Public Azure? + if (channelProvider == null || channelProvider.isPublicAzure()) { + return CallerIdConstants.PUBLIC_AZURE_CHANNEL; + } + + // Is the activity from Azure Gov? + if (channelProvider != null && channelProvider.isGovernment()) { + return CallerIdConstants.US_GOV_CHANNEL; + } + + // Return null so that the callerId is cleared. + return null; + } + ); + } + /** * Sends activities to the conversation. * @@ -492,15 +518,14 @@ public CompletableFuture sendActivities( // if it is a Trace activity we only send to the channel if it's the emulator. response = null; } else if (!StringUtils.isEmpty(activity.getReplyToId())) { - ConnectorClient connectorClient = context.getTurnState() - .get(CONNECTOR_CLIENT_KEY); + ConnectorClient connectorClient = + context.getTurnState().get(CONNECTOR_CLIENT_KEY); response = connectorClient.getConversations().replyToActivity(activity).join(); } else { - ConnectorClient connectorClient = context.getTurnState() - .get(CONNECTOR_CLIENT_KEY); - response = connectorClient.getConversations() - .sendToConversation(activity) - .join(); + ConnectorClient connectorClient = + context.getTurnState().get(CONNECTOR_CLIENT_KEY); + response = + connectorClient.getConversations().sendToConversation(activity).join(); } // If No response is set, then default to a "simple" response. This can't really @@ -518,9 +543,8 @@ public CompletableFuture sendActivities( // https://github.com/Microsoft/botbuilder-dotnet/issues/460 // bug report : https://github.com/Microsoft/botbuilder-dotnet/issues/465 if (response == null) { - response = new ResourceResponse( - (activity.getId() == null) ? "" : activity.getId() - ); + response = + new ResourceResponse((activity.getId() == null) ? "" : activity.getId()); } responses[index] = response; @@ -1314,8 +1338,8 @@ protected CompletableFuture getOrCreateConnectorClient( usingAppCredentials ); } else { - AppCredentials emptyCredentials = channelProvider != null - && channelProvider.isGovernment() + AppCredentials emptyCredentials = + channelProvider != null && channelProvider.isGovernment() ? MicrosoftGovernmentAppCredentials.empty() : MicrosoftAppCredentials.empty(); connectorClient = new RestConnectorClient( @@ -1395,8 +1419,8 @@ public CompletableFuture onTurn(TurnContext turnContext, NextDelegate next && StringUtils.isEmpty(turnContext.getActivity().getConversation().getTenantId()) ) { - JsonNode teamsChannelData = new ObjectMapper() - .valueToTree(turnContext.getActivity().getChannelData()); + JsonNode teamsChannelData = + new ObjectMapper().valueToTree(turnContext.getActivity().getChannelData()); if ( teamsChannelData != null && teamsChannelData.has("tenant") && teamsChannelData.get("tenant").has("id") diff --git a/libraries/bot-schema/src/main/java/com/microsoft/bot/schema/CallerIdConstants.java b/libraries/bot-schema/src/main/java/com/microsoft/bot/schema/CallerIdConstants.java new file mode 100644 index 000000000..d8ab53f7f --- /dev/null +++ b/libraries/bot-schema/src/main/java/com/microsoft/bot/schema/CallerIdConstants.java @@ -0,0 +1,30 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.bot.schema; + +/** + * Constants for CallerId values. + */ +public final class CallerIdConstants { + private CallerIdConstants() { + + } + + /** + * The caller ID for any Bot Framework channel. + */ + public static final String PUBLIC_AZURE_CHANNEL = "urn:botframework:azure"; + + /** + * The caller ID for any Bot Framework US Government cloud channel. + */ + public static final String US_GOV_CHANNEL = "urn:botframework:azureusgov"; + + /** + * The caller ID prefix when a bot initiates a request to another bot. This + * prefix will be followed by the Azure Active Directory App ID of the bot that + * initiated the call. + */ + public static final String BOT_TO_BOT_PREFIX = "urn:botframework:aadappid:"; +}