Repository navigation
SharedTokenCacheCredential with Custom TokenCachePersistenceOptions Does Not Persist #26982
Copy link
Copy link
Closed
Labels
Azure.IdentityClientThis issue points to a problem in the data-plane of the library.This issue points to a problem in the data-plane of the library.bugThis issue requires a change to an existing behavior in the product in order to be resolved.This issue requires a change to an existing behavior in the product in order to be resolved.customer-reportedIssues that are reported by GitHub users external to the Azure organization.Issues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as thatThe issue doesn't require a change to the product in order to be resolved. Most issues start as that
Description
I am not getting the SharedTokenCacheCredential to work as expected. Considering the following script:
from azure.identity import (
ChainedTokenCredential,
InteractiveBrowserCredential,
TokenCachePersistenceOptions,
)
TENANT_ID = "..."
cache_options = TokenCachePersistenceOptions(name="my.cache")
credential = ChainedTokenCredential(
SharedTokenCacheCredential(
tenant_id=TENANT_ID,
cache_persistence_options=cache_options
),
InteractiveBrowserCredential(
tenant_id=TENANT_ID,
cache_persistence_options=cache_options,
)
)
credential.get_token(...)
The first time, it runs as expected. At credential.get_token(...), a browser tab opens and I am prompted to sign in. On successful sign in, it generates the token, and the script returns.
On all subsequent runs, it does not behave as expected.
- Expected: the token should be in the persistent cache, and thus, the
SharedTokenCacheCredentialshould be used, and I should not be prompted to sign in again. - Actual: it opens another browser tab and prompts me to sign in again, and prints the following to the terminal:
SharedTokenCacheCredential authentication unavailable. No accounts were found in the cache.
The following workaround yields the expected behavior, but uses a "private" function from the azure.identity package.
from azure.identity import (
ChainedTokenCredential,
InteractiveBrowserCredential,
TokenCachePersistenceOptions,
)
+from azure.identity._persistent_cache import _load_persistent_cache
TENANT_ID = "..."
cache_options = TokenCachePersistenceOptions(name="my.cache")
credential = ChainedTokenCredential(
SharedTokenCacheCredential(
tenant_id=TENANT_ID,
cache_persistence_options=cache_options,
+ _cache=_load_persistent_cache(cache_options),
),
InteractiveBrowserCredential(
tenant_id=TENANT_ID,
cache_persistence_options=cache_options,
)
)
credential.get_token(...)My question is: is there another solution that uses the "public" api?
My setup:
- OS: macOS Monterey (12.1)
- python: 3.7.15
- azure-identity: 1.11.0
Reactions are currently unavailable
Metadata
Metadata
Labels
Azure.IdentityClientThis issue points to a problem in the data-plane of the library.This issue points to a problem in the data-plane of the library.bugThis issue requires a change to an existing behavior in the product in order to be resolved.This issue requires a change to an existing behavior in the product in order to be resolved.customer-reportedIssues that are reported by GitHub users external to the Azure organization.Issues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as thatThe issue doesn't require a change to the product in order to be resolved. Most issues start as that
Activity
[-]`SharedTokenCacheCredential` with Custom `TokenCachePersistenceOptions` Does Not Persist Between Sessions[/-][+]`SharedTokenCacheCredential` with Custom `TokenCachePersistenceOptions` Does Not Persist[/+]pvaneck commentedon Oct 24, 2022
Hey, Steven Lewis (@poocat). Thanks for reporting the issue.
Can you try setting the cache name to "msal.cache" (the default cache name) or removing
cache_persistence_optionsfrom each of the credentials? I believe the custom cache name may not be propagating correctly, andSharedTokenCacheCredentialkeeps looking for "msal.cache" (which doesn't exist) instead of "my.cache".poocat commentedon Oct 24, 2022
Paul Van Eck (@pvaneck) thank you very much for your reply.
Switching the name to
"msal.cache"worked!Removing
cache_persistence_optionsdid not.poocat commentedon Oct 25, 2022
Yes, it has been addressed. Thank you!
In the meantime, until the bug gets fixed in a later release, it might be best to change the documentation so that it does not urge you in an enormous font to "specify a cache name to isolate the cache from other applications".
pvaneck commentedon Oct 25, 2022
Hah, yea, the formatting is messed up on that doc page and will be fixed in the next release. Meanwhile, I opened #27030 which should fix the issue here.
[Identity] Fix issue with cache option usage (#27030)
Get main (#27100)