o
    i$                     @   s   d dl mZmZmZmZ d dlmZmZmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZmZ edddZG dd dZG dd deZdS )    )AnyOptionalTypeVarcast)AccessTokenTokenRequestOptionsAccessTokenInfoSupportsTokenInfoTokenCredential   )SilentAuthenticationCredential   )CredentialUnavailableError)DEVELOPER_SIGN_ON_CLIENT_ID)	AadClientAadClientBase
within_dac)log_get_token)NO_TOKENSharedTokenCacheBaseT_SharedTokenCacheCredential)boundc                   @   s   e Zd ZdZddee deddfddZddd	Zd
eddfddZ	dddZ
edddddedee dee dededefddZedddedee defddZedefddZdS ) SharedTokenCacheCredentiala  Authenticates using tokens in the local cache shared between Microsoft applications.

    :param str username: Username (typically an email address) of the user to authenticate as. This is used when the
        local cache contains tokens for multiple identities.

    :keyword str authority: Authority of a Microsoft Entra endpoint, for example 'login.microsoftonline.com',
        the authority for Azure Public Cloud (which is the default). :class:`~azure.identity.AzureAuthorityHosts`
        defines authorities for other clouds.
    :keyword str tenant_id: a Microsoft Entra tenant ID. Used to select an account when the cache contains
        tokens for multiple identities.
    :keyword AuthenticationRecord authentication_record: an authentication record returned by a user credential such as
        :class:`DeviceCodeCredential` or :class:`InteractiveBrowserCredential`
    :keyword cache_persistence_options: configuration for persistent token caching. If not provided, the credential
        will use the persistent cache shared by Microsoft development applications
    :paramtype cache_persistence_options: ~azure.identity.TokenCachePersistenceOptions
    Nusernamekwargsreturnc                 K   s4   d|v rt di || _d S tdd|i|| _d S )NZauthentication_recordr    )r   _credentialr   )selfr   r   r   r   _/home/app/Keep/.python/lib/python3.10/site-packages/azure/identity/_credentials/shared_cache.py__init__%   s   z#SharedTokenCacheCredential.__init__c                 C   s   | j   | S N)r   	__enter__r   r   r   r    r#   +   s   
z$SharedTokenCacheCredential.__enter__argsc                 G   s   | j j|  d S r"   )r   __exit__r   r%   r   r   r    r&   /   s   z#SharedTokenCacheCredential.__exit__c                 C   s   |    dS )z)Close the credential's transport session.Nr&   r$   r   r   r    close2   s   z SharedTokenCacheCredential.closeFclaims	tenant_id
enable_caescopesr+   r,   r-   c                O   s    t t| jj||||d|S )a
  Get an access token for `scopes` from the shared cache.

        If no access token is cached, attempt to acquire one using a cached refresh token.

        This method is called automatically by Azure SDK clients.

        :param str scopes: desired scopes for the access token. This method requires at least one scope.
            For more information about scopes, see
            https://learn.microsoft.com/entra/identity-platform/scopes-oidc.
        :keyword str claims: additional claims required in the token, such as those returned in a resource provider's
            claims challenge following an authorization failure
        :keyword str tenant_id: not used by this credential; any value provided will be ignored.
        :keyword bool enable_cae: indicates whether to enable Continuous Access Evaluation (CAE) for the requested
            token. Defaults to False.

        :return: An access token with the desired scopes.
        :rtype: ~azure.core.credentials.AccessToken
        :raises ~azure.identity.CredentialUnavailableError: the cache is unavailable or contains insufficient user
            information
        :raises ~azure.core.exceptions.ClientAuthenticationError: authentication failed. The error's ``message``
            attribute gives a reason.
        r*   )r   r
   r   	get_token)r   r+   r,   r-   r.   r   r   r   r    r/   6   s
   z$SharedTokenCacheCredential.get_tokenoptionsr1   c                G   s   t t| jj|d|iS )a  Request an access token for `scopes`.

        If no access token is cached, attempt to acquire one using a cached refresh token.

        This is an alternative to `get_token` to enable certain scenarios that require additional properties
        on the token. This method is called automatically by Azure SDK clients.

        :param str scopes: desired scope for the access token. This method requires at least one scope.
            For more information about scopes, see https://learn.microsoft.com/entra/identity-platform/scopes-oidc.
        :keyword options: A dictionary of options for the token request. Unknown options will be ignored. Optional.
        :paramtype options: ~azure.core.credentials.TokenRequestOptions

        :rtype: ~azure.core.credentials.AccessTokenInfo
        :return: An AccessTokenInfo instance containing information about the token.
        :raises ~azure.identity.CredentialUnavailableError: the cache is unavailable or contains insufficient user
            information.
        :raises ~azure.core.exceptions.ClientAuthenticationError: authentication failed. The error's ``message``
            attribute gives a reason.
        r1   )r   r	   r   get_token_infor   r1   r.   r   r   r    r2   Y   s   z)SharedTokenCacheCredential.get_token_infoc                   C   s   t  S )zWhether the shared token cache is supported on the current platform.

        :return: True if the shared token cache is supported on the current platform, otherwise False.
        :rtype: bool
        )r   	supportedr   r   r   r    r4   p   s   z$SharedTokenCacheCredential.supportedr"   )r   r   r   N)__name__
__module____qualname____doc__r   strr   r!   r#   r&   r)   r   boolr   r/   r   r   r2   staticmethodr4   r   r   r   r    r      s6    

""r   c                   @   s   e Zd ZdZdedefddZdd Zdd	d
Zdddddede	e de	e de
dedefddZdddede	e defddZddddede	e dededef
ddZdedefddZdS ) r   zQThe original SharedTokenCacheCredential, which doesn't use msal.ClientApplicationr   r   c                 C   s   | j r| j   | S r"   )_clientr#   r$   r   r   r    r#   }   s   
z%_SharedTokenCacheCredential.__enter__c                 G   s   | j r| j j|  d S d S r"   )r=   r&   r'   r   r   r    r&      s   z$_SharedTokenCacheCredential.__exit__Nc                 C   s   |    d S r"   r(   r$   r   r   r    r)      s   z!_SharedTokenCacheCredential.closeFr*   r.   r+   r,   r-   r   c                O   sH   i }|r||d< |r||d< ||d< | j ||dd|}t|j|jS )Nr+   r,   r-   r/   r1   base_method_name)_get_token_baser   tokenZ
expires_on)r   r+   r,   r-   r.   r   r1   Z
token_infor   r   r    r/      s   z%_SharedTokenCacheCredential.get_tokenr0   r1   c                G   s   | j ||ddS )Nr2   r>   )r@   r3   r   r   r    r2         z*_SharedTokenCacheCredential.get_token_infor2   r>   r?   c                O   sD  |s
t d| d| js|   |pi }|d}|d}|dd}|r*| jn| j}|s<| j|d}|s<tdd	| j| j	| j
|d}	|sT| j||	|d}
|
rT|
S | j|	|dD ];}ztt| jj||f|||d
|}
|
W   S  ty } zt rtt|dt|t|dd d| d }~ww tt|	dd	)N'z' requires at least one scoper+   r,   r-   F)is_caezShared token cache unavailable)messager*   rE   response)rE   rF   r   )
ValueErrorZ_client_initializedZ_initialize_clientgetZ
_cae_cache_cacheZ_initialize_cacher   Z_get_accountZ	_usernameZ
_tenant_idZ_get_cached_access_tokenZ_get_refresh_tokensr   r   r=   Zobtain_token_by_refresh_token	Exceptionr   getattrr:   r   format)r   r1   r?   r.   r   r+   r,   rD   Ztoken_cacheaccountrA   Zrefresh_tokener   r   r    r@      sN   



z+_SharedTokenCacheCredential._get_token_basec                 K   s   t ddti|S )NZ	client_idr   )r   r   )r   r   r   r   r    _get_auth_client   rB   z,_SharedTokenCacheCredential._get_auth_clientr5   )r6   r7   r8   r9   r   r#   r&   r)   r:   r   r;   r   r   r/   r   r   r2   r@   r   rO   r   r   r   r    r   z   sH    

 
3N)typingr   r   r   r   Zazure.core.credentialsr   r   r   r	   r
   Zsilentr    r   
_constantsr   	_internalr   r   r   Z_internal.decoratorsr   Z_internal.shared_token_cacher   r   r   r   r   r   r   r   r    <module>   s   g