
    jG(                         d dl Z d dlmZmZmZ d dlmZ d dlmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ  e j$                  e      Zd Z G d	 d
      Zy)    N)AnyOptionalcast)ClientAuthenticationError)AccessTokenAccessTokenInfoTokenRequestOptionsSupportsTokenInfoTokenCredentialTokenProvider   )CredentialUnavailableError)within_credential_chainc                 "   g }| D ]i  \  }}t        |d      r|j                  }n|j                  j                  }|r"|j	                  dj                  ||             Y|j	                  |       k dj                  dj                  |            S )N_credential_namez{}: {}z
Attempted credentials:
	{}z
	)hasattrr   	__class____name__appendformatjoin)historyattempts
credentialerrorcredential_names        d/root/aria/tools/markitdown-venv/lib/python3.12/site-packages/azure/identity/_credentials/chained.py_get_error_messager      s    H$ 
-
E:12(99O(22;;OOOHOOOUCDOOO,
-  &H!    c                       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	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y)ChainedTokenCredentiala@  A sequence of credentials that is itself a credential.

    Its :func:`get_token` method calls ``get_token`` on each credential in the sequence, in order, returning the first
    valid token received. For more information, see `ChainedTokenCredential overview
    <"https://aka.ms/azsdk/python/identity/credential-chains#chainedtokencredential-overview">`__.

    :param credentials: credential instances to form the chain
    :type credentials: ~azure.core.credentials.TokenProvider

    .. admonition:: Example:

        .. literalinclude:: ../samples/credential_creation_code_snippets.py
            :start-after: [START create_chained_token_credential]
            :end-before: [END create_chained_token_credential]
            :language: python
            :dedent: 4
            :caption: Create a ChainedTokenCredential.
    credentialsreturnNc                 :    |st        d      d | _        || _        y )Nz#at least one credential is required)
ValueError_successful_credentialr"   )selfr"   s     r   __init__zChainedTokenCredential.__init__>   s!    BCC?C#&r   c                 H    | j                   D ]  }|j                           | S N)r"   	__enter__)r'   r   s     r   r+   z ChainedTokenCredential.__enter__E   s(    ** 	#J  "	#r   argsc                 D    | j                   D ]  } |j                  |   y r*   )r"   __exit__)r'   r,   r   s      r   r.   zChainedTokenCredential.__exit__J   s'    ** 	'JJ&	'r   c                 $    | j                          y)z<Close the transport session of each credential in the chain.N)r.   )r'   s    r   closezChainedTokenCredential.closeN   s    r   Fclaims	tenant_id
enable_caescopesr2   r3   r4   kwargsc          	      $   t        j                  d       g }| j                  D ]  }	 t        |d      r% t	        t
        |      j                  ||||d|}nUi }	|r||	d<   |r||	d<   ||	d<    t	        t        |      j                  |d|	i}
t        |
j                  |
j                        }t        j                  d| j                  j                  |j                  j                         || _        t        j                  d	       |c S  t        j                  d	       t/        |      }| j                  j                  dz   |z   dz   }t        j1                  |       t3        |      # t"        $ r(}|j%                  ||j&                  f       Y d
}~vd
}~wt(        $ ri}|j%                  |t+        |      f       t        j-                  d| j                  j                  |j                  j                  |d       Y d
}~ d
}~ww xY w)a  Request a token from each chained credential, in order, returning the first token received.

        If no credential provides a token, raises :class:`azure.core.exceptions.ClientAuthenticationError`
        with an error message from each credential.

        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.
        :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.core.exceptions.ClientAuthenticationError: no credential in the chain provided a token
        T	get_tokenr1   r2   r3   r4   options%s acquired a token from %sFNz4%s.get_token failed: %s raised unexpected error "%s"exc_info: failed to retrieve a token from the included credentials.
To mitigate this issue, please refer to the troubleshooting guidelines here at https://aka.ms/azsdk/python/identity/defaultazurecredential/troubleshoot.message)r   setr"   r   r   r   r8   r
   get_token_infor   token
expires_on_LOGGERinfor   r   r&   r   r   r@   	Exceptionstrdebugr   warningr   )r'   r2   r3   r4   r5   r6   r   r   rC   r9   
token_infoexr   r@   s                 r   r8   z ChainedTokenCredential.get_tokenR   s   : 	 ##D)** #	J":{3GD*=GG)PZ^dE 46G,2) /8,,6GL)!S&7!D!S!SU[!mel!mJ'
(8(8*:O:OPE:DNN<S<SU_UiUiUrUrs.8+'++E2)#	H 	 ##E*%g.NN##JKXX 	 	 '881 . 9
BJJ788 

CG45JNN++((11!   
s%   C"E..	H7FH&AH

H)r9   r9   c          	      8   t        j                  d       g }|xs i }| j                  D ]  }	 t        |d      r! t	        t
        |      j                  |d|i}n]|j                  d      rt        d       t	        t        |      j                  |i |}t        |j                  |j                        }t        j                  d| j                   j"                  |j                   j"                         || _        t        j                  d       |c S  t        j                  d       t1        |      }| j                   j"                  dz   |z   dz   }	t        j3                  |	       t5        |	      # t        $ r(}|j'                  ||j(                  f       Y d	}~zd	}~wt*        $ ri}|j'                  |t-        |      f       t        j/                  d
| j                   j"                  |j                   j"                  |d       Y d	}~ d	}~ww xY w)aZ  Request a token from each chained credential, in order, returning the first token received.

        If no credential provides a token, raises :class:`azure.core.exceptions.ClientAuthenticationError`
        with an error message from each credential.

        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 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 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.core.exceptions.ClientAuthenticationError: no credential in the chain provided a token.
        TrB   r9   popzDProof of possession arguments are not supported for this credential.)rC   rD   r:   FNz9%s.get_token_info failed: %s raised unexpected error "%s"r;   r=   r>   r?   )r   rA   r"   r   r   r
   rB   getr   r   r8   r   rC   rD   rE   rF   r   r   r&   r   r@   rG   rH   rI   r   rJ   r   )
r'   r9   r5   r   r   rK   rC   rL   r   r@   s
             r   rB   z%ChainedTokenCredential.get_token_info   s   & 	 ##D)-R** 	J:'78!S&7!D!S!SU[!mel!mJ{{5)8b  HD*=GG[SZ[E!0u{{uO_O_!`J:DNN<S<SU_UiUiUrUrs.8+'++E2!!!	@ 	 ##E*%g.NN##JKXX 	 	 '883 . 9
BJJ788 

CG45ONN++((11!   
s%   C&E88	HF$$H0AHH)r#   r!   )r#   N)r   
__module____qualname____doc__r   r(   r+   r   r.   r0   rH   r   boolr   r8   r	   r   rB    r   r   r!   r!   *   s    &'] 't '
'c 'd ' !%#' M9M9 M9 C=	M9
 M9 M9 
M9^ UY @9c @9H=P4Q @9]l @9r   r!   )loggingtypingr   r   r   azure.core.exceptionsr   azure.core.credentialsr   r   r	   r
   r   r    r   	_internalr   	getLoggerr   rE   r   r!   rT   r   r   <module>r\      sH   
  & & ;  * /
'

H
%&w9 w9r   