Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identity Adding Authenticate APIs to UsernamePasswordCredential #12502

Merged
merged 5 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sdk/identity/Azure.Identity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 1.2.0-preview.4 (Unreleased)

### New Features
- Added `Authenticate` methods to `UsernamePasswordCredential`

## 1.2.0-preview.3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ public partial class UsernamePasswordCredential : Azure.Core.TokenCredential
protected UsernamePasswordCredential() { }
public UsernamePasswordCredential(string username, string password, string tenantId, string clientId) { }
public UsernamePasswordCredential(string username, string password, string tenantId, string clientId, Azure.Identity.TokenCredentialOptions options) { }
public virtual Azure.Identity.AuthenticationRecord Authenticate(Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Identity.AuthenticationRecord Authenticate(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Identity.AuthenticationRecord> AuthenticateAsync(Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Identity.AuthenticationRecord> AuthenticateAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public override Azure.Core.AccessToken GetToken(Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public override System.Threading.Tasks.ValueTask<Azure.Core.AccessToken> GetTokenAsync(Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
}
Expand Down
69 changes: 69 additions & 0 deletions sdk/identity/Azure.Identity/src/UsernamePasswordCredential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ namespace Azure.Identity
/// </summary>
public class UsernamePasswordCredential : TokenCredential
{
private const string NoDefaultScopeMessage = "Authenticating in this environment requires specifying a TokenRequestContext.";

private readonly MsalPublicClient _client;
private readonly CredentialPipeline _pipeline;
private readonly string _username;
private readonly SecureString _password;
private AuthenticationRecord _record;


/// <summary>
Expand Down Expand Up @@ -76,6 +79,54 @@ internal UsernamePasswordCredential(string username, string password, Credential
_client = client;
}

/// <summary>
/// Authenticates the user using the specified username and password.
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>The <see cref="AuthenticationRecord"/> of the authenticated account.</returns>
public virtual AuthenticationRecord Authenticate(CancellationToken cancellationToken = default)
{
// get the default scope for the authority, throw if no default scope exists
string defaultScope = KnownAuthorityHosts.GetDefaultScope(_pipeline.AuthorityHost) ?? throw new CredentialUnavailableException(NoDefaultScopeMessage);

return Authenticate(new TokenRequestContext(new string[] { defaultScope }), cancellationToken);
}

/// <summary>
/// Authenticates the user using the specified username and password.
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>The <see cref="AuthenticationRecord"/> of the authenticated account.</returns>
public virtual async Task<AuthenticationRecord> AuthenticateAsync(CancellationToken cancellationToken = default)
{
// get the default scope for the authority, throw if no default scope exists
string defaultScope = KnownAuthorityHosts.GetDefaultScope(_pipeline.AuthorityHost) ?? throw new CredentialUnavailableException(NoDefaultScopeMessage);

return await AuthenticateAsync(new TokenRequestContext(new string[] { defaultScope }), cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Authenticates the user using the specified username and password.
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <param name="requestContext">The details of the authentication request.</param>
/// <returns>The <see cref="AuthenticationRecord"/> of the authenticated account.</returns>
public virtual AuthenticationRecord Authenticate(TokenRequestContext requestContext, CancellationToken cancellationToken = default)
{
return AuthenticateImplAsync(false, requestContext, cancellationToken).EnsureCompleted();
}

/// <summary>
/// Authenticates the user using the specified username and password.
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <param name="requestContext">The details of the authentication request.</param>
/// <returns>The <see cref="AuthenticationRecord"/> of the authenticated account.</returns>
public virtual async Task<AuthenticationRecord> AuthenticateAsync(TokenRequestContext requestContext, CancellationToken cancellationToken = default)
{
return await AuthenticateImplAsync(true, requestContext, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Obtains a token for a user account, authenticating them using the given username and password. Note: This will fail with
/// an <see cref="AuthenticationFailedException"/> if the specified user account has MFA enabled. This method is called by Azure SDK clients. It isn't intended for use in application code.
Expand All @@ -100,6 +151,22 @@ public override async ValueTask<AccessToken> GetTokenAsync(TokenRequestContext r
return await GetTokenImplAsync(true, requestContext, cancellationToken).ConfigureAwait(false);
}

private async Task<AuthenticationRecord> AuthenticateImplAsync(bool async, TokenRequestContext requestContext, CancellationToken cancellationToken)
{
using CredentialDiagnosticScope scope = _pipeline.StartGetTokenScope($"{nameof(UsernamePasswordCredential)}.{nameof(Authenticate)}", requestContext);

try
{
scope.Succeeded(await GetTokenImplAsync(async, requestContext, cancellationToken).ConfigureAwait(false));

return _record;
}
catch (Exception e)
{
throw scope.FailWrapAndThrow(e);
}
}

private async Task<AccessToken> GetTokenImplAsync(bool async, TokenRequestContext requestContext, CancellationToken cancellationToken)
{
using CredentialDiagnosticScope scope = _pipeline.StartGetTokenScope("UsernamePasswordCredential.GetToken", requestContext);
Expand All @@ -110,6 +177,8 @@ private async Task<AccessToken> GetTokenImplAsync(bool async, TokenRequestContex
.AcquireTokenByUsernamePasswordAsync(requestContext.Scopes, _username, _password, async, cancellationToken)
.ConfigureAwait(false);

_record = new AuthenticationRecord(result);

return scope.Succeeded(new AccessToken(result.AccessToken, result.ExpiresOn));
}
catch (Exception e)
Expand Down
Loading