Skip to content

Commit d6d30da

Browse files
schaabserich-wangazure-sdk
authored andcommitted
[Identity] Merging feature/identity/140 back to master (Azure#17083)
* {Identity] Prototype TokenCache option to enable sharing cache across credentials and executions * prototype updates * adding client side user authentication samples * adding persistent token cache options * fix compilation issues * adding token cache samples * fix header * reword * updating API spec * temporary fix for AzureStack eng bits * workaround for MSA account * ignore failure test cases temporarily * update api sig * update version * update change log * update version * fix changelog * fix msal cache * update msal version * removing AuthenticationTokenRecord workaround * adding configuration to SharedTokenCacheCredential * upgrading msal * update snippet * [Identity] prepare for 1.4.0-beta.1 release (Azure#16021) * Updating changelog for 1.4.0-beta.1 release * updating MSAL dependency * Increment package version after release of Azure.Identity (Azure#16028) * make AuthenticationRecord public * making APIs public that got switched to internal in merge * adressing PR feedback * add tests for new STCC options Co-authored-by: Erich(Renyong) Wang <[email protected]> Co-authored-by: Azure SDK Bot <[email protected]>
1 parent 1bd46df commit d6d30da

34 files changed

+1143
-154
lines changed

sdk/identity/Azure.Identity/CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# Release History
2+
## 1.4.0-beta.2 (Unreleased)
23

3-
## 1.4.0-beta.1 (Unreleased)
44

5+
## 1.4.0-beta.1 (2020-10-15)
6+
7+
### New Features
8+
- Redesigned Application Authentication APIs
9+
- Adds `TokenCache` and `PersistentTokenCache` classes to give more user control over how the tokens are cached and how the cache is persisted.
10+
- Adds `TokenCache` property to options for credentials supporting token cache configuration.
511

612
## 1.3.0 (2020-11-12)
713

sdk/identity/Azure.Identity/api/Azure.Identity.netstandard2.0.cs

Lines changed: 83 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Client side user authentication
2+
3+
Client side applications often need to authenticate users to interact with resources in Azure. Some examples of this might be a command line tool which fetches secrets a user has access to from a key vault to setup a local test environment, or a GUI based application which allows a user to browse storage blobs they have access to. This sample demonstrates authenticating users with the `Azure.Identity` library.
4+
5+
## Interactive user authentication
6+
7+
Most often authenticating users requires some user interaction. Properly handling this user interaction for OAuth2 authorization code or device code authentication can be challenging. To simplify this for client side applications the `Azure.Identity` library provides the `InteractiveBrowserCredential` and the `DeviceCodeCredential`. These credentials are designed to handle the user interactions needed to authenticate via these two client side authentication flows, so the application developer can simply create the credential and authenticate clients with it.
8+
9+
10+
## Authenticating users with the InteractiveBrowserCredential
11+
12+
For clients which have a default browser available, the `InteractiveBrowserCredential` provides the most simple user authentication experience. In the sample below an application authenticates a `SecretClient` using the `InteractiveBrowserCredential`.
13+
14+
```C# Snippet:Identity_ClientSideUserAuthentication_SimpleInteractiveBrowser
15+
var client = new SecretClient(new Uri("https://myvault.azure.vaults.net/"), new InteractiveBrowserCredential());
16+
```
17+
As code uses the `SecretClient` in the above sample, the `InteractiveBrowserCredential` will automatically authenticate the user by launching the default system browser prompting the user to login. In this case the user interaction happens on demand as is necessary to authenticate calls from the client.
18+
19+
20+
## Authenticating users with the DeviceCodeCredential
21+
22+
For terminal clients without an available web browser, or clients with limited UI capabilities the `DeviceCodeCredential` provides the ability to authenticate any client using a device code. The next sample shows authenticating a `BlobClient` using the `DeviceCodeCredential`.
23+
24+
```C# Snippet:Identity_ClientSideUserAuthentication_SimpleDeviceCode
25+
var credential = new DeviceCodeCredential();
26+
27+
var client = new BlobClient(new Uri("https://myaccount.blob.core.windows.net/mycontainer/myblob"), credential);
28+
```
29+
Similarly to the `InteractiveBrowserCredential` the `DeviceCodeCredential` will also initiate the user interaction automatically as needed. To instantiate the `DeviceCodeCredential` the application must provide a callback which is called to display the device code along with details on how to authenticate to the user. In the above sample a lambda is provided which prints the full device code message to the console.
30+
31+
32+
## Controlling user interaction
33+
34+
In many cases applications require tight control over user interaction. In these applications automatically blocking on required user interaction is often undesired or impractical. For this reason, credentials in the `Azure.Identity` library which interact with the user offer mechanisms to fully control user interaction.
35+
36+
```C# Snippet:Identity_ClientSideUserAuthentication_DisableAutomaticAuthentication
37+
var credential = new InteractiveBrowserCredential(new InteractiveBrowserCredentialOptions { DisableAutomaticAuthentication = true });
38+
39+
await credential.AuthenticateAsync();
40+
41+
var client = new SecretClient(new Uri("https://myvault.azure.vaults.net/"), credential);
42+
```
43+
In this sample the application is again using the `InteractiveBrowserCredential` to authenticate a `SecretClient`, but with two major differences from our first example. First, in this example the application is explicitly forcing any user interaction to happen before the credential is given to the client by calling `AuthenticateAsync`.
44+
45+
The second difference is here the application is preventing the credential from automatically initiating user interaction. Even though the application authenticates the user before the credential is used, further interaction might still be needed, for instance in the case that the user's refresh token expires, or a specific method require additional consent or authentication.
46+
47+
By setting the option `DisableAutomaticAuthentication` to `true` the credential will fail to automatically authenticate calls where user interaction is necessary. Instead, the credential will throw an `AuthenticationRequiredException`. The following example demonstrates an application handling such an exception to prompt the user to authenticate only after some application logic has completed.
48+
49+
```C# Snippet:Identity_ClientSideUserAuthentication_DisableAutomaticAuthentication_ExHandling
50+
try
51+
{
52+
client.GetSecret("secret");
53+
}
54+
catch (AuthenticationRequiredException e)
55+
{
56+
await EnsureAnimationCompleteAsync();
57+
58+
await credential.AuthenticateAsync(e.TokenRequestContext);
59+
60+
client.GetSecret("secret");
61+
}
62+
```
63+
64+
## Persisting user authentication data
65+
66+
Quite often applications desire the ability to be run multiple times without having to reauthenticate the user on each execution. This requires that data from the original authentication be persisted outside of the application memory, so that it can authenticate silently on subsequent executions. Specifically two pieces of data need to be persisted, the `TokenCache` and the `AuthenticationRecord`.
67+
68+
### Persisting the TokenCache
69+
70+
The `TokenCache` contains all the data needed to silently authenticate, one or many accounts. It contains sensitive data such as refresh tokens, and access tokens and must be protected to prevent compromising the accounts it houses tokens for. The `Azure.Identity` library provides the `PersistentTokenCache` class which by default will protect and persist the cache using available platform data protection.
71+
72+
To use the `PersistentTokenCache` to persist the cache of any credential simply set the `TokenCache` option.
73+
74+
```C# Snippet:Identity_ClientSideUserAuthentication_Persist_TokenCache
75+
var credential = new InteractiveBrowserCredential(new InteractiveBrowserCredentialOptions { TokenCache = new PersistentTokenCache() });
76+
```
77+
78+
### Persisting the AuthenticationRecord
79+
80+
The `AuthenticationRecord` which is returned from the `Authenticate` and `AuthenticateAsync`, contains data identifying an authenticated account. It is needed to identify the appropriate entry in the `TokenCache` to silently authenticate on subsequent executions. There is no sensitive data in the `AuthenticationRecord` so it can be persisted in a non-protected state.
81+
82+
Here is an example of an application storing the `AuthenticationRecord` to the local file system after authenticating the user.
83+
84+
```C# Snippet:Identity_ClientSideUserAuthentication_Persist_AuthRecord
85+
AuthenticationRecord authRecord = await credential.AuthenticateAsync();
86+
87+
using var authRecordStream = new FileStream(AUTH_RECORD_PATH, FileMode.Create, FileAccess.Write);
88+
89+
await authRecord.SerializeAsync(authRecordStream);
90+
91+
await authRecordStream.FlushAsync();
92+
```
93+
### Silent authentication with AuthenticationRecord and PersistentTokenCache
94+
95+
Once an application has persisted both the `TokenCache` and the `AuthenticationRecord` this data can be used to silently authenticate. This example demonstrates an application using the `PersistentTokenCache` and retrieving an `AuthenticationRecord` from the local file system to create an `InteractiveBrowserCredential` capable of silent authentication.
96+
97+
```C# Snippet:Identity_ClientSideUserAuthentication_Persist_SilentAuth
98+
using var authRecordStream = new FileStream(AUTH_RECORD_PATH, FileMode.Open, FileAccess.Read);
99+
100+
AuthenticationRecord authRecord = await AuthenticationRecord.DeserializeAsync(authRecordStream);
101+
102+
var credential = new InteractiveBrowserCredential(new InteractiveBrowserCredentialOptions { TokenCache = new PersistentTokenCache(), AuthenticationRecord = authRecord });
103+
```
104+
105+
The credential created in this example will silently authenticate given that a valid token for corresponding to the `AuthenticationRecord` still exists in the `TokenCache`. There are some cases where interaction will still be required such as on token expiry, or when additional authentication is required for a particular resource.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Persisting the credential TokenCache
2+
Many credential implementations in the Azure.Identity library have an underlying `TokenCache` which caches sensitive authentication data such as account information, access tokens, and refresh tokens. By default this `TokenCache` instance is an in memory cache which is specific to the credential instance. However, there are scenarios where an application needs to share the token cache across credentials, and persist it across executions. To accomplish this the Azure.Identity provides the `TokenCache` and `PeristantTokenCache` classes.
3+
4+
>IMPORTANT! The `TokenCache` contains sensitive data and **MUST** be protected to prevent compromising accounts. All application decisions regarding the storage of the `TokenCache` must consider that a breach of its content will fully compromise all the accounts it contains.
5+
6+
## Using the default PersistentTokenCache
7+
8+
The simplest way to persist the `TokenCache` of a credential is to to use the default `PersistentTokenCache`. This will persist and read the `TokenCache` from a shared persisted token cache protected to the current account.
9+
10+
```C# Snippet:Identity_TokenCache_PersistentDefault
11+
var credential = new InteractiveBrowserCredential(new InteractiveBrowserCredentialOptions { TokenCache = new PersistentTokenCache() });
12+
```
13+
14+
## Using a named PersistentTokenCache
15+
16+
Some applications may prefer to isolate the `PersistentTokenCache` they user rather than using the shared instance. To accomplish this they can specify a `PersistentTokenCacheOptions` when creating the `PersistentTokenCache` and provide a `Name` for the persisted cache instance.
17+
18+
```C# Snippet:Identity_TokenCache_PersistentNamed
19+
var tokenCache = new PersistentTokenCache(new PersistentTokenCacheOptions { Name = "my_application_name" });
20+
21+
var credential = new InteractiveBrowserCredential(new InteractiveBrowserCredentialOptions { TokenCache = tokenCache });
22+
```
23+
24+
## Allowing unencrypted storage
25+
By default the `PersistentTokenCache` will protect any data which is persisted using the user data protection APIs available on the current platform. However, there are cases where no data protection is available, and applications may choose to still persist the token cache in an unencrypted state. This is accomplished with the `AllowUnencryptedStorage` option.
26+
27+
```C# Snippet:Identity_TokenCache_PersistentUnencrypted
28+
var tokenCache = new PersistentTokenCache(new PersistentTokenCacheOptions { AllowUnencryptedStorage = true });
29+
30+
var credential = new InteractiveBrowserCredential(new InteractiveBrowserCredentialOptions { TokenCache = tokenCache});
31+
```
32+
By setting `AllowUnencryptedStorage` to `true`, the `PersistentTokenCache` will encrypt the contents of the `TokenCache` before persisting it if data protection is available on the current platform, otherwise it will write and read the `TokenCache` data to an unencrypted local file ACL'd to the current account. If `AllowUnencryptedStorage` is `false` (the default) a `CredentialUnavailableException` will be raised in the case no data protection is available.
33+
34+
## Implementing custom TokenCache persistence
35+
Some applications may require complete control of how the `TokenCache` is persisted. To enable this the `TokenCache` provides the methods `Serialize`, `SerializeAsync`, `Deserialize` and `DeserializeAsync` methods so applications can write the `TokenCache` to any stream. The following samples illustrate how to use these serialization methods to write and read the cache from a stream.
36+
37+
> IMPORTANT! This sample assumes the location of the file it is using for storage is secure. The `Serialize` and `SerializeAsync` methods will write the unencrypted content of the `TokenCache` to the provide stream. It is the responsibility the implementer to properly protect the `TokenCache` data.
38+
39+
The `Serialize` or `SerializeAsync` methods can be used to write out content of a `TokenCache` to any writeable stream.
40+
41+
```C# Snippet:Identity_TokenCache_CustomPersistence_Write
42+
using var cacheStream = new FileStream(TokenCachePath, FileMode.Create, FileAccess.Write);
43+
44+
await tokenCache.SerializeAsync(cacheStream);
45+
```
46+
47+
The `Deserialize` or `DeserializeAsync` methods can be used to read the content of a `TokenCache` from any readable stream.
48+
49+
```C# Snippet:Identity_TokenCache_CustomPersistence_Read
50+
using var cacheStream = new FileStream(TokenCachePath, FileMode.OpenOrCreate, FileAccess.Read);
51+
52+
var tokenCache = await TokenCache.DeserializeAsync(cacheStream);
53+
```
54+
55+
Applications can combine these methods along with the `Updated` event to automatically persist and read the token from a storage solution of their choice.
56+
```C# Snippet:Identity_TokenCache_CustomPersistence_Usage
57+
public static async Task<TokenCache> ReadTokenCacheAsync()
58+
{
59+
using var cacheStream = new FileStream(TokenCachePath, FileMode.OpenOrCreate, FileAccess.Read);
60+
61+
var tokenCache = await TokenCache.DeserializeAsync(cacheStream);
62+
63+
tokenCache.Updated += WriteCacheOnUpdateAsync;
64+
65+
return tokenCache;
66+
}
67+
68+
public static async Task WriteCacheOnUpdateAsync(TokenCacheUpdatedArgs args)
69+
{
70+
using var cacheStream = new FileStream(TokenCachePath, FileMode.Create, FileAccess.Write);
71+
72+
await args.Cache.SerializeAsync(cacheStream);
73+
}
74+
75+
public static async Task Main()
76+
{
77+
var tokenCache = await ReadTokenCacheAsync();
78+
79+
var credential = new InteractiveBrowserCredential(new InteractiveBrowserCredentialOptions { TokenCache = tokenCache });
80+
}
81+
```

sdk/identity/Azure.Identity/src/AuthenticationRecord.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Azure.Identity
1515
/// <summary>
1616
/// Account information relating to an authentication request.
1717
/// </summary>
18-
internal class AuthenticationRecord
18+
public class AuthenticationRecord
1919
{
2020
private const string UsernamePropertyName = "username";
2121
private const string AuthorityPropertyName = "authority";

sdk/identity/Azure.Identity/src/AuthenticationRequiredException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Azure.Identity
99
/// <summary>
1010
/// An exception indicating that interactive authentication is required.
1111
/// </summary>
12-
internal class AuthenticationRequiredException : CredentialUnavailableException
12+
public class AuthenticationRequiredException : CredentialUnavailableException
1313
{
1414
/// <summary>
1515
/// Creates a new <see cref="AuthenticationRequiredException"/> with the specified message and context.

sdk/identity/Azure.Identity/src/Azure.Identity.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<Description>This is the implementation of the Azure SDK Client Library for Azure Identity</Description>
44
<AssemblyTitle>Microsoft Azure.Identity Component</AssemblyTitle>
5-
<Version>1.4.0-beta.1</Version>
5+
<Version>1.4.0-beta.2</Version>
66
<ApiCompatVersion>1.3.0</ApiCompatVersion>
77
<PackageTags>Microsoft Azure Identity;$(PackageCommonTags)</PackageTags>
88
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>

sdk/identity/Azure.Identity/src/ClientCertificateCredentialOptions.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,14 @@ namespace Azure.Identity
88
/// </summary>
99
public class ClientCertificateCredentialOptions : TokenCredentialOptions, ITokenCacheOptions
1010
{
11-
12-
/// <summary>
13-
/// If set to true the credential will store tokens in a cache persisted to the machine, protected to the current user, which can be shared by other credentials and processes.
14-
/// </summary>
15-
internal bool EnablePersistentCache { get; set; }
16-
1711
/// <summary>
18-
/// If set to true the credential will fall back to storing tokens in an unencrypted file if no OS level user encryption is available.
12+
/// Specifies the <see cref="TokenCache"/> to be used by the credential.
1913
/// </summary>
20-
internal bool AllowUnencryptedCache { get; set; }
14+
public TokenCache TokenCache { get; set; }
2115

2216
/// <summary>
2317
/// Will include x5c header in client claims when acquiring a token to enable subject name / issuer based authentication for the <see cref="ClientCertificateCredential"/>.
2418
/// </summary>
2519
public bool SendCertificateChain { get; set; }
26-
27-
bool ITokenCacheOptions.EnablePersistentCache => EnablePersistentCache;
28-
29-
bool ITokenCacheOptions.AllowUnencryptedCache => AllowUnencryptedCache;
3020
}
3121
}

sdk/identity/Azure.Identity/src/ClientSecretCredential.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public ClientSecretCredential(string tenantId, string clientId, string clientSec
6060
/// <param name="clientId">The client (application) ID of the service principal</param>
6161
/// <param name="clientSecret">A client secret that was generated for the App Registration used to authenticate the client.</param>
6262
/// <param name="options">Options that allow to configure the management of the requests sent to the Azure Active Directory service.</param>
63-
internal ClientSecretCredential(string tenantId, string clientId, string clientSecret, ClientSecretCredentialOptions options)
63+
public ClientSecretCredential(string tenantId, string clientId, string clientSecret, ClientSecretCredentialOptions options)
6464
: this(tenantId, clientId, clientSecret, options, null, null)
6565
{
6666
}

sdk/identity/Azure.Identity/src/ClientSecretCredentialOptions.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,12 @@ namespace Azure.Identity
66
/// <summary>
77
/// Options used to configure the <see cref="ClientSecretCredential"/>.
88
/// </summary>
9-
internal class ClientSecretCredentialOptions : TokenCredentialOptions, ITokenCacheOptions
9+
public class ClientSecretCredentialOptions : TokenCredentialOptions, ITokenCacheOptions
1010
{
11-
1211
/// <summary>
13-
/// If set to true the credential will store tokens in a persistent cache shared by other credentials.
12+
/// Specifies the <see cref="TokenCache"/> to be used by the credential.
1413
/// </summary>
15-
public bool EnablePersistentCache { get; set; }
14+
public TokenCache TokenCache { get; set; }
1615

17-
/// <summary>
18-
/// If set to true the credential will fall back to storing tokens in an unencrypted file if no OS level user encryption is available.
19-
/// </summary>
20-
public bool AllowUnencryptedCache { get; set; }
2116
}
2217
}

0 commit comments

Comments
 (0)