
    j$                         d dl Z d dlZd dlmZmZmZmZ d dlZd dl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 d	d
lmZ d	dlmZ d	dlmZ eeeef   Z e j8                  e      Z G d d      Zy)    N)OptionalUnionAnycast)AccessTokenAccessTokenInfoTokenRequestOptionsSupportsTokenInfo   )CredentialUnavailableError)EnvironmentVariables)
within_dac)log_get_token   )CertificateCredential)ClientSecretCredential)UsernamePasswordCredentialc                       e Zd ZdZdeddfdZddZdeddfdZdd	Ze	ddd
de
dee
   dee
   dedef
d       Ze	ddde
dee   defd       Zy)EnvironmentCredentiala  A credential configured by environment variables.

    This credential is capable of authenticating as a service principal using a client secret or a certificate.
    Configuration is attempted in this order, using these environment variables:

    Service principal with secret:
      - **AZURE_TENANT_ID**: ID of the service principal's tenant. Also called its 'directory' ID.
      - **AZURE_CLIENT_ID**: the service principal's client ID
      - **AZURE_CLIENT_SECRET**: one of the service principal's client secrets
      - **AZURE_AUTHORITY_HOST**: authority of a Microsoft Entra endpoint, for example
        "login.microsoftonline.com", the authority for Azure Public Cloud, which is the default
        when no value is given.

    Service principal with certificate:
      - **AZURE_TENANT_ID**: ID of the service principal's tenant. Also called its 'directory' ID.
      - **AZURE_CLIENT_ID**: the service principal's client ID
      - **AZURE_CLIENT_CERTIFICATE_PATH**: path to a PEM or PKCS12 certificate file including the private key.
      - **AZURE_CLIENT_CERTIFICATE_PASSWORD**: (optional) password of the certificate file, if any.
      - **AZURE_CLIENT_SEND_CERTIFICATE_CHAIN**: (optional) If True, the credential will send the public certificate
        chain in the x5c header of each token request's JWT. This is required for Subject Name/Issuer (SNI)
        authentication. Defaults to False.
      - **AZURE_AUTHORITY_HOST**: authority of a Microsoft Entra endpoint, for example
        "login.microsoftonline.com", the authority for Azure Public Cloud, which is the default
        when no value is given.

    .. admonition:: Example:

        .. literalinclude:: ../samples/credential_creation_code_snippets.py
            :start-after: [START create_environment_credential]
            :end-before: [END create_environment_credential]
            :language: python
            :dedent: 4
            :caption: Create an EnvironmentCredential.
    kwargsreturnNc                     d | _         t        d t        j                  D              rst	        dt
        j                  t        j                     t
        j                  t        j                     t
        j                  t        j                     d|| _         nt        d t        j                  D              rt        dt
        j                  t        j                     t
        j                  t        j                     t
        j                  t        j                     t
        j                  j                  t        j                        t        t
        j                  j                  t        j                   d            d|| _         nt        d t        j"                  D              rt%        dt
        j                  t        j                     t
        j                  t        j&                     t
        j                  t        j(                     t
        j                  j                  t        j                        dd|| _         t+        j,                  d	t.        t1        j                         rd
nd       | j                   r5t2        j5                  d| j                   j6                  j8                         y t;        t        j                  t        j                  z   t        j"                  z         }|D cg c]  }|t
        j                  v s| }}|rVt2        j=                  |j                  d      rt>        j@                  nt>        jB                  ddjE                  |             y t2        j5                  d       y c c}w )Nc              3   ^   K   | ]%  }t         j                  j                  |      d u ' y wNosenvironget.0vs     h/root/aria/tools/markitdown-venv/lib/python3.12/site-packages/azure/identity/_credentials/environment.py	<genexpr>z1EnvironmentCredential.__init__.<locals>.<genexpr>?   s"     ^rzz~~a ,^   +-)	client_idclient_secret	tenant_idc              3   ^   K   | ]%  }t         j                  j                  |      d u ' y wr   r   r   s     r"   r#   z1EnvironmentCredential.__init__.<locals>.<genexpr>F   s"     W1"$.Wr$   F)r%   r'   certificate_pathpasswordsend_certificate_chainc              3   ^   K   | ]%  }t         j                  j                  |      d u ' y wr   r   r   s     r"   r#   z1EnvironmentCredential.__init__.<locals>.<genexpr>Q   s"     d1"$.dr$   T)r%   usernamer*   r'   _silence_deprecation_warningzEnvironment is configured to use username and password authentication. This authentication method is deprecated, as it doesn't support multifactor authentication (MFA). For more details, see https://aka.ms/azsdk/identity/mfa.   r   )
stacklevelz Environment is configured for %s_within_dacz[Incomplete environment configuration for EnvironmentCredential. These variables are set: %sz, z#No environment configuration found. )#_credentialallr   CLIENT_SECRET_VARSr   r   r   AZURE_CLIENT_IDAZURE_CLIENT_SECRETAZURE_TENANT_ID	CERT_VARSr   AZURE_CLIENT_CERTIFICATE_PATHr   !AZURE_CLIENT_CERTIFICATE_PASSWORDbool#AZURE_CLIENT_SEND_CERTIFICATE_CHAINUSERNAME_PASSWORD_VARSr   AZURE_USERNAMEAZURE_PASSWORDwarningswarnDeprecationWarningr   _LOGGERinfo	__class____name__setlogloggingINFOWARNINGjoin)selfr   expected_variablesr!   set_variabless        r"   __init__zEnvironmentCredential.__init__<   s   AE^6J6]6]^^5  **%9%I%IJ jj)=)Q)QR**%9%I%IJ  	 D W8L8V8VWW4 	 **%9%I%IJ**%9%I%IJ!#,@,^,^!_(<(^(^_'+JJNN#7#[#[]bc(	  	 D d8L8c8cdd9  **%9%I%IJ$8$G$GH$8$G$GH**..)=)M)MN-1   D MMK # * 01a LL;T=M=M=W=W=`=`a!$$..&99:&==>"
 );N1a2::oQNMN$*JJ}$=GLL7??qIIm, BC Os   ?NNc                 R    | j                   r| j                   j                          | S r   )r3   	__enter__rN   s    r"   rS   zEnvironmentCredential.__enter__t   s"    &&(    argsc                 P    | j                   r | j                   j                  |  y y r   )r3   __exit__)rN   rV   s     r"   rX   zEnvironmentCredential.__exit__y   s&    %D%%t, rU   c                 $    | j                          y)z)Close the credential's transport session.N)rX   rT   s    r"   closezEnvironmentCredential.close}   s    rU   claimsr'   scopesr\   r'   c                t    | j                   sd}t        |       | j                   j                  |||d|S )a@  Request an access token for `scopes`.

        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: optional tenant to include in the token request.

        :return: An access token with the desired scopes.
        :rtype: ~azure.core.credentials.AccessToken

        :raises ~azure.identity.CredentialUnavailableError: environment variable configuration is incomplete
        EnvironmentCredential authentication unavailable. Environment variables are not fully configured.
Visit https://aka.ms/azsdk/python/identity/environmentcredential/troubleshoot to troubleshoot this issue.messager[   )r3   r   	get_token)rN   r\   r'   r]   r   ra   s         r"   rb   zEnvironmentCredential.get_token   sG    (  
 -W==)t))6&I`Y_``rU   )optionsrc   c                    | j                   sd}t        |       t        t        | j                         j                  |d|iS )a  Request an access token for `scopes`.

        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: environment variable configuration is incomplete.
        r_   r`   rc   )r3   r   r   r
   get_token_info)rN   rc   r]   ra   s       r"   re   z$EnvironmentCredential.get_token_info   sK    "  
 -W==Gt%t'7'78GGaY`aarU   )r   r   )r   N)rG   
__module____qualname____doc__r   rQ   rS   rX   rZ   r   strr   r   rb   r	   r   re   r2   rU   r"   r   r      s    !F6D 6D 6Dp
-c -d - 48UYaa$,SMaEMc]aeha	a a8 TX bc bH=P4Q b]l b brU   r   ) rJ   r   typingr   r   r   r   rA   azure.core.credentialsr   r   r	   r
    r   
_constantsr   	_internalr   _internal.decoratorsr   certificater   r&   r   user_passwordr   EnvironmentCredentialTypes	getLoggerrG   rD   r   r2   rU   r"   <module>rt      sf   
  	 - -  g g ) - " 0 . 1 5"#8:PRl#lm 
'

H
%^b ^brU   