Skip to content

Commit a733d82

Browse files
authored
Adding Azure.Identity.BrokeredAuthentication package (internals visible) (Azure#26772)
* Adding Azure.Identity.BrokeredAuthentication for broker authentication support * adding changelog * adding readme and tests * address feedback * add unit test * fb
1 parent 9d2509b commit a733d82

16 files changed

+374
-21
lines changed

eng/Packages.Data.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
<PackageReference Update="Microsoft.Azure.Amqp" Version="2.5.10" />
103103
<PackageReference Update="Microsoft.Azure.WebPubSub.Common" Version="1.1.0" />
104104
<PackageReference Update="Microsoft.Identity.Client" Version="4.39.0" />
105+
<PackageReference Update="Microsoft.Identity.Client.Desktop" Version="4.39.0" />
105106
<PackageReference Update="Microsoft.Identity.Client.Extensions.Msal" Version="2.19.3" />
106107

107108
<!-- TODO: Make sure this package is arch-board approved -->
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Release History
2+
3+
## 1.0.0-beta.1 (Unreleased)
4+
5+
### Features Added
6+
- Added `InteractiveBrowserCredentialBrokerOptions` to enable `InteractiveBrowserCredential` to use the authentication broker when this specicialized options type is used to construct the credential.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<!--
3+
Add any shared properties you want for the projects under this package directory that need to be set before the auto imported Directory.Build.props
4+
-->
5+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory).., Directory.Build.props))\Directory.Build.props" />
6+
</Project>
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Azure Identity Brokered Authentication client library for .NET
2+
The library extends the Azure.Identity library to provide authentication broker support. It includes the necessary dependencies, and provides the `InteractiveBrowserCredentialBrokerOptions` class. This options class can be used to create an `InteractiveBrowserCredential` capable of using the system authentication broker in lieu of the system browser when available.
3+
4+
[Source code][source] | [Package (nuget)][package] | [API reference documentation][identity_api_docs] | [Azure Active Directory documentation][aad_doc]
5+
6+
## Getting started
7+
8+
### Install the package
9+
10+
Install the Azure Identity client library for .NET with [NuGet][nuget]:
11+
12+
```PowerShell
13+
dotnet add package Azure.Identity.BrokeredAuthentication --prerelease
14+
```
15+
16+
### Prerequisites
17+
* The [Azure.Identity][azure_identity] library is a dependency of Azure.Identity.BrokeredAuthentication.
18+
19+
### Authenticate the client
20+
21+
## Key concepts
22+
23+
## Examples
24+
25+
### Configuring the `InteractiveBrowserCredential` to use the system authentication broker
26+
27+
This example demonstrates configuring the `InteractiveBrowserCredential` with the specialized options type `InteractiveBrowserCredentialBrokerOptions` to enable brokered authentication.
28+
29+
```C# Snippet:ConfigureInteractiveBrowserToUseBroker
30+
// Create an interactive browser credential which will use the system authentication broker
31+
var credential = new InteractiveBrowserCredential(new InteractiveBrowserCredentialBrokerOptions());
32+
33+
// Use the credential to authenticate a secret client
34+
var client = new SecretClient(new Uri("https://myvault.vault.azure.net/"), credential);
35+
```
36+
37+
## Troubleshooting
38+
39+
See the [troubleshooting guide](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/TROUBLESHOOTING.md) for details on how to diagnose various failure scenarios.
40+
41+
### Error Handling
42+
Errors arising from authentication can be raised on any service client method which makes a request to the service. This is because the first time the token is requested from the credential is on the first call to the service, and any subsequent calls might need to refresh the token. In order to distinguish these failures from failures in the service client Azure Identity classes raise the `AuthenticationFailedException` with details to the source of the error in the exception message as well as possibly the error message. Depending on the application these errors may or may not be recoverable.
43+
44+
``` c#
45+
using Azure.Identity;
46+
using Azure.Security.KeyVault.Secrets;
47+
48+
// Create a secret client using the DefaultAzureCredential
49+
var client = new SecretClient(new Uri("https://myvault.vault.azure.net/"), new DefaultAzureCredential());
50+
51+
try
52+
{
53+
KeyVaultSecret secret = await client.GetSecretAsync("secret1");
54+
}
55+
catch (AuthenticationFailedException e)
56+
{
57+
Console.WriteLine($"Authentication Failed. {e.Message}");
58+
}
59+
```
60+
61+
For more details on dealing with errors arising from failed requests to Azure Active Directory, or managed identity endpoints please refer to the Azure Active Directory [documentation on authorization error codes][aad_err_doc].
62+
63+
### Logging
64+
65+
The Azure Identity library provides the same [logging capabilities](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Diagnostics.md#logging) as the rest of the Azure SDK.
66+
67+
The simplest way to see the logs to help debug authentication issues is to enable the console logging.
68+
69+
``` c#
70+
// Setup a listener to monitor logged events.
71+
using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger();
72+
```
73+
74+
All credentials can be configured with diagnostic options, in the same way as other clients in the SDK.
75+
76+
``` c#
77+
DefaultAzureCredentialOptions options = new DefaultAzureCredentialOptions()
78+
{
79+
Diagnostics =
80+
{
81+
LoggedHeaderNames = { "x-ms-request-id" },
82+
LoggedQueryParameters = { "api-version" },
83+
IsLoggingContentEnabled = true
84+
}
85+
};
86+
```
87+
88+
> CAUTION: Requests and responses in the Azure Identity library contain sensitive information. Precaution must be taken to protect logs when customizing the output to avoid compromising account security.
89+
90+
### Thread safety
91+
We guarantee that all credential instance methods are thread-safe and independent of each other ([guideline](https://azure.github.io/azure-sdk/dotnet_introduction.html#dotnet-service-methods-thread-safety)).
92+
This ensures that the recommendation of reusing credential instances is always safe, even across threads.
93+
94+
### Additional concepts
95+
[Client options](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/README.md#configuring-service-clients-using-clientoptions) |
96+
[Accessing the response](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/README.md#accessing-http-response-details-using-responset) |
97+
[Diagnostics](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/samples/Diagnostics.md) |
98+
[Mocking](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/README.md#mocking) |
99+
[Client lifetime](https://devblogs.microsoft.com/azure-sdk/lifetime-management-and-thread-safety-guarantees-of-azure-sdk-net-clients/)
100+
101+
## Next steps
102+
103+
### Client libraries supporting authentication with Azure Identity
104+
105+
Many of the client libraries listed [here](https://azure.github.io/azure-sdk/releases/latest/dotnet.html) support authenticating with `TokenCredential` and the Azure Identity library.
106+
There you will also find links where you can learn more about their use, including additional documentation and samples.
107+
108+
### Known Issues
109+
110+
This library does not currently support scenarios relating to the [AAD B2C](https://docs.microsoft.com/azure/active-directory-b2c/overview) service.
111+
112+
Currently open issues for the Azure.Identity library can be found [here](https://github.com/Azure/azure-sdk-for-net/issues?q=is%3Aissue+is%3Aopen+label%3AAzure.Identity).
113+
114+
## Contributing
115+
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
116+
117+
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
118+
119+
This project has adopted the [Microsoft Open Source Code of Conduct][code_of_conduct]. For more information see the [Code of Conduct FAQ][code_of_conduct_faq] or contact [email protected] with any additional questions or comments.
120+
121+
<!-- LINKS -->
122+
[azure_cli]: https://docs.microsoft.com/cli/azure
123+
[azure_powerShell]: https://docs.microsoft.com/powershell/azure
124+
[azure_sub]: https://azure.microsoft.com/free/dotnet/
125+
[azure_identity]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/identity/Azure.Identity/README.md
126+
[source]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/identity/Azure.Identity.BrokeredAuthentication/src
127+
[package]: https://www.nuget.org/packages?q=Azure.Identity.BrokeredAuthentication
128+
[aad_doc]: https://docs.microsoft.com/azure/active-directory/
129+
[aad_err_doc]: https://docs.microsoft.com/azure/active-directory/develop/reference-aadsts-error-codes
130+
[certificates_client_library]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/keyvault/Azure.Security.KeyVault.Certificates
131+
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/
132+
[code_of_conduct_faq]: https://opensource.microsoft.com/codeofconduct/faq/
133+
[nuget]: https://www.nuget.org/
134+
[keys_client_library]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/keyvault/Azure.Security.KeyVault.Keys
135+
[secrets_client_library]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/keyvault/Azure.Security.KeyVault.Secrets
136+
[blobs_client_library]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/storage/Azure.Storage.Blobs
137+
[queues_client_library]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/storage/Azure.Storage.Queues
138+
[eventhubs_client_library]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/eventhub/Azure.Messaging.EventHubs
139+
[azure_core_library]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/core/Azure.Core
140+
[identity_api_docs]: https://docs.microsoft.com/dotnet/api/azure.identity?view=azure-dotnet
141+
[vs_login_image]: https://raw.githubusercontent.com/Azure/azure-sdk-for-net/main/sdk/identity/Azure.Identity/images/VsLoginDialog.png
142+
[azure_cli_login_image]: https://raw.githubusercontent.com/Azure/azure-sdk-for-net/main/sdk/identity/Azure.Identity/images/AzureCliLogin.png
143+
[azure_cli_login_device_code_image]: https://raw.githubusercontent.com/Azure/azure-sdk-for-net/main/sdk/identity/Azure.Identity/images/AzureCliLoginDeviceCode.png
144+
[default_azure_credential_authflow_image]: https://raw.githubusercontent.com/Azure/azure-sdk-for-net/main/sdk/identity/Azure.Identity/images/DefaultAzureCredentialAuthenticationFlow.png
145+
[ref_DefaultAzureCredential]: https://docs.microsoft.com/dotnet/api/azure.identity.defaultazurecredential?view=azure-dotnet
146+
[ref_ChainedTokenCredential]: https://docs.microsoft.com/dotnet/api/azure.identity.chainedtokencredential?view=azure-dotnet
147+
[ref_EnvironmentCredential]: https://docs.microsoft.com/dotnet/api/azure.identity.environmentcredential?view=azure-dotnet
148+
[ref_ManagedIdentityCredential]: https://docs.microsoft.com/dotnet/api/azure.identity.managedidentitycredential?view=azure-dotnet
149+
[ref_ClientSecretCredential]: https://docs.microsoft.com/dotnet/api/azure.identity.clientsecretcredential?view=azure-dotnet
150+
[ref_ClientCertificateCredential]: https://docs.microsoft.com/dotnet/api/azure.identity.clientcertificatecredential?view=azure-dotnet
151+
[ref_InteractiveBrowserCredential]: https://docs.microsoft.com/dotnet/api/azure.identity.interactivebrowsercredential?view=azure-dotnet
152+
[ref_DeviceCodeCredential]: https://docs.microsoft.com/dotnet/api/azure.identity.devicecodecredential?view=azure-dotnet
153+
[ref_UsernamePasswordCredential]: https://docs.microsoft.com/dotnet/api/azure.identity.usernamepasswordcredential?view=azure-dotnet
154+
[ref_AuthorizationCodeCredential]: https://docs.microsoft.com/dotnet/api/azure.identity.authorizationcodecredential?view=azure-dotnet
155+
[ref_AzureCliCredential]: https://docs.microsoft.com/dotnet/api/azure.identity.azureclicredential?view=azure-dotnet
156+
[ref_AzurePowerShellCredential]: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/src/AzurePowerShellCredential.cs
157+
[ref_VisualStudioCredential]: https://docs.microsoft.com/dotnet/api/azure.identity.visualstudiocredential?view=azure-dotnet
158+
[ref_VisualStudioCodeCredential]: https://docs.microsoft.com/dotnet/api/azure.identity.visualstudiocodecredential?view=azure-dotnet
159+
160+
![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-net%2Fsdk%2Fidentity%2FAzure.Identity%2FREADME.png)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<Description>This is the implementation of the Azure SDK Client Library for Azure Identity</Description>
4+
<AssemblyTitle>Microsoft Azure.Identity.BrokeredAuthentication Component</AssemblyTitle>
5+
<Version>1.0.0-beta.1</Version>
6+
<!--The ApiCompatVersion is managed automatically and should not generally be modified manually.-->
7+
<!-- <ApiCompatVersion>1.0.0</ApiCompatVersion> -->
8+
<PackageTags>Microsoft Azure Identity Broker;$(PackageCommonTags)</PackageTags>
9+
<TargetFrameworks>$(RequiredTargetFrameworks);net461</TargetFrameworks>
10+
<NoWarn>$(NoWarn);3021</NoWarn>
11+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
12+
</PropertyGroup>
13+
<ItemGroup>
14+
<PackageReference Include="Azure.Core" />
15+
<!--<PackageReference Include="Azure.Identity" />-->
16+
<!-- This should be changed back to a package reference once the required changes in Azure.Identity have shipped in the next GA -->
17+
<ProjectReference Include="..\..\Azure.Identity\src\Azure.Identity.csproj" />
18+
<PackageReference Include="System.Memory" />
19+
<PackageReference Include="System.Text.Json" />
20+
<PackageReference Include="System.Threading.Tasks.Extensions" />
21+
<PackageReference Include="Microsoft.Identity.Client" />
22+
<PackageReference Include="Microsoft.Identity.Client.Extensions.Msal" />
23+
<PackageReference Include="System.Security.Cryptography.ProtectedData" />
24+
</ItemGroup>
25+
<!--Only Add Microsoft.Identity.Client.Desktop when compiled for desktop-->
26+
<ItemGroup Condition="('$(TargetFramework)' == 'net461')">
27+
<PackageReference Include="Microsoft.Identity.Client.Desktop" />
28+
</ItemGroup>
29+
</Project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Identity.Client;
5+
#if (NETFRAMEWORK)
6+
using Microsoft.Identity.Client.Desktop;
7+
#endif
8+
9+
namespace Azure.Identity.BrokeredAuthentication
10+
{
11+
/// <summary>
12+
/// Options to configure the <see cref="InteractiveBrowserCredential"/> to use the system authentication broker in lieu of the system browser if available.
13+
/// </summary>
14+
public class InteractiveBrowserCredentialBrokerOptions : InteractiveBrowserCredentialOptions
15+
{
16+
/// <summary>
17+
/// Creates a new instance of <see cref="InteractiveBrowserCredentialBrokerOptions"/>.
18+
/// </summary>
19+
public InteractiveBrowserCredentialBrokerOptions()
20+
{
21+
this.BeforeBuildClient = AddBroker;
22+
}
23+
24+
private void AddBroker(PublicClientApplicationBuilder builder)
25+
{
26+
#if (NETFRAMEWORK)
27+
builder.WithWindowsBroker();
28+
#else
29+
builder.WithBroker();
30+
#endif
31+
}
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
5+
<IsPackable>false</IsPackable>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="NUnit" />
10+
<PackageReference Include="NUnit3TestAdapter" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" />
12+
<PackageReference Include="Moq" />
13+
<PackageReference Include="BenchmarkDotNet" />
14+
<PackageReference Include="Azure.Security.KeyVault.Secrets" />
15+
</ItemGroup>
16+
17+
18+
<ItemGroup>
19+
<ProjectReference Include="$(AzureCoreTestFramework)" />
20+
<ProjectReference Include="..\..\Azure.Identity\src\Azure.Identity.csproj" />
21+
<ProjectReference Include="..\src\Azure.Identity.BrokeredAuthentication.csproj" />
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Threading.Tasks;
5+
using Azure.Core;
6+
using NUnit.Framework;
7+
8+
namespace Azure.Identity.BrokeredAuthentication.Tests
9+
{
10+
public class ManualInteractiveBrowserCredentialBrokerTests
11+
{
12+
[Test]
13+
[Ignore("This test is an integration test which can only be run with user interaction")]
14+
public async Task AuthenticateWithBrokerAsync()
15+
{
16+
// to fully manually verify the InteractiveBrowserCredential this test should be run both authenticating with a
17+
// school / organization account as well as a personal live account, i.e. a @outlook.com, @live.com, or @hotmail.com
18+
var cred = new InteractiveBrowserCredential(new InteractiveBrowserCredentialBrokerOptions());
19+
20+
AccessToken token = await cred.GetTokenAsync(new TokenRequestContext(new string[] { "https://vault.azure.net/.default" })).ConfigureAwait(false);
21+
22+
Assert.NotNull(token.Token);
23+
}
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using Azure.Identity.BrokeredAuthentication;
6+
using Azure.Security.KeyVault.Secrets;
7+
using NUnit.Framework;
8+
9+
namespace Azure.Identity.Samples
10+
{
11+
public class ReadmeSnippets
12+
{
13+
[Test]
14+
public void ConfigureInteractiveBrowserToUseBroker()
15+
{
16+
#region Snippet:ConfigureInteractiveBrowserToUseBroker
17+
// Create an interactive browser credential which will use the system authentication broker
18+
var credential = new InteractiveBrowserCredential(new InteractiveBrowserCredentialBrokerOptions());
19+
20+
// Use the credential to authenticate a secret client
21+
var client = new SecretClient(new Uri("https://myvault.vault.azure.net/"), credential);
22+
#endregion
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)