o
    iq$                     @   s   d dl Z d dlZd dlmZmZmZ d dlmZmZ d dl	m
Z
mZ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 ddlmZ ddlmZmZ ddlmZ G dd dZdS )    N)DictOptionalAny)PublicClientApplication
TokenCache)AccessTokenAccessTokenInfoTokenRequestOptions)ClientAuthenticationError   )CredentialUnavailableError)resolve_tenantvalidate_tenant_id
within_dac)wrap_exceptions)
MsalClient)NO_TOKEN)_load_persistent_cacheTokenCachePersistenceOptions)AuthenticationRecordc                   @   s:  e Zd ZdZdddedee ddfddZd,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.d edee fd!d"Zdefd#d$Zedededefd%d&Zdeeef fd'd(Zd)eeef ddfd*d+ZdS )/SilentAuthenticationCredentialau  Internal class for authenticating from the default shared cache given an AuthenticationRecord.

    :param authentication_record: an AuthenticationRecord from which to authenticate
    :type authentication_record: ~azure.identity.AuthenticationRecord
    :keyword str tenant_id: tenant ID of the application the credential is authenticating for. Defaults to the tenant
    N)	tenant_idauthentication_recordr   returnc                K   s   || _ |p| j j| _t| j |dd | _|dd | _| js#| jr'd| _nd| _|dd | _i | _	i | _
|dg | _tdi || _d S )N_cache
_cae_cacheTFZcache_persistence_optionsadditionally_allowed_tenants )_auth_recordr   
_tenant_idr   popr   r   _custom_cache_cache_persistence_options_client_applications_cae_client_applications_additionally_allowed_tenantsr   _client)selfr   r   kwargsr   r   Y/home/app/Keep/.python/lib/python3.10/site-packages/azure/identity/_credentials/silent.py__init__   s   
z'SilentAuthenticationCredential.__init__c                 C   s   | j   | S N)r&   	__enter__r'   r   r   r)   r,   6   s   
z(SilentAuthenticationCredential.__enter__c                 G   s   | j j|  d S r+   )r&   __exit__)r'   argsr   r   r)   r.   :   s   z'SilentAuthenticationCredential.__exit__c                 C   s   |    d S r+   )r.   r-   r   r   r)   close=   s   z$SilentAuthenticationCredential.closeFclaimsr   
enable_caescopesr2   r3   r(   c                O   sH   i }|r||d< |r||d< ||d< | j ||dd|}t|j|jS )Nr2   r   r3   	get_tokenoptionsbase_method_name)_get_token_baser   tokenZ
expires_on)r'   r2   r   r3   r4   r(   r7   Z
token_infor   r   r)   r5   @   s   z(SilentAuthenticationCredential.get_token)r7   r7   c                G   s   | j ||ddS )Nget_token_infor6   )r9   )r'   r7   r4   r   r   r)   r;   R   s   z-SilentAuthenticationCredential.get_token_infor;   r6   r8   c          	      O   s   |s
t d| d|pi }|d}|d}|dd}|r#| jn| j}|s>| j|d}|s>t r9tdd	tdd	| j||||d
|S )N'z' requires at least one scoper2   r   r3   F)is_caezShared token cache unavailablemessager1   )	
ValueErrorgetr   r   _initialize_cacher   r   r
   _acquire_token_silent)	r'   r7   r8   r4   r(   r2   r   r3   token_cacher   r   r)   r9   U   s   



z.SilentAuthenticationCredential._get_token_baser=   c                 C   s   | j ptdd}t dvrtdd| js+|s+zt||| _W n
 ty*   Y d S w | jsC|rCzt||| _W n
 tyB   Y d S w |rH| jS | jS )NT)Zallow_unencrypted_storage>   LinuxDarwinWindowsz5Shared token cache is not supported on this platform.r>   )	r"   r   platformsystemr   r   r   	Exceptionr   )r'   r=   Zcache_optionsr   r   r)   rB   s   s    


z0SilentAuthenticationCredential._initialize_cachec                 K   s|   t | jfd| ji|}| j}d }| j}|dr"| j}dg}| j}||vr:t| j	j
d| j	j||| j|d||< || S )Nr   r3   ZCP1zhttps://{}/{})	client_id	authorityrD   http_clientZclient_capabilities)r   r   r%   r#   r   rA   r$   r   r   r   rK   formatrL   r&   )r'   r(   r   Zclient_applications_mapZcapabilitiesrD   r   r   r)   _get_client_application   s.   

z6SilentAuthenticationCredential._get_client_applicationc                 O   s  d}| j di |}|j| jjd}|std|D ]K}|d| jjkr&qtt }|j	t
|||dd}|red|v red|v red	|v rMt|d	 nd}t|d |t|d  |d
d|d  S q|rd}	|dps|d}
|
r}|	d|
7 }	t|	dtt| jjd)zSilently acquire a token from MSAL.

        :param str scopes: desired scopes for the access token
        :return: an access token
        :rtype: ~azure.core.credentials.AccessToken
        N)usernamezFThe cache contains no account matching the given AuthenticationRecord.home_account_idr2   )accountZclaims_challengeZaccess_tokenZ
expires_in
refresh_on
token_typeZBearer)rT   rS   zToken acquisition failedZerror_descriptionerrorz: {}r>   r   )rO   Zget_accountsr   rP   r   rA   rQ   inttimeZacquire_token_silent_with_errorlistr   rN   r
   r   )r'   r4   r(   resultZclient_applicationZaccounts_for_userrR   nowrS   r?   detailsr   r   r)   rC      s8   	


z4SilentAuthenticationCredential._acquire_token_silentc                 C   s    | j  }| js|d= |d= |S )Nr   r   )__dict__copyr!   r'   stater   r   r)   __getstate__   s
   
z+SilentAuthenticationCredential.__getstate__r_   c                 C   s&   | j | | jsd | _d | _d S d S r+   )r\   updater!   r   r   r^   r   r   r)   __setstate__   s
   
z+SilentAuthenticationCredential.__setstate__)r   r   )r   N)F)__name__
__module____qualname____doc__r   r   strr*   r,   r.   r0   boolr   r   r5   r	   r   r;   r9   r   rB   rO   r   rC   r   r`   rb   r   r   r   r)   r      sb    



 
-r   ) rH   rW   typingr   r   r   Zmsalr   r   Zazure.core.credentialsr   r   r	   Zazure.core.exceptionsr
    r   	_internalr   r   r   Z_internal.decoratorsr   Z_internal.msal_clientr   Z_internal.shared_token_cacher   Z_persistent_cacher   r   r   r   r   r   r   r)   <module>   s   