Skip to content

Commit

Permalink
[Identity] Add IdentityModelFactory to facilitate mocking (Azure#14769)
Browse files Browse the repository at this point in the history
* [Identity] Add IdentityModelFactory to facilitate mocking

* update API spec and changelog
  • Loading branch information
schaabs authored and erich-wang committed Sep 7, 2020
1 parent 2a1f6c6 commit c2ff7f4
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
8 changes: 8 additions & 0 deletions sdk/identity/Azure.Identity/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# Release History
## 1.3.0-preview.1 (Unreleased)

### New Features
- Restoring Application Authentication APIs from 1.2.0-preview.6
- Added `IncludeX5CClaimHeader` to `ClientCertificateCredentialOptions` to enable subject name / issuer authentication with the `ClientCertificateCredential`.
- Added `RedirectUri` to `InteractiveBrowserCredentialOptions` to enable authentication with user specified application with a custom redirect url.
- Added `IdentityModelFactory` to enable constructing models from the Azure.Identity library for mocking.

### Fixes and improvements
- Fixed issue with non GUID Client Ids (Issue [#14585](https://github.com/Azure/azure-sdk-for-net/issues/14585))


## 1.2.2 (2020-08-20)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ public EnvironmentCredential(Azure.Identity.TokenCredentialOptions options) { }
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; }
}
public static partial class IdentityModelFactory
{
public static Azure.Identity.AuthenticationRecord AuthenticationRecord(string username, string authority, string homeAccountId, string tenantId, string clientId) { throw null; }
public static Azure.Identity.DeviceCodeInfo DeviceCodeInfo(string userCode, string deviceCode, System.Uri verificationUri, System.DateTimeOffset expiresOn, string message, string clientId, System.Collections.Generic.IReadOnlyCollection<string> scopes) { throw null; }
}
public partial class InteractiveBrowserCredential : Azure.Core.TokenCredential
{
public InteractiveBrowserCredential() { }
Expand Down
20 changes: 12 additions & 8 deletions sdk/identity/Azure.Identity/src/DeviceCodeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Text;

namespace Azure.Identity
{
Expand All @@ -14,14 +13,19 @@ namespace Azure.Identity
public struct DeviceCodeInfo
{
internal DeviceCodeInfo(DeviceCodeResult deviceCode)
: this(deviceCode.UserCode, deviceCode.DeviceCode, new Uri(deviceCode.VerificationUrl), deviceCode.ExpiresOn, deviceCode.Message, deviceCode.ClientId, deviceCode.Scopes)
{
UserCode = deviceCode.UserCode;
DeviceCode = deviceCode.DeviceCode;
VerificationUri = new Uri(deviceCode.VerificationUrl);
ExpiresOn = deviceCode.ExpiresOn;
Message = deviceCode.Message;
ClientId = deviceCode.ClientId;
Scopes = deviceCode.Scopes;
}

internal DeviceCodeInfo(string userCode, string deviceCode, Uri verificationUri, DateTimeOffset expiresOn, string message, string clientId, IReadOnlyCollection<string> scopes)
{
UserCode = userCode;
DeviceCode = deviceCode;
VerificationUri = verificationUri;
ExpiresOn = expiresOn;
Message = message;
ClientId = clientId;
Scopes = scopes;
}

/// <summary>
Expand Down
41 changes: 41 additions & 0 deletions sdk/identity/Azure.Identity/src/IdentityModelFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;

namespace Azure.Identity
{

/// <summary>
/// Model factory that enables mocking for the Azure Identity library.
/// </summary>
public static class IdentityModelFactory
{
/// <summary>
/// Initializes a new instance of the <see cref="AuthenticationRecord"/> class for mocking purposes.
/// </summary>
/// <param name="username">Sets the <see cref="AuthenticationRecord.Username"/>.</param>
/// <param name="authority">Sets the <see cref="AuthenticationRecord.Authority"/>.</param>
/// <param name="homeAccountId">Sets the <see cref="AuthenticationRecord.HomeAccountId"/>.</param>
/// <param name="tenantId">Sets the <see cref="AuthenticationRecord.TenantId"/>.</param>
/// <param name="clientId">Sets the <see cref="AuthenticationRecord.ClientId"/>.</param>
/// <returns>A new instance of the <see cref="AuthenticationRecord"/> for mocking purposes.</returns>
public static AuthenticationRecord AuthenticationRecord(string username, string authority, string homeAccountId, string tenantId, string clientId)
=> new AuthenticationRecord(username, authority, homeAccountId, tenantId, clientId);

/// <summary>
/// Initializes a new instance of the <see cref="DeviceCodeInfo"/> class for mocking purposes.
/// </summary>
/// <param name="userCode">Sets the <see cref="DeviceCodeInfo.UserCode"/>.</param>
/// <param name="deviceCode">Sets the <see cref="DeviceCodeInfo.DeviceCode"/>.</param>
/// <param name="verificationUri">Sets the <see cref="DeviceCodeInfo.VerificationUri"/>.</param>
/// <param name="expiresOn">Sets the <see cref="DeviceCodeInfo.ExpiresOn"/>.</param>
/// <param name="message">Sets the <see cref="DeviceCodeInfo.Message"/>.</param>
/// <param name="clientId">Sets the <see cref="DeviceCodeInfo.ClientId"/>.</param>
/// <param name="scopes">Sets the <see cref="DeviceCodeInfo.Scopes"/>.</param>
/// <returns>A new instance of the <see cref="DeviceCodeInfo"/> for mocking purposes.</returns>
public static DeviceCodeInfo DeviceCodeInfo(string userCode, string deviceCode, Uri verificationUri, DateTimeOffset expiresOn, string message, string clientId, IReadOnlyCollection<string> scopes)
=> new DeviceCodeInfo(userCode, deviceCode, verificationUri, expiresOn, message, clientId, scopes);
}
}

0 comments on commit c2ff7f4

Please sign in to comment.