Skip to content

SharedTokenCacheCredential with Custom TokenCachePersistenceOptions Does Not Persist #26982

Closed

Description

@poocat

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 SharedTokenCacheCredential should 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

Activity

  1. ghost added
    customer-reportedIssues that are reported by GitHub users external to the Azure organization.
    on Oct 21, 2022
  2. ghost added
    questionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that
    on Oct 21, 2022
  3. added
    needs-triageWorkflow: This is a new issue that needs to be triaged to the appropriate team.
    on Oct 21, 2022
  4. ghost removed
    needs-triageWorkflow: This is a new issue that needs to be triaged to the appropriate team.
    on Oct 21, 2022
  5. changed the title [-]`SharedTokenCacheCredential` with Custom `TokenCachePersistenceOptions` Does Not Persist Between Sessions[/-] [+]`SharedTokenCacheCredential` with Custom `TokenCachePersistenceOptions` Does Not Persist[/+] on Oct 21, 2022
  6. pvaneck commented on Oct 24, 2022

    @pvaneck
    Member

    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_options from each of the credentials? I believe the custom cache name may not be propagating correctly, and SharedTokenCacheCredential keeps looking for "msal.cache" (which doesn't exist) instead of "my.cache".

  7. added
    bugThis issue requires a change to an existing behavior in the product in order to be resolved.
    on Oct 24, 2022
  8. ghost added
    needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK team
    on Oct 24, 2022
  9. poocat commented on Oct 24, 2022

    @poocat
    Author

    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_options from each of the credentials? I believe the custom cache name may not be propagating correctly, and SharedTokenCacheCredential keeps looking for "msal.cache" (which doesn't exist) instead of "my.cache".

    Paul Van Eck (@pvaneck) thank you very much for your reply.

    Switching the name to "msal.cache" worked!

    Removing cache_persistence_options did not.

  10. ghost removed
    needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK team
    on Oct 24, 2022
  11. poocat commented on Oct 25, 2022

    @poocat
    Author

    Hi Steven Lewis (@poocat). Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text “/unresolve” to remove the “issue-addressed” label and continue the conversation.

    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".

  12. ghost removed
    issue-addressedWorkflow: The Azure SDK team believes it to be addressed and ready to close.
    on Oct 25, 2022
  13. ghost added
    needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK team
    on Oct 25, 2022
  14. pvaneck commented on Oct 25, 2022

    @pvaneck
    Member

    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.

  15. added a commit that references this issue on Oct 27, 2022
  16. locked and limited conversation to collaborators on Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Azure.IdentityClientThis 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.customer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: 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 that

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions