From 23a80420e47608fe22813aa21d0508e6dae64478 Mon Sep 17 00:00:00 2001 From: tracyboehrer Date: Tue, 2 Feb 2021 09:29:10 -0600 Subject: [PATCH] Can provide custom oAuthScope to MicrosoftGovernmentAppCredentials --- .../bot/builder/BotFrameworkAdapter.java | 2 +- .../authentication/AppCredentials.java | 16 +++++++- .../MicrosoftAppCredentials.java | 19 ++++++++++ .../MicrosoftGovernmentAppCredentials.java | 37 +++++++++++++------ 4 files changed, 61 insertions(+), 13 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 dc07f0ad9..9cae8c24a 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 @@ -1482,7 +1482,7 @@ private CompletableFuture getAppCredentials(String appId, String return credentialProvider.getAppPassword(appId).thenApply(appPassword -> { AppCredentials credentials = channelProvider != null && channelProvider.isGovernment() - ? new MicrosoftGovernmentAppCredentials(appId, appPassword) + ? new MicrosoftGovernmentAppCredentials(appId, appPassword, scope) : new MicrosoftAppCredentials(appId, appPassword); appCredentialMap.put(cacheKey, credentials); return credentials; diff --git a/libraries/bot-connector/src/main/java/com/microsoft/bot/connector/authentication/AppCredentials.java b/libraries/bot-connector/src/main/java/com/microsoft/bot/connector/authentication/AppCredentials.java index cf638239c..000f3db41 100644 --- a/libraries/bot-connector/src/main/java/com/microsoft/bot/connector/authentication/AppCredentials.java +++ b/libraries/bot-connector/src/main/java/com/microsoft/bot/connector/authentication/AppCredentials.java @@ -37,6 +37,7 @@ public abstract class AppCredentials implements ServiceClientCredentials { private String appId; private String authTenant; + private String authScope; private Authenticator authenticator; /** @@ -45,7 +46,20 @@ public abstract class AppCredentials implements ServiceClientCredentials { * @param withChannelAuthTenant Optional. The oauth token tenant. */ public AppCredentials(String withChannelAuthTenant) { + this(withChannelAuthTenant, AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE); + } + + /** + * Initializes a new instance of the AppCredentials class. + * + * @param withChannelAuthTenant Optional. The oauth token tenant. + * @param withOAuthScope The scope for the token. + */ + public AppCredentials(String withChannelAuthTenant, String withOAuthScope) { setChannelAuthTenant(withChannelAuthTenant); + authScope = StringUtils.isEmpty(withOAuthScope) + ? AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE + : withOAuthScope; } /** @@ -179,7 +193,7 @@ AuthenticationConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL_TEMPLATE, getChannelAuthTe * @return OAuth scope. */ public String oAuthScope() { - return AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE; + return authScope; } /** diff --git a/libraries/bot-connector/src/main/java/com/microsoft/bot/connector/authentication/MicrosoftAppCredentials.java b/libraries/bot-connector/src/main/java/com/microsoft/bot/connector/authentication/MicrosoftAppCredentials.java index c7f39b50f..801b9c04e 100644 --- a/libraries/bot-connector/src/main/java/com/microsoft/bot/connector/authentication/MicrosoftAppCredentials.java +++ b/libraries/bot-connector/src/main/java/com/microsoft/bot/connector/authentication/MicrosoftAppCredentials.java @@ -57,6 +57,25 @@ public MicrosoftAppCredentials( setAppPassword(withAppPassword); } + /** + * Initializes a new instance of the MicrosoftAppCredentials class. + * + * @param withAppId The Microsoft app ID. + * @param withAppPassword The Microsoft app password. + * @param withChannelAuthTenant Optional. The oauth token tenant. + * @param withOAuthScope The scope for the token. + */ + public MicrosoftAppCredentials( + String withAppId, + String withAppPassword, + String withChannelAuthTenant, + String withOAuthScope + ) { + super(withChannelAuthTenant, withOAuthScope); + setAppId(withAppId); + setAppPassword(withAppPassword); + } + /** * Gets the app password for this credential. * diff --git a/libraries/bot-connector/src/main/java/com/microsoft/bot/connector/authentication/MicrosoftGovernmentAppCredentials.java b/libraries/bot-connector/src/main/java/com/microsoft/bot/connector/authentication/MicrosoftGovernmentAppCredentials.java index 7fcbd695a..619689313 100644 --- a/libraries/bot-connector/src/main/java/com/microsoft/bot/connector/authentication/MicrosoftGovernmentAppCredentials.java +++ b/libraries/bot-connector/src/main/java/com/microsoft/bot/connector/authentication/MicrosoftGovernmentAppCredentials.java @@ -3,6 +3,8 @@ package com.microsoft.bot.connector.authentication; +import org.apache.commons.lang3.StringUtils; + /** * MicrosoftGovernmentAppCredentials auth implementation. */ @@ -14,7 +16,30 @@ public class MicrosoftGovernmentAppCredentials extends MicrosoftAppCredentials { * @param password The Microsoft app password. */ public MicrosoftGovernmentAppCredentials(String appId, String password) { - super(appId, password); + super( + appId, + password, + null, + GovernmentAuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE + ); + } + + /** + * Initializes a new instance of the MicrosoftGovernmentAppCredentials class. + * + * @param appId The Microsoft app ID. + * @param password The Microsoft app password. + * @param oAuthScope The scope for the token. + */ + public MicrosoftGovernmentAppCredentials(String appId, String password, String oAuthScope) { + super( + appId, + password, + null, + StringUtils.isEmpty(oAuthScope) + ? GovernmentAuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE + : oAuthScope + ); } /** @@ -35,14 +60,4 @@ public static MicrosoftGovernmentAppCredentials empty() { public String oAuthEndpoint() { return GovernmentAuthenticationConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL; } - - /** - * Gets the Gov OAuth scope to use. - * - * @return The OAuth scope to use. - */ - @Override - public String oAuthScope() { - return GovernmentAuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE; - } }