Skip to content

Commit c81d9ab

Browse files
add token caching doc (#30090)
* add token caching doc * Update sdk/identity/azure-identity/README.md Co-authored-by: Scott Addie <10702007+scottaddie@users.noreply.github.com> * Update sdk/identity/azure-identity/README.md Co-authored-by: Scott Addie <10702007+scottaddie@users.noreply.github.com> * Update sdk/identity/azure-identity/README.md Co-authored-by: Scott Addie <10702007+scottaddie@users.noreply.github.com> * Update sdk/identity/azure-identity/TOKEN_CACHING.md Co-authored-by: Scott Addie <10702007+scottaddie@users.noreply.github.com> * Update sdk/identity/azure-identity/TOKEN_CACHING.md Co-authored-by: Scott Addie <10702007+scottaddie@users.noreply.github.com> * Update sdk/identity/azure-identity/TOKEN_CACHING.md Co-authored-by: Scott Addie <10702007+scottaddie@users.noreply.github.com> * update * update * update * update --------- Co-authored-by: Scott Addie <10702007+scottaddie@users.noreply.github.com>
1 parent 2d34101 commit c81d9ab

3 files changed

Lines changed: 110 additions & 0 deletions

File tree

.vscode/cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
"dont",
175175
"dotenv",
176176
"dowhile",
177+
"DPAPI",
177178
"dpkg",
178179
"dtlk",
179180
"dtlksd",

sdk/identity/azure-identity/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,16 @@ variables:
289289

290290
Configuration is attempted in the above order. For example, if values for a client secret and certificate are both present, the client secret will be used.
291291

292+
## Token caching
293+
294+
Token caching is a feature provided by the Azure Identity library that allows apps to:
295+
- Cache tokens in memory (default) or on disk (opt-in).
296+
- Improve resilience and performance.
297+
- Reduce the number of requests made to Azure AD to obtain access tokens.
298+
299+
The Azure Identity library offers both in-memory and persistent disk caching. For more details, see the [token caching documentation](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/identity/azure-identity/TOKEN_CACHING.md).
300+
301+
292302
## Troubleshooting
293303

294304
See the [troubleshooting guide][troubleshooting_guide] for details on how to diagnose various failure scenarios.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
## Token caching in the Azure Identity client library
2+
3+
*Token caching* is a feature provided by the Azure Identity library that allows apps to:
4+
5+
- Improve their resilience and performance.
6+
- Reduce the number of requests made to Azure Active Directory (Azure AD) to obtain access tokens.
7+
- Reduce the number of times the user is prompted to authenticate.
8+
9+
When an app needs to access a protected Azure resource, it typically needs to obtain an access token from Azure AD. Obtaining that token involves sending a request to Azure AD and may also involve prompting the user. Azure AD then validates the credentials provided in the request and issues an access token.
10+
11+
Token caching, via the Azure Identity library, allows the app to store this access token [in memory](#in-memory-token-caching), where it's accessible to the current process, or [on disk](#persistent-token-caching) where it can be accessed across application or process invocations. The token can then be retrieved quickly and easily the next time the app needs to access the same resource. The app can avoid making another request to Azure AD, which reduces network traffic and improves resilience. Additionally, in scenarios where the app is authenticating users, token caching also avoids prompting the user each time new tokens are requested.
12+
13+
### In-memory token caching
14+
15+
*In-memory token caching* is the default option provided by the Azure Identity library. This caching approach allows apps to store access tokens in memory. With in-memory token caching, the library first determines if a valid access token for the requested resource is already stored in memory. If a valid token is found, it's returned to the app without the need to make another request to Azure AD. If a valid token isn't found, the library will automatically acquire a token by sending a request to Azure AD.
16+
17+
The in-memory token cache provided by the Azure Identity library can be used by multiple threads concurrently.
18+
19+
**Note:** When Azure Identity library credentials are used with Azure service libraries (for example, Azure Blob Storage), the in-memory token caching is active in the `HttpPipeline` layer as well. All `TokenCredential` implementations are supported there, including custom implementations external to the Azure Identity library.
20+
21+
### Persistent token caching
22+
23+
*Persistent disk token caching* is an opt-in feature in the Azure Identity library. The feature allows apps to cache access tokens in an encrypted, persistent storage mechanism. As indicated in the following table, the storage mechanism differs across operating systems.
24+
25+
| Operating system | Storage mechanism |
26+
|------------------|-------------------|
27+
| Linux | Keyring |
28+
| macOS | Keychain |
29+
| Windows | DPAPI |
30+
31+
Users can enable the cache to fall back to storing data in plaintext by setting the `allow_unencrypted_storage argument` to `True` in `TokenCachePersistenceOptions`. Enabling this does not disable encryption, but does allow plaintext storage as a fallback if encryption attempts fail. This is substantially less secure since tokens aren't encrypted to the current user, and anyone with access to the system could potentially access tokens from the cache. As such, enabling this setting is not recommended.
32+
33+
With persistent disk token caching enabled, the library first determines if a valid access token for the requested resource is already stored in the persistent cache. If a valid token is found, it's returned to the app without the need to make another request to Azure AD. Additionally, the tokens are preserved across app runs, which:
34+
35+
- Makes the app more resilient to failures.
36+
- Ensures the app can continue to function during an Azure AD outage or disruption.
37+
- Avoids having to prompt users to authenticate each time the process is restarted.
38+
39+
#### Code sample
40+
41+
The sample showcases how to activate persistence token caching in the credentials offered by the Azure Identity library. You need to specify `TokenCachePersistenceOptions` when creating the credential to activate persistent token caching.
42+
43+
```python
44+
ClientSecretCredential(
45+
"tenant", "client-id", "secret", cache_persistence_options=TokenCachePersistenceOptions()
46+
)
47+
```
48+
49+
#### Persist user authentication record
50+
51+
When authenticating a user via `InteractiveBrowserCredential`, `DeviceCodeCredential`, or `UsernamePasswordCredential`, an `AuthenticationRecord` can be persisted as well. The authentication record is:
52+
53+
- Returned from the `authenticate` API and contains data identifying an authenticated account.
54+
- Needed to identify the appropriate entry in the persisted token cache to silently authenticate on subsequent executions.
55+
56+
There's no sensitive data in the `AuthenticationRecord`, so it can be persisted in a non-protected state. Here's an example of an app storing the `AuthenticationRecord` to the local file system after authenticating the user:
57+
58+
```python
59+
credential = InteractiveBrowserCredential(cache_persistence_options=TokenCachePersistenceOptions())
60+
record = credential.authenticate()
61+
record_json = record.serialize()
62+
# Store the record_json to the local file system
63+
```
64+
65+
#### Silently authenticating a user with AuthenticationRecord and TokenCachePersistenceOptions
66+
67+
Once an app has configured an `InteractiveBrowserCredential`, `DeviceCodeCredential`, or `UsernamePasswordCredential` to persist token data and an `AuthenticationRecord`, it's possible to silently authenticate. This example demonstrates an app setting the `TokenCachePersistenceOptions` and retrieving an `AuthenticationRecord` from the local file system to create an `InteractiveBrowserCredential` capable of silent authentication:
68+
69+
```python
70+
deserialized_record = AuthenticationRecord.deserialize(record_json)
71+
new_credential = InteractiveBrowserCredential(
72+
cache_persistence_options=TokenCachePersistenceOptions(), authentication_record=deserialized_record
73+
)
74+
```
75+
76+
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. 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.
77+
78+
### Credentials supporting token caching
79+
80+
The following table indicates the state of in-memory and persistent caching in each credential type.
81+
82+
**Note:** In-memory caching is activated by default. Persistent token caching needs to be enabled as shown in this [code sample](#code-sample).
83+
84+
| Credential | In-memory token caching | Persistent disk token caching |
85+
|--------------------------------|------------------------------------------------------------------------|-------------------------------|
86+
| `AuthorizationCodeCredential` | Supported | Supported |
87+
| `AzureCliCredential` | Not Supported | Not Supported |
88+
| `AzureDeveloperCliCredential` | Not Supported | Not Supported |
89+
| `AzurePowershellCredential` | Not Supported | Not Supported |
90+
| `ClientAssertionCredential` | Supported | Not Supported |
91+
| `ClientCertificateCredential` | Supported | Supported |
92+
| `ClientSecretCredential` | Supported | Supported |
93+
| `DefaultAzureCredential` | Supported if the target credential in the credential chain supports it | Not Supported |
94+
| `DeviceCodeCredential` | Supported | Supported |
95+
| `InteractiveBrowserCredential` | Supported | Supported |
96+
| `ManagedIdentityCredential` | Supported | Not Supported |
97+
| `OnBehalfOfCredential` | Supported | Supported |
98+
| `UsernamePasswordCredential` | Supported | Supported |
99+
| `WorkloadIdentityCredential` | Supported | Not Supported |

0 commit comments

Comments
 (0)