From 64b459d175b1a17bacd5965bcdf95394606e8c2a Mon Sep 17 00:00:00 2001 From: John Taylor Date: Wed, 7 Oct 2020 14:05:02 -0700 Subject: [PATCH 1/3] 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 BotFrameworkAuthentication Create( 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 BotFrameworkAuthentication Create( 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 @@ public ParameterizedBotFrameworkAuthentication( 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() From 0f3c2cbfc7d0a0891854f93c1945e00520fa0c79 Mon Sep 17 00:00:00 2001 From: John Taylor Date: Wed, 7 Oct 2020 16:11:12 -0700 Subject: [PATCH 2/3] code review feedback --- .../Authentication/BotFrameworkAuthenticationFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Microsoft.Bot.Connector/Authentication/BotFrameworkAuthenticationFactory.cs b/libraries/Microsoft.Bot.Connector/Authentication/BotFrameworkAuthenticationFactory.cs index 8465085016..df951ab126 100644 --- a/libraries/Microsoft.Bot.Connector/Authentication/BotFrameworkAuthenticationFactory.cs +++ b/libraries/Microsoft.Bot.Connector/Authentication/BotFrameworkAuthenticationFactory.cs @@ -83,7 +83,7 @@ public static BotFrameworkAuthentication Create( } else { - throw new Exception("A ChannelService was given but the value was not recognized."); + throw new ArgumentException("A ChannelService was given but the value was not recognized."); } } } From 989005588a11a8243fbd2b4dad38df10e1e520e1 Mon Sep 17 00:00:00 2001 From: John Taylor Date: Wed, 7 Oct 2020 16:15:06 -0700 Subject: [PATCH 3/3] code review feedback (comment) --- .../Authentication/BotFrameworkAuthenticationFactory.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/Microsoft.Bot.Connector/Authentication/BotFrameworkAuthenticationFactory.cs b/libraries/Microsoft.Bot.Connector/Authentication/BotFrameworkAuthenticationFactory.cs index df951ab126..e8b8307daf 100644 --- a/libraries/Microsoft.Bot.Connector/Authentication/BotFrameworkAuthenticationFactory.cs +++ b/libraries/Microsoft.Bot.Connector/Authentication/BotFrameworkAuthenticationFactory.cs @@ -83,7 +83,9 @@ public static BotFrameworkAuthentication Create( } else { - throw new ArgumentException("A ChannelService was given but the value was not recognized."); + // The ChannelService value is used an indicator of which built in set of constants to use. If it is not recognized, a full configuration is expected. + + throw new ArgumentException("The provided ChannelService value is not supported."); } } }