Skip to content

Commit

Permalink
Add UT
Browse files Browse the repository at this point in the history
Fix some coding
  • Loading branch information
fangyangci committed Dec 15, 2023
1 parent 159126d commit 5e248ed
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 156 deletions.
46 changes: 33 additions & 13 deletions libraries/Microsoft.Bot.Connector/Authentication/AppCredentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public abstract class AppCredentials : ServiceClientCredentials
/// </summary>
private Lazy<IAuthenticator> _authenticator;

private string _oAuthScope;

private string _channelAuthTenant;

/// <summary>
/// Initializes a new instance of the <see cref="AppCredentials"/> class.
/// </summary>
Expand All @@ -54,7 +58,7 @@ public AppCredentials(string channelAuthTenant = null, HttpClient customHttpClie
/// <param name="oAuthScope">The scope for the token.</param>
public AppCredentials(string channelAuthTenant = null, HttpClient customHttpClient = null, ILogger logger = null, string oAuthScope = null)
{
OAuthScope = string.IsNullOrWhiteSpace(oAuthScope) ? AuthenticationConstants.ToChannelFromBotOAuthScope : oAuthScope;
_oAuthScope = oAuthScope;
ChannelAuthTenant = channelAuthTenant;
CustomHttpClient = customHttpClient;
Logger = logger ?? NullLogger.Instance;
Expand All @@ -76,15 +80,17 @@ public AppCredentials(string channelAuthTenant = null, HttpClient customHttpClie
/// </value>
public virtual string ChannelAuthTenant
{
get => string.IsNullOrEmpty(AuthTenant) ? AuthenticationConstants.DefaultChannelAuthTenant : AuthTenant;
get => string.IsNullOrEmpty(_channelAuthTenant)
? DefaultChannelAuthTenant
: _channelAuthTenant;
set
{
// Advanced user only, see https://aka.ms/bots/tenant-restriction
var endpointUrl = string.Format(CultureInfo.InvariantCulture, AuthenticationConstants.ToChannelFromBotLoginUrlTemplate, value);
var endpointUrl = string.Format(CultureInfo.InvariantCulture, ToChannelFromBotLoginUrlTemplate, value);

if (Uri.TryCreate(endpointUrl, UriKind.Absolute, out _))
{
AuthTenant = value;
_channelAuthTenant = value;
}
else
{
Expand All @@ -99,7 +105,7 @@ public virtual string ChannelAuthTenant
/// <value>
/// The OAuth endpoint to use.
/// </value>
public virtual string OAuthEndpoint => string.Format(CultureInfo.InvariantCulture, AuthenticationConstants.ToChannelFromBotLoginUrlTemplate, ChannelAuthTenant);
public virtual string OAuthEndpoint => string.Format(CultureInfo.InvariantCulture, ToChannelFromBotLoginUrlTemplate, ChannelAuthTenant);

/// <summary>
/// Gets a value indicating whether to validate the Authority.
Expand All @@ -115,31 +121,45 @@ public virtual string ChannelAuthTenant
/// <value>
/// The OAuth scope to use.
/// </value>
public virtual string OAuthScope { get; }
public virtual string OAuthScope => string.IsNullOrEmpty(_oAuthScope)
? ToChannelFromBotOAuthScope
: _oAuthScope;

/// <summary>
/// Gets or sets the channel auth token tenant for this credential.
/// </summary>
/// <value>
/// The channel auth token tenant for this credential.
/// </value>
protected string AuthTenant { get; set; }
protected HttpClient CustomHttpClient { get; set; }

/// <summary>
/// Gets or sets the channel auth token tenant for this credential.
/// </summary>
/// <value>
/// The channel auth token tenant for this credential.
/// </value>
protected HttpClient CustomHttpClient { get; set; }
protected ILogger Logger { get; set; }

/// <summary>
/// Gets or sets the channel auth token tenant for this credential.
/// Gets DefaultChannelAuthTenant.
/// </summary>
/// <value>
/// The channel auth token tenant for this credential.
/// </value>
protected ILogger Logger { get; set; }
/// <value>DefaultChannelAuthTenant.</value>
protected virtual string DefaultChannelAuthTenant => AuthenticationConstants.DefaultChannelAuthTenant;

/// <summary>
/// Gets ToChannelFromBotOAuthScope.
/// </summary>
/// <value>ToChannelFromBotOAuthScope.</value>
protected virtual string ToChannelFromBotOAuthScope => AuthenticationConstants.ToChannelFromBotOAuthScope;

/// <summary>
/// Gets ToChannelFromBotLoginUrlTemplate.
/// </summary>
/// <value>ToChannelFromBotLoginUrlTemplate.</value>
#pragma warning disable CA1056 // Uri properties should not be strings
protected virtual string ToChannelFromBotLoginUrlTemplate => AuthenticationConstants.ToChannelFromBotLoginUrlTemplate;
#pragma warning restore CA1056 // Uri properties should not be strings

/// <summary>
/// Adds the host of service url to <see cref="MicrosoftAppCredentials"/> trusted hosts.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class GovernmentCloudBotFrameworkAuthentication : BuiltinBotFrameworkAu
public GovernmentCloudBotFrameworkAuthentication(ServiceClientCredentialsFactory credentialFactory, AuthenticationConfiguration authConfiguration, IHttpClientFactory httpClientFactory, ILogger logger = null)
: base(
GovernmentAuthenticationConstants.ToChannelFromBotOAuthScope,
GovernmentAuthenticationConstants.ToChannelFromBotLoginUrl,
GovernmentAuthenticationConstants.ToChannelFromBotLoginUrlTemplate,
CallerIdConstants.USGovChannel,
GovernmentAuthenticationConstants.ChannelService,
GovernmentAuthenticationConstants.OAuthUrlGov,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,6 @@ public class MicrosoftAppCredentials : AppCredentials
/// </summary>
public static readonly MicrosoftAppCredentials Empty = new MicrosoftAppCredentials(null, null);

/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftAppCredentials"/> class.
/// </summary>
/// <param name="appId">The Microsoft app ID.</param>
/// <param name="password">The Microsoft app password.</param>
public MicrosoftAppCredentials(string appId, string password)
: this(appId, password, null, null, null, null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftAppCredentials"/> class.
/// </summary>
/// <param name="appId">The Microsoft app ID.</param>
/// <param name="password">The Microsoft app password.</param>
/// <param name="customHttpClient">Optional <see cref="HttpClient"/> to be used when acquiring tokens.</param>
public MicrosoftAppCredentials(string appId, string password, HttpClient customHttpClient)
: this(appId, password, null, customHttpClient)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftAppCredentials"/> class.
/// </summary>
/// <param name="appId">The Microsoft app ID.</param>
/// <param name="password">The Microsoft app password.</param>
/// <param name="customHttpClient">Optional <see cref="HttpClient"/> to be used when acquiring tokens.</param>
/// <param name="logger">Optional <see cref="ILogger"/> to gather telemetry data while acquiring and managing credentials.</param>
public MicrosoftAppCredentials(string appId, string password, HttpClient customHttpClient, ILogger logger)
: this(appId, password, null, customHttpClient, logger)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftAppCredentials"/> class.
/// </summary>
Expand All @@ -80,36 +47,11 @@ public MicrosoftAppCredentials(string appId, string password, HttpClient customH
/// <param name="customHttpClient">Optional <see cref="HttpClient"/> to be used when acquiring tokens.</param>
/// <param name="logger">Optional <see cref="ILogger"/> to gather telemetry data while acquiring and managing credentials.</param>
/// <param name="oAuthScope">The scope for the token.</param>
public MicrosoftAppCredentials(string appId, string password, HttpClient customHttpClient, ILogger logger, string oAuthScope)
public MicrosoftAppCredentials(string appId, string password, HttpClient customHttpClient = null, ILogger logger = null, string oAuthScope = null)
: this(appId, password, null, customHttpClient, logger, oAuthScope)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftAppCredentials"/> class.
/// </summary>
/// <param name="appId">The Microsoft app ID.</param>
/// <param name="password">The Microsoft app password.</param>
/// <param name="channelAuthTenant">Optional. The oauth token tenant.</param>
/// <param name="customHttpClient">Optional <see cref="HttpClient"/> to be used when acquiring tokens.</param>
public MicrosoftAppCredentials(string appId, string password, string channelAuthTenant, HttpClient customHttpClient)
: this(appId, password, channelAuthTenant, customHttpClient, null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftAppCredentials"/> class.
/// </summary>
/// <param name="appId">The Microsoft app ID.</param>
/// <param name="password">The Microsoft app password.</param>
/// <param name="channelAuthTenant">Optional. The oauth token tenant.</param>
/// <param name="customHttpClient">Optional <see cref="HttpClient"/> to be used when acquiring tokens.</param>
/// <param name="logger">Optional <see cref="ILogger"/> to gather telemetry data while acquiring and managing credentials.</param>
public MicrosoftAppCredentials(string appId, string password, string channelAuthTenant, HttpClient customHttpClient, ILogger logger = null)
: this(appId, password, channelAuthTenant, customHttpClient, logger, null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftAppCredentials"/> class.
/// </summary>
Expand All @@ -119,7 +61,7 @@ public MicrosoftAppCredentials(string appId, string password, string channelAuth
/// <param name="customHttpClient">Optional <see cref="HttpClient"/> to be used when acquiring tokens.</param>
/// <param name="logger">Optional <see cref="ILogger"/> to gather telemetry data while acquiring and managing credentials.</param>
/// <param name="oAuthScope">The scope for the token.</param>
public MicrosoftAppCredentials(string appId, string password, string channelAuthTenant, HttpClient customHttpClient, ILogger logger = null, string oAuthScope = null)
public MicrosoftAppCredentials(string appId, string password, string channelAuthTenant, HttpClient customHttpClient = null, ILogger logger = null, string oAuthScope = null)
: base(channelAuthTenant, customHttpClient, logger, oAuthScope)
{
MicrosoftAppId = appId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,6 @@ public class MicrosoftGovernmentAppCredentials : MicrosoftAppCredentials
/// </summary>
public static new readonly MicrosoftGovernmentAppCredentials Empty = new MicrosoftGovernmentAppCredentials(null, null, null, null, GovernmentAuthenticationConstants.ToChannelFromBotOAuthScope);

/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftGovernmentAppCredentials"/> class.
/// </summary>
/// <param name="appId">The Microsoft app ID.</param>
/// <param name="password">The Microsoft app password.</param>
/// <param name="customHttpClient">Optional <see cref="HttpClient"/> to be used when acquiring tokens.</param>
public MicrosoftGovernmentAppCredentials(string appId, string password, HttpClient customHttpClient = null)
: this(appId, password, customHttpClient, null, GovernmentAuthenticationConstants.ToChannelFromBotOAuthScope)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftGovernmentAppCredentials"/> class.
/// </summary>
/// <param name="appId">The Microsoft app ID.</param>
/// <param name="password">The Microsoft app password.</param>
/// <param name="customHttpClient">Optional <see cref="HttpClient"/> to be used when acquiring tokens.</param>
/// <param name="logger">Optional <see cref="ILogger"/> to gather telemetry data while acquiring and managing credentials.</param>
public MicrosoftGovernmentAppCredentials(string appId, string password, HttpClient customHttpClient, ILogger logger)
: this(appId, password, customHttpClient, logger, GovernmentAuthenticationConstants.ToChannelFromBotOAuthScope)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftGovernmentAppCredentials"/> class.
/// </summary>
Expand All @@ -48,8 +25,8 @@ public MicrosoftGovernmentAppCredentials(string appId, string password, HttpClie
/// <param name="customHttpClient">Optional <see cref="HttpClient"/> to be used when acquiring tokens.</param>
/// <param name="logger">Optional <see cref="ILogger"/> to gather telemetry data while acquiring and managing credentials.</param>
/// <param name="oAuthScope">The scope for the token (defaults to <see cref="GovernmentAuthenticationConstants.ToChannelFromBotOAuthScope"/> if null).</param>
public MicrosoftGovernmentAppCredentials(string appId, string password, HttpClient customHttpClient, ILogger logger, string oAuthScope = null)
: this(appId, password, tenantId: string.Empty, customHttpClient, logger, oAuthScope)
public MicrosoftGovernmentAppCredentials(string appId, string password, HttpClient customHttpClient = null, ILogger logger = null, string oAuthScope = null)
: base(appId, password, customHttpClient, logger, oAuthScope)
{
}

Expand All @@ -58,36 +35,22 @@ public MicrosoftGovernmentAppCredentials(string appId, string password, HttpClie
/// </summary>
/// <param name="appId">The Microsoft app ID.</param>
/// <param name="password">The Microsoft app password.</param>
/// <param name="tenantId">Tenant ID of the Azure AD tenant where the bot is created.</param>
/// <param name="channelAuthTenant">Optional. The oauth token tenant.</param>
/// <param name="customHttpClient">Optional <see cref="HttpClient"/> to be used when acquiring tokens.</param>
/// <param name="logger">Optional <see cref="ILogger"/> to gather telemetry data while acquiring and managing credentials.</param>
/// <param name="oAuthScope">The scope for the token (defaults to <see cref="GovernmentAuthenticationConstants.ToChannelFromBotOAuthScope"/> if null).</param>
public MicrosoftGovernmentAppCredentials(string appId, string password, string tenantId, HttpClient customHttpClient, ILogger logger, string oAuthScope = null)
: base(appId, password, tenantId, customHttpClient, logger, oAuthScope ?? GovernmentAuthenticationConstants.ToChannelFromBotOAuthScope)
public MicrosoftGovernmentAppCredentials(string appId, string password, string channelAuthTenant, HttpClient customHttpClient = null, ILogger logger = null, string oAuthScope = null)
: base(appId, password, channelAuthTenant, customHttpClient, logger, oAuthScope)
{
}

/// <summary>
/// Gets or sets tenant to be used for channel authentication.
/// </summary>
/// <value>
/// Tenant to be used for channel authentication.
/// </value>
public override string ChannelAuthTenant
{
get => string.IsNullOrEmpty(AuthTenant) ? GovernmentAuthenticationConstants.DefaultChannelAuthTenant : AuthTenant;
set => base.ChannelAuthTenant = value;
}
/// <inheritdoc/>
protected override string DefaultChannelAuthTenant => GovernmentAuthenticationConstants.DefaultChannelAuthTenant;

/// <summary>
/// Gets the OAuth endpoint to use.
/// </summary>
/// <value>
/// The OAuth endpoint to use.
/// </value>
public override string OAuthEndpoint
{
get => string.Format(CultureInfo.InvariantCulture, GovernmentAuthenticationConstants.ToChannelFromBotLoginUrlTemplate, ChannelAuthTenant);
}
/// <inheritdoc/>
protected override string ToChannelFromBotOAuthScope => GovernmentAuthenticationConstants.ToChannelFromBotOAuthScope;

/// <inheritdoc/>
protected override string ToChannelFromBotLoginUrlTemplate => GovernmentAuthenticationConstants.ToChannelFromBotLoginUrlTemplate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,25 @@ public override Task<ServiceClientCredentials> CreateCredentialsAsync(string app
if (loginEndpoint.StartsWith(AuthenticationConstants.ToChannelFromBotLoginUrlTemplate, StringComparison.OrdinalIgnoreCase))
{
return Task.FromResult<ServiceClientCredentials>(
new MsalAppCredentials(_clientApplication, appId, authority: null, scope: audience, validateAuthority: validateAuthority, logger: _logger));
new MsalAppCredentials(
_clientApplication,
appId,
authority: null,
scope: audience,
validateAuthority: validateAuthority,
logger: _logger));
}

// Legacy gov: Set the authority (login url) to the legacy gov url, and allow for passed in scope for skill auth in
// gov, or otherwise leave the default channel scope for gov.
if (loginEndpoint.Equals(GovernmentAuthenticationConstants.ToChannelFromBotLoginUrl, StringComparison.OrdinalIgnoreCase))
if (loginEndpoint.StartsWith(GovernmentAuthenticationConstants.ToChannelFromBotLoginUrlTemplate, StringComparison.OrdinalIgnoreCase))
{
return Task.FromResult<ServiceClientCredentials>(
new MsalAppCredentials(
_clientApplication,
appId,
authority: GovernmentAuthenticationConstants.ToChannelFromBotLoginUrl,
scope: audience ?? GovernmentAuthenticationConstants.ToChannelFromBotOAuthScope,
authority: null,
scope: audience,
validateAuthority: validateAuthority,
logger: _logger));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public override Task<ServiceClientCredentials> CreateCredentialsAsync(string app
return Task.FromResult<ServiceClientCredentials>(new MicrosoftAppCredentials(
appId, Password, TenantId, _httpClient, _logger, oauthScope));
}
else if (loginEndpoint.Equals(GovernmentAuthenticationConstants.ToChannelFromBotLoginUrl, StringComparison.OrdinalIgnoreCase))
else if (loginEndpoint.StartsWith(GovernmentAuthenticationConstants.ToChannelFromBotLoginUrlTemplate, StringComparison.OrdinalIgnoreCase))
{
return Task.FromResult<ServiceClientCredentials>(new MicrosoftGovernmentAppCredentials(
appId, Password, TenantId, _httpClient, _logger, oauthScope));
Expand Down
Loading

0 comments on commit 5e248ed

Please sign in to comment.