Skip to content

Commit 675631c

Browse files
christothesschaabs
andauthored
TokenCache becomes internal and only exposes TokenCachePersistenceOptions (Azure#19240)
* TokenCache becomes internal and only exposes TokenCachePersistenceOptions Co-authored-by: Scott Schaab <sschaab@microsoft.com>
1 parent 867e419 commit 675631c

29 files changed

+656
-810
lines changed

sdk/identity/Azure.Identity/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
### New Features
3333

3434
- Redesigned Application Authentication APIs
35-
- Adds `TokenCache` and `PersistentTokenCache` classes to give more user control over how the tokens are cached and how the cache is persisted.
35+
- Adds `TokenCache` and `TokenCache` classes to give more user control over how the tokens are cached and how the cache is persisted.
3636
- Adds `TokenCache` property to options for credentials supporting token cache configuration.
3737

3838
## 1.3.0 (2020-11-12)

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

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public partial class ClientCertificateCredentialOptions : Azure.Identity.TokenCr
6969
{
7070
public ClientCertificateCredentialOptions() { }
7171
public bool SendCertificateChain { get { throw null; } set { } }
72-
public Azure.Identity.TokenCache TokenCache { get { throw null; } set { } }
72+
public Azure.Identity.TokenCachePersistenceOptions TokenCachePersistenceOptions { get { throw null; } set { } }
7373
}
7474
public partial class ClientSecretCredential : Azure.Core.TokenCredential
7575
{
@@ -83,7 +83,7 @@ public ClientSecretCredential(string tenantId, string clientId, string clientSec
8383
public partial class ClientSecretCredentialOptions : Azure.Identity.TokenCredentialOptions
8484
{
8585
public ClientSecretCredentialOptions() { }
86-
public Azure.Identity.TokenCache TokenCache { get { throw null; } set { } }
86+
public Azure.Identity.TokenCachePersistenceOptions TokenCachePersistenceOptions { get { throw null; } set { } }
8787
}
8888
public partial class CredentialUnavailableException : Azure.Identity.AuthenticationFailedException
8989
{
@@ -138,7 +138,7 @@ public DeviceCodeCredentialOptions() { }
138138
public System.Func<Azure.Identity.DeviceCodeInfo, System.Threading.CancellationToken, System.Threading.Tasks.Task> DeviceCodeCallback { get { throw null; } set { } }
139139
public bool DisableAutomaticAuthentication { get { throw null; } set { } }
140140
public string TenantId { get { throw null; } set { } }
141-
public Azure.Identity.TokenCache TokenCache { get { throw null; } set { } }
141+
public Azure.Identity.TokenCachePersistenceOptions TokenCachePersistenceOptions { get { throw null; } set { } }
142142
}
143143
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
144144
public partial struct DeviceCodeInfo
@@ -188,7 +188,7 @@ public InteractiveBrowserCredentialOptions() { }
188188
public bool DisableAutomaticAuthentication { get { throw null; } set { } }
189189
public System.Uri RedirectUri { get { throw null; } set { } }
190190
public string TenantId { get { throw null; } set { } }
191-
public Azure.Identity.TokenCache TokenCache { get { throw null; } set { } }
191+
public Azure.Identity.TokenCachePersistenceOptions TokenCachePersistenceOptions { get { throw null; } set { } }
192192
}
193193
public partial class ManagedIdentityCredential : Azure.Core.TokenCredential
194194
{
@@ -197,16 +197,6 @@ public ManagedIdentityCredential(string clientId = null, Azure.Identity.TokenCre
197197
public override Azure.Core.AccessToken GetToken(Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
198198
public override System.Threading.Tasks.ValueTask<Azure.Core.AccessToken> GetTokenAsync(Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
199199
}
200-
public partial class PersistentTokenCache : Azure.Identity.TokenCache
201-
{
202-
public PersistentTokenCache(Azure.Identity.PersistentTokenCacheOptions options = null) { }
203-
}
204-
public partial class PersistentTokenCacheOptions
205-
{
206-
public PersistentTokenCacheOptions() { }
207-
public bool AllowUnencryptedStorage { get { throw null; } set { } }
208-
public string Name { get { throw null; } set { } }
209-
}
210200
public partial class SharedTokenCacheCredential : Azure.Core.TokenCredential
211201
{
212202
public SharedTokenCacheCredential() { }
@@ -219,36 +209,36 @@ public SharedTokenCacheCredential(string username, Azure.Identity.TokenCredentia
219209
public partial class SharedTokenCacheCredentialOptions : Azure.Identity.TokenCredentialOptions
220210
{
221211
public SharedTokenCacheCredentialOptions() { }
222-
public SharedTokenCacheCredentialOptions(Azure.Identity.TokenCache tokenCache) { }
212+
public SharedTokenCacheCredentialOptions(Azure.Identity.TokenCachePersistenceOptions tokenCacheOptions) { }
223213
public Azure.Identity.AuthenticationRecord AuthenticationRecord { get { throw null; } set { } }
224214
public string ClientId { get { throw null; } set { } }
225215
public bool EnableGuestTenantAuthentication { get { throw null; } set { } }
226216
public string TenantId { get { throw null; } set { } }
227-
public Azure.Identity.TokenCache TokenCache { get { throw null; } }
217+
public Azure.Identity.TokenCachePersistenceOptions TokenCachePersistenceOptions { get { throw null; } }
228218
public string Username { get { throw null; } set { } }
229219
}
230-
public partial class TokenCache
220+
public partial class TokenCachePersistenceOptions
231221
{
232-
public TokenCache() { }
233-
public event System.Func<Azure.Identity.TokenCacheUpdatedArgs, System.Threading.Tasks.Task> Updated { add { } remove { } }
234-
}
235-
public static partial class TokenCacheSerializer
236-
{
237-
public static Azure.Identity.TokenCache Deserialize(System.IO.Stream stream, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
238-
public static System.Threading.Tasks.Task<Azure.Identity.TokenCache> DeserializeAsync(System.IO.Stream stream, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
239-
public static void Serialize(Azure.Identity.TokenCache tokenCache, System.IO.Stream stream, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { }
240-
public static System.Threading.Tasks.Task SerializeAsync(Azure.Identity.TokenCache tokenCache, System.IO.Stream stream, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
222+
public TokenCachePersistenceOptions() { }
223+
public string Name { get { throw null; } set { } }
224+
public bool UnsafeAllowUnencryptedStorage { get { throw null; } set { } }
241225
}
242226
public partial class TokenCacheUpdatedArgs
243227
{
244228
internal TokenCacheUpdatedArgs() { }
245-
public Azure.Identity.TokenCache Cache { get { throw null; } }
229+
public System.ReadOnlyMemory<byte> UnsafeCacheData { get { throw null; } }
246230
}
247231
public partial class TokenCredentialOptions : Azure.Core.ClientOptions
248232
{
249233
public TokenCredentialOptions() { }
250234
public System.Uri AuthorityHost { get { throw null; } set { } }
251235
}
236+
public abstract partial class UnsafeTokenCacheOptions : Azure.Identity.TokenCachePersistenceOptions
237+
{
238+
protected UnsafeTokenCacheOptions() { }
239+
protected internal abstract System.Threading.Tasks.Task<System.ReadOnlyMemory<byte>> RefreshCacheAsync();
240+
protected internal abstract System.Threading.Tasks.Task TokenCacheUpdatedAsync(Azure.Identity.TokenCacheUpdatedArgs tokenCacheUpdatedArgs);
241+
}
252242
public partial class UsernamePasswordCredential : Azure.Core.TokenCredential
253243
{
254244
protected UsernamePasswordCredential() { }
@@ -265,7 +255,7 @@ public UsernamePasswordCredential(string username, string password, string tenan
265255
public partial class UsernamePasswordCredentialOptions : Azure.Identity.TokenCredentialOptions
266256
{
267257
public UsernamePasswordCredentialOptions() { }
268-
public Azure.Identity.TokenCache TokenCache { get { throw null; } set { } }
258+
public Azure.Identity.TokenCachePersistenceOptions TokenCachePersistenceOptions { get { throw null; } set { } }
269259
}
270260
public partial class VisualStudioCodeCredential : Azure.Core.TokenCredential
271261
{

sdk/identity/Azure.Identity/samples/ClientSideUserAuthentication.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ In many cases applications require tight control over user interaction. In these
4040

4141
```C# Snippet:Identity_ClientSideUserAuthentication_DisableAutomaticAuthentication
4242
var credential = new InteractiveBrowserCredential(
43-
new InteractiveBrowserCredentialOptions {
43+
new InteractiveBrowserCredentialOptions
44+
{
4445
DisableAutomaticAuthentication = true
4546
});
4647

@@ -72,24 +73,31 @@ catch (AuthenticationRequiredException e)
7273

7374
## Persisting user authentication data
7475

75-
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`.
76+
Quite often applications desire the ability to be run multiple times without having to re-authenticate the user on each execution.
77+
This requires that data from credentials be persisted outside of the application memory so that it can authenticate silently on subsequent executions.
78+
Applications can persist this data using `TokenPersistenceOptions` when constructing the credential, and persisting the `AuthenticationRecord` returned from `Authenticate`.
7679

77-
### Persisting the TokenCache
80+
### Persisting the token cache
7881

79-
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.
82+
The credential handles persisting all the data needed to silently authenticate one or many accounts.
83+
It manages sensitive data such as refresh tokens and access tokens which must be protected to prevent compromising the accounts related to them.
84+
By default, the `Azure.Identity` library will protect and cache sensitive token data using available platform data protection.
8085

81-
To use the `PersistentTokenCache` to persist the cache of any credential simply set the `TokenCache` option.
86+
To configure a credential, such as the `InteractiveBrowserCredential`, to persist token data, simply set the `TokenCachePersistenceOptions` option.
8287

8388
```C# Snippet:Identity_ClientSideUserAuthentication_Persist_TokenCache
8489
var credential = new InteractiveBrowserCredential(
85-
new InteractiveBrowserCredentialOptions {
86-
TokenCache = new PersistentTokenCache()
90+
new InteractiveBrowserCredentialOptions
91+
{
92+
TokenCachePersistenceOptions = new TokenCachePersistenceOptions()
8793
});
8894
```
8995

9096
### Persisting the AuthenticationRecord
9197

92-
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.
98+
The `AuthenticationRecord` which is returned from the `Authenticate` and `AuthenticateAsync`, contains data identifying an authenticated account.
99+
It is needed to identify the appropriate entry in the persisted token cache to silently authenticate on subsequent executions.
100+
There is no sensitive data in the `AuthenticationRecord` so it can be persisted in a non-protected state.
93101

94102
Here is an example of an application storing the `AuthenticationRecord` to the local file system after authenticating the user.
95103

@@ -106,9 +114,10 @@ using (var authRecordStream = new FileStream(AUTH_RECORD_PATH, FileMode.Create,
106114
}
107115
```
108116

109-
### Silent authentication with AuthenticationRecord and PersistentTokenCache
117+
### Silent authentication with AuthenticationRecord and TokenCachePersistenceOptions
110118

111-
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.
119+
Once an application has configured a credential to persist token data and an `AuthenticationRecord`, it is possible to silently authenticate.
120+
This example demonstrates an application setting the `TokenCachePersistenceOptions` and retrieving an `AuthenticationRecord` from the local file system to create an `InteractiveBrowserCredential` capable of silent authentication.
112121

113122
```C# Snippet:Identity_ClientSideUserAuthentication_Persist_SilentAuth
114123
AuthenticationRecord authRecord;
@@ -119,10 +128,12 @@ using (var authRecordStream = new FileStream(AUTH_RECORD_PATH, FileMode.Open, Fi
119128
}
120129

121130
var credential = new InteractiveBrowserCredential(
122-
new InteractiveBrowserCredentialOptions {
123-
TokenCache = new PersistentTokenCache(),
131+
new InteractiveBrowserCredentialOptions
132+
{
133+
TokenCachePersistenceOptions = new TokenCachePersistenceOptions(),
124134
AuthenticationRecord = authRecord
125135
});
126136
```
127137

128-
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.
138+
The credential created in this example will silently authenticate given that a valid token for corresponding to the `AuthenticationRecord` still exists in the persisted token data.
139+
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.

0 commit comments

Comments
 (0)