
    jq$                         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      Zy)    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                   8   e Zd ZdZdddedee   ddfdZd dZd	 Z	d!d
Z
dddddedee   dee   dededefdZdddedee   defdZddddedee   dededef
dZd"dedee   fdZdefdZedededefd       Zdeeef   fdZdeeef   ddfdZy)#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                   || _         |xs | j                   j                  | _        t        | j                         |j	                  dd       | _        |j	                  dd       | _        | j
                  s| j                  rd| _        nd| _        |j	                  dd       | _        i | _	        i | _
        |j	                  dg       | _        t        di || _        y )N_cache
_cae_cacheTF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   kwargss       c/root/aria/tools/markitdown-venv/lib/python3.12/site-packages/azure/identity/_credentials/silent.py__init__z'SilentAuthenticationCredential.__init__   s     2 $Bt'8'8'B'B4??+jj40 **\48;;$//!%D!&D*0**5PRV*W'HJ!LN%-3ZZ8VXZ-[*!+F+    c                 :    | j                   j                          | S N)r*   	__enter__r+   s    r-   r2   z(SilentAuthenticationCredential.__enter__6   s     r/   c                 6     | j                   j                  |  y r1   )r*   __exit__)r+   argss     r-   r5   z'SilentAuthenticationCredential.__exit__:   s    t$r/   c                 $    | j                          y r1   )r5   r3   s    r-   closez$SilentAuthenticationCredential.close=   s    r/   Fclaimsr   
enable_caescopesr:   r;   r,   c                    i }|r||d<   |r||d<   ||d<    | j                   ||dd|}t        |j                  |j                        S )Nr:   r   r;   	get_tokenoptionsbase_method_name)_get_token_baser   token
expires_on)r+   r:   r   r;   r<   r,   r@   
token_infos           r-   r>   z(SilentAuthenticationCredential.get_token@   sc     (* &GH#,GK  *)T))67U`kdjk
:++Z-B-BCCr/   )r@   r@   c                (     | j                   ||ddS )Nget_token_infor?   )rB   )r+   r@   r<   s      r-   rG   z-SilentAuthenticationCredential.get_token_infoR   s    #t##VWO_``r/   rG   r?   rA   c                |   |st        d| d      |xs i }|j                  d      }|j                  d      }|j                  dd      }|r| j                  n| j                  }|s@| j	                  |      }|s,t        j                         rt        d	      t        d	       | j                  ||||d
|S )N'z' requires at least one scoper:   r   r;   F)is_caezShared token cache unavailablemessager9   )	
ValueErrorgetr   r   _initialize_cacher   r   r   _acquire_token_silent)	r+   r@   rA   r<   r,   r:   r   r;   token_caches	            r-   rB   z.SilentAuthenticationCredential._get_token_baseU   s     q!1 22OPQQ-RX&KK,	[[u5
)3doo 00
0CK >>#4=]^^/8XYY)t))6&Iblwpvwwr/   rJ   c                 j   | j                   xs t        d      }t        j                         dvrt	        d      | j
                  s|s	 t        ||      | _        | j                  s|r	 t        ||      | _        |r| j                  S | j
                  S # t        $ r Y y w xY w# t        $ r Y y w xY w)NT)allow_unencrypted_storage>   LinuxDarwinWindowsz5Shared token cache is not supported on this platform.rK   )	r&   r   platformsystemr   r   r   	Exceptionr   )r+   rJ   cache_optionss      r-   rO   z0SilentAuthenticationCredential._initialize_caches   s     77w;Wrv;w??$BB,5lmm{{64]FK 6"8"O #)t9dkk9    s$   B ,B& 	B#"B#&	B21B2c                    t        | j                  fd| j                  i|}| j                  }d }| j                  }|j                  d      r| j                  }dg}| j                  }||vrUt        | j                  j                  dj                  | j                  j                  |      || j                  |      ||<   ||   S )Nr    r;   CP1zhttps://{}/{})	client_id	authorityrQ   http_clientclient_capabilities)r   r#   r)   r'   r   rN   r(   r   r   r"   r]   formatr^   r*   )r+   r,   r   client_applications_mapcapabilitiesrQ   s         r-   _get_client_applicationz6SilentAuthenticationCredential._get_client_application   s    "OO
:>:\:\
`f
	 #'";";kk::l#&*&C&C#!7L//K331H++55)001B1B1L1LiX' LL$02#I. 'y11r/   c           	      "   d} | j                   di |}|j                  | j                  j                        }|st	        d      |D ]  }|j                  d      | j                  j                  k7  r,t        t        j                               }|j                  t        |      ||j                  d            }|sxd|v s}d|v sd	|v rt        |d	         nd}t        |d   |t        |d         z   |j                  d
d      |      c S  |rHd}	|j                  d      xs |j                  d      }
|
r|	dj                  |
      z  }	t        |	      t	        t        j                  | j                  j                              )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_idr:   )accountclaims_challengeaccess_token
expires_in
refresh_on
token_typeBearer)rm   rl   zToken acquisition failederror_descriptionerrorz: {}rK   r!   )rd   get_accountsr"   rf   r   rN   rg   inttimeacquire_token_silent_with_errorlistr	   ra   r   r   )r+   r<   r,   resultclient_applicationaccounts_for_userrh   nowrl   rL   detailss              r-   rP   z4SilentAuthenticationCredential._acquire_token_silent   s    9T99CFC.;;TEVEVE_E_;` ,-uvv( 	G{{,-1B1B1R1RRdiik"C'GGVg

8@T H F .F2|v7M:F&:PS!56VZ
&>*#f\233%zz,A)	 	& 0Gjj!45LG9LG6==11+G<< )ARARA[A[1\]]r/   c                 ^    | j                   j                         }| j                  s|d= |d= |S )Nr   r   )__dict__copyr%   r+   states     r-   __getstate__z+SilentAuthenticationCredential.__getstate__   s1    ""$!!hl#r/   r   c                 p    | j                   j                  |       | j                  sd | _        d | _        y y r1   )r|   updater%   r   r   r~   s     r-   __setstate__z+SilentAuthenticationCredential.__setstate__   s0    U#!!DK"DO "r/   )r   r   )r   N)F)__name__
__module____qualname____doc__r   r   strr.   r2   r5   r8   boolr   r   r>   r
   r	   rG   rB   r   rO   rd   r   rP   r   r   r   r!   r/   r-   r   r      s    Z^,%9,IQRU,	,.% !%#' DD D C=	D
 D D 
D$ UY ac aH=P4Q a]l a 26 0	xx -.x 	x
 x 
x<: :*9M :22 22 +^S +^C +^O +^ +^Zd38n #$sCx. #T #r/   r   ) rW   rs   typingr   r   r   msalr   r   azure.core.credentialsr   r	   r
   azure.core.exceptionsr    r   	_internalr   r   r   _internal.decoratorsr   _internal.msal_clientr   _internal.shared_token_cacher   _persistent_cacher   r   r   r   r!   r/   r-   <module>r      sC   
   & & 4 T T ; ) F F 2 . 3 T #I# I#r/   