From 64b459d175b1a17bacd5965bcdf95394606e8c2a Mon Sep 17 00:00:00 2001 From: John Taylor Date: Wed, 7 Oct 2020 14:05:02 -0700 Subject: [PATCH] fix the default behavior when cloud environments are configured --- .../BotFrameworkAuthenticationFactory.cs | 38 ++++++++++++++----- ...ParameterizedBotFrameworkAuthentication.cs | 3 -- .../CloudAdapterTests.cs | 2 +- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/libraries/Microsoft.Bot.Connector/Authentication/BotFrameworkAuthenticationFactory.cs b/libraries/Microsoft.Bot.Connector/Authentication/BotFrameworkAuthenticationFactory.cs index d864c37521..8465085016 100644 --- a/libraries/Microsoft.Bot.Connector/Authentication/BotFrameworkAuthenticationFactory.cs +++ b/libraries/Microsoft.Bot.Connector/Authentication/BotFrameworkAuthenticationFactory.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System; using System.Net.Http; using Microsoft.Extensions.Logging; @@ -43,18 +44,18 @@ public static class BotFrameworkAuthenticationFactory HttpClient httpClient, ILogger logger) { - if (string.IsNullOrEmpty(channelService)) - { - return new PublicCloudBotFrameworkAuthentication(credentialFactory, authConfiguration, httpClient, logger); - } - else if (channelService == GovernmentAuthenticationConstants.ChannelService) - { - return new GovernmentCloudBotFrameworkAuthentication(credentialFactory, authConfiguration, httpClient, logger); - } - else + if ( + !string.IsNullOrEmpty(toChannelFromBotLoginUrl) || + !string.IsNullOrEmpty(toChannelFromBotOAuthScope) || + !string.IsNullOrEmpty(toBotFromChannelTokenIssuer) || + !string.IsNullOrEmpty(oAuthUrl) || + !string.IsNullOrEmpty(toBotFromChannelOpenIdMetadataUrl) || + !string.IsNullOrEmpty(toBotFromEmulatorOpenIdMetadataUrl) || + !string.IsNullOrEmpty(callerId)) { + // if we have any of the 'parameterized' properties defined we'll assume this is the parameterized code + return new ParameterizedBotFrameworkAuthentication( - channelService, validateAuthority, toChannelFromBotLoginUrl, toChannelFromBotOAuthScope, @@ -68,6 +69,23 @@ public static class BotFrameworkAuthenticationFactory httpClient, logger); } + else + { + // else apply the built in default behavior, which is either the public cloud or the gov cloud depending on whether we have a channelService value present + + if (string.IsNullOrEmpty(channelService)) + { + return new PublicCloudBotFrameworkAuthentication(credentialFactory, authConfiguration, httpClient, logger); + } + else if (channelService == GovernmentAuthenticationConstants.ChannelService) + { + return new GovernmentCloudBotFrameworkAuthentication(credentialFactory, authConfiguration, httpClient, logger); + } + else + { + throw new Exception("A ChannelService was given but the value was not recognized."); + } + } } } } diff --git a/libraries/Microsoft.Bot.Connector/Authentication/ParameterizedBotFrameworkAuthentication.cs b/libraries/Microsoft.Bot.Connector/Authentication/ParameterizedBotFrameworkAuthentication.cs index 05083f62c1..49406d8534 100644 --- a/libraries/Microsoft.Bot.Connector/Authentication/ParameterizedBotFrameworkAuthentication.cs +++ b/libraries/Microsoft.Bot.Connector/Authentication/ParameterizedBotFrameworkAuthentication.cs @@ -19,7 +19,6 @@ internal class ParameterizedBotFrameworkAuthentication : BotFrameworkAuthenticat { private static HttpClient _defaultHttpClient = new HttpClient(); - private readonly string _channelService; private readonly bool _validateAuthority; private readonly string _toChannelFromBotLoginUrl; private readonly string _toChannelFromBotOAuthScope; @@ -34,7 +33,6 @@ internal class ParameterizedBotFrameworkAuthentication : BotFrameworkAuthenticat private readonly ILogger _logger; public ParameterizedBotFrameworkAuthentication( - string channelService, bool validateAuthority, string toChannelFromBotLoginUrl, string toChannelFromBotOAuthScope, @@ -48,7 +46,6 @@ internal class ParameterizedBotFrameworkAuthentication : BotFrameworkAuthenticat HttpClient httpClient = null, ILogger logger = null) { - _channelService = channelService; _validateAuthority = validateAuthority; _toChannelFromBotLoginUrl = toChannelFromBotLoginUrl; _toChannelFromBotOAuthScope = toChannelFromBotOAuthScope; diff --git a/tests/integration/Microsoft.Bot.Builder.Integration.AspNet.Core.Tests/CloudAdapterTests.cs b/tests/integration/Microsoft.Bot.Builder.Integration.AspNet.Core.Tests/CloudAdapterTests.cs index d9370e9a24..31ef5b8c32 100644 --- a/tests/integration/Microsoft.Bot.Builder.Integration.AspNet.Core.Tests/CloudAdapterTests.cs +++ b/tests/integration/Microsoft.Bot.Builder.Integration.AspNet.Core.Tests/CloudAdapterTests.cs @@ -127,7 +127,7 @@ public void ConstructorWithConfiguration() { { "MicrosoftAppId", "appId" }, { "MicrosoftAppPassword", "appPassword" }, - { "ChannelService", "channelService" } + { "ChannelService", GovernmentAuthenticationConstants.ChannelService } }; var configuration = new ConfigurationBuilder()