Skip to content

Commit

Permalink
Identity Adding Authenticate APIs to UsernamePasswordCredential (#12502)
Browse files Browse the repository at this point in the history
* Adding Authenticate APIs to UsernamePasswordCredential

* updating test recordings

* updating api listing

* update changelog
  • Loading branch information
schaabs authored Jun 5, 2020
1 parent 6c96ee3 commit 4ae35d4
Show file tree
Hide file tree
Showing 10 changed files with 1,444 additions and 85 deletions.
1 change: 1 addition & 0 deletions sdk/identity/Azure.Identity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### New Features
- Makes `AzureCliCredential`, `VisualStudioCredential` and `VisualStudioCodeCredential` public to allow direct usage.
- Added `Authenticate` methods to `UsernamePasswordCredential`

## 1.2.0-preview.3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,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

1 comment on commit 4ae35d4

@yukfang
Copy link

@yukfang yukfang commented on 4ae35d4 Jun 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Scott, do we have any ETA for the 1.2.0 GA version?

Please sign in to comment.