o
    iP                     @   s$  d dl Z d dlZ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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# ddl$m%Z% ddl&m'Z' ddl(m)Z) ddl*m+Z+ e ,e-Z.G dd dZ/G dd deZ0dS )    N)ListAnyOptionalcast)AccessTokenAccessTokenInfoTokenRequestOptionsSupportsTokenInfoTokenCredential   )CredentialUnavailableError)EnvironmentVariables)get_default_authoritynormalize_authority
within_dacprocess_credential_exclusions   )AzurePowerShellCredential)BrokerCredential)InteractiveBrowserCredential)ChainedTokenCredential)EnvironmentCredential)ManagedIdentityCredential)SharedTokenCacheCredential)AzureCliCredential)AzureDeveloperCliCredential)VisualStudioCodeCredential)WorkloadIdentityCredentialc                   @   s   e Zd ZdZdededdfddZded	edefd
dZddde	e
 d	edefddZdddZdeddfddZdddZdS )FailedDACCredentialzThis acts as a substitute for a credential that has failed to initialize in the DAC chain.

    This allows instantiation errors to be reported in ChainTokenCredential if all token requests fail.
    credential_nameerrorreturnNc                 C   s   || _ || _d S N)_errorZ_credential_name)selfr   r     r%   Z/home/app/Keep/.python/lib/python3.10/site-packages/azure/identity/_credentials/default.py__init__(   s   
zFailedDACCredential.__init__scopeskwargsc                 O   
   t | jr"   r   r#   )r$   r(   r)   r%   r%   r&   	get_token,      
zFailedDACCredential.get_tokenoptionsr/   c                O   r*   r"   r+   )r$   r/   r(   r)   r%   r%   r&   get_token_info/   r-   z"FailedDACCredential.get_token_infoc                 C   s   | S r"   r%   r$   r%   r%   r&   	__enter__2      zFailedDACCredential.__enter__argsc                 G      d S r"   r%   )r$   r4   r%   r%   r&   __exit__5   r3   zFailedDACCredential.__exit__c                 C   r5   r"   r%   r1   r%   r%   r&   close8   r3   zFailedDACCredential.close)r!   r   )r!   N)__name__
__module____qualname____doc__strr'   r   r   r,   r   r   r   r0   r2   r6   r7   r%   r%   r%   r&   r   "   s     
r   c                       s   e Zd ZdZdeddf fddZdddded	ee d
ee dedef
 fddZ	dddedee
 def fddZ  ZS )DefaultAzureCredentiala  A credential capable of handling most Azure SDK authentication scenarios. For more information, See
    `Usage guidance for DefaultAzureCredential
    <"https://aka.ms/azsdk/python/identity/credential-chains#usage-guidance-for-defaultazurecredential">`__.

    The identity it uses depends on the environment. When an access token is needed, it requests one using these
    identities in turn, stopping when one provides a token:

    1. A service principal configured by environment variables. See :class:`~azure.identity.EnvironmentCredential` for
       more details.
    2. WorkloadIdentityCredential if environment variable configuration is set by the Azure workload
       identity webhook.
    3. An Azure managed identity. See :class:`~azure.identity.ManagedIdentityCredential` for more details.
    4. On Windows only: a user who has signed in with a Microsoft application, such as Visual Studio. If multiple
       identities are in the cache, then the value of  the environment variable ``AZURE_USERNAME`` is used to select
       which identity to use. See :class:`~azure.identity.SharedTokenCacheCredential` for more details.
    5. The identity logged in to Visual Studio Code with the Azure Resources extension.
    6. The identity currently logged in to the Azure CLI.
    7. The identity currently logged in to Azure PowerShell.
    8. The identity currently logged in to the Azure Developer CLI.
    9. Brokered authentication. On Windows and WSL only, this uses the default account logged in via
       Web Account Manager (WAM) if the `azure-identity-broker` package is installed.

    This default behavior is configurable with keyword arguments.

    :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. Managed identities ignore this because they reside in a single cloud.
    :keyword bool exclude_workload_identity_credential: Whether to exclude the workload identity from the credential.
        Defaults to **False**.
    :keyword bool exclude_developer_cli_credential: Whether to exclude the Azure Developer CLI
        from the credential. Defaults to **False**.
    :keyword bool exclude_cli_credential: Whether to exclude the Azure CLI from the credential. Defaults to **False**.
    :keyword bool exclude_environment_credential: Whether to exclude a service principal configured by environment
        variables from the credential. Defaults to **False**.
    :keyword bool exclude_managed_identity_credential: Whether to exclude managed identity from the credential.
        Defaults to **False**.
    :keyword bool exclude_powershell_credential: Whether to exclude Azure PowerShell. Defaults to **False**.
    :keyword bool exclude_visual_studio_code_credential: Whether to exclude stored credential from VS Code.
        Defaults to **False**.
    :keyword bool exclude_shared_token_cache_credential: Whether to exclude the shared token cache. Defaults to
        **False**.
    :keyword bool exclude_interactive_browser_credential: Whether to exclude interactive browser authentication (see
        :class:`~azure.identity.InteractiveBrowserCredential`). Defaults to **True**.
    :keyword bool exclude_broker_credential: Whether to exclude the broker credential from the credential chain.
        Defaults to **False**.
    :keyword str interactive_browser_tenant_id: Tenant ID to use when authenticating a user through
        :class:`~azure.identity.InteractiveBrowserCredential`. Defaults to the value of environment variable
        AZURE_TENANT_ID, if any. If unspecified, users will authenticate in their home tenants.
    :keyword str broker_tenant_id: The tenant ID to use when using brokered authentication. Defaults to the value of
        environment variable AZURE_TENANT_ID, if any. If unspecified, users will authenticate in their home tenants.
    :keyword str managed_identity_client_id: The client ID of a user-assigned managed identity. Defaults to the value
        of the environment variable AZURE_CLIENT_ID, if any. If not specified, a system-assigned identity will be used.
    :keyword str workload_identity_client_id: The client ID of an identity assigned to the pod. Defaults to the value
        of the environment variable AZURE_CLIENT_ID, if any. If not specified, the pod's default identity will be used.
    :keyword str workload_identity_tenant_id: Preferred tenant for :class:`~azure.identity.WorkloadIdentityCredential`.
        Defaults to the value of environment variable AZURE_TENANT_ID, if any.
    :keyword str interactive_browser_client_id: The client ID to be used in interactive browser credential. If not
        specified, users will authenticate to an Azure development application.
    :keyword str broker_client_id: The client ID to be used in brokered authentication. If not specified, users will
        authenticate to an Azure development application.
    :keyword str shared_cache_username: Preferred username for :class:`~azure.identity.SharedTokenCacheCredential`.
        Defaults to the value of environment variable AZURE_USERNAME, if any.
    :keyword str shared_cache_tenant_id: Preferred tenant for :class:`~azure.identity.SharedTokenCacheCredential`.
        Defaults to the value of environment variable AZURE_TENANT_ID, if any.
    :keyword str visual_studio_code_tenant_id: Tenant ID to use when authenticating with
        :class:`~azure.identity.VisualStudioCodeCredential`. Defaults to the tenant specified in the authentication
        record file used by the Azure Resources extension.
    :keyword int process_timeout: The timeout in seconds to use for developer credentials that run
        subprocesses (e.g. AzureCliCredential, AzurePowerShellCredential). Defaults to **10** seconds.
    :keyword bool require_envvar: If **True**, require that the AZURE_TOKEN_CREDENTIALS environment variable be set
        to a value denoting the credential type or credential group to use. If unset or empty, DefaultAzureCredential
        will raise a `ValueError`. Defaults to **False**.

    .. admonition:: Example:

        .. literalinclude:: ../samples/credential_creation_code_snippets.py
            :start-after: [START create_default_credential]
            :end-before: [END create_default_credential]
            :language: python
            :dedent: 4
            :caption: Create a DefaultAzureCredential.
    r)   r!   Nc           $         s
  d|v rt d|dd }|rt|nt }|dd }|dtjtj}|dtjtj	}|d|}|dtjtj}|d	d }|d
tjtj}	|dd }
|dtjtj
}|dtjtj}|dd}|dd}tjtjd  }|r|stdddddddddddddddddddddd ddd!d"ddd#d$ddd%d&d'dd(ddd)
}i }i }| D ]\}}tt|d* }||d ||< |d+ ||< qt|||}|d, }|d- }|d. }|d/ }|d0 }|d1 }|d2 }|d3 }|d4 }|d5 }g } td' |s+| td@|d'd6| |sez| td@tt||tjtjd7| W n tyd }! z| td8t|!d9 W Y d }!~!nd }!~!ww |sx| td@|||dkd:| |st rtd@|||d;|}"| |" |s| t|d< |s| t|d= |s| t|d= |s| t |d= |s|r| t!d@||d>| n| t!d@d|i| |sd|	i|}#|
r|
|#d?< | t"d@i |# td t#t$| j%|   d S )AN	tenant_idz7'tenant_id' is not supported in DefaultAzureCredential.	authorityZvisual_studio_code_tenant_idinteractive_browser_tenant_idmanaged_identity_client_idworkload_identity_client_idworkload_identity_tenant_idinteractive_browser_client_idbroker_tenant_idbroker_client_idshared_cache_usernameshared_cache_tenant_idprocess_timeout
   require_envvarF zAZURE_TOKEN_CREDENTIALS environment variable is required but is not set or is empty. Set it to 'dev', 'prod', or a specific credential name.exclude_environment_credentialZenvironmentcredential)exclude_paramenv_namedefault_exclude$exclude_workload_identity_credentialZworkloadidentitycredential#exclude_managed_identity_credentialZmanagedidentitycredential%exclude_shared_token_cache_credential)rN   rP   %exclude_visual_studio_code_credentialZvisualstudiocodecredentialexclude_cli_credentialZazureclicredential exclude_developer_cli_credentialZazuredeveloperclicredentialexclude_powershell_credentialZazurepowershellcredential&exclude_interactive_browser_credentialZinteractivebrowsercredentialTexclude_broker_credential)
environmentworkload_identitymanaged_identityshared_token_cachevisual_studio_codeclideveloper_cli
powershellinteractive_browserbrokerrN   rP   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   )r?   Z_within_dac)	client_idr>   Ztoken_file_pathr   )r    )rd   Z%_exclude_workload_identity_credentialZ_enable_imds_probe)usernamer>   r?   )r>   )rI   )r>   rd   rd   r%   )&	TypeErrorpopr   r   osenvirongetr   ZAZURE_TENANT_IDZAZURE_CLIENT_IDZAZURE_USERNAMEZAZURE_TOKEN_CREDENTIALSstriplower
ValueErroritemsr   r<   r   r   setappendr   r   ZAZURE_FEDERATED_TOKEN_FILEr   r   r   	supportedr   r   r   r   r   r   superr=   r'   )$r$   r)   r?   Zvscode_tenant_idr@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rK   Ztoken_credentials_envZcredential_configZexclude_flagsZuser_excludesZcred_keyconfig
param_namerM   rQ   rR   rS   rT   rU   rV   rW   rX   rY   credentialsexshared_cacheZbroker_credential_args	__class__r%   r&   r'      s$  4
"

zDefaultAzureCredential.__init__claimsr>   r(   r{   r>   c             	      s   | j r tt| j j|||d|}td| jj| j jj |S t	d zt
 j|||d|}W t	d |S t	d w )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.core.exceptions.ClientAuthenticationError: authentication failed. The exception has a
            `message` attribute listing each authentication attempt and its error message.
        rz   %s acquired a token from %sTF)_successful_credentialr   r
   r,   _LOGGERinfory   r8   r   ro   rr   )r$   r{   r>   r(   r)   tokenrx   r%   r&   r,   ;  s    

z DefaultAzureCredential.get_tokenr.   r/   c             	      sz   | j rtt| j j|d|i}td| jj| j jj |S t	d zttt
 j|d|i}W t	d |S t	d w )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 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: authentication failed. The exception has a
           `message` attribute listing each authentication attempt and its error message.
        r/   r|   TF)r}   r   r	   r0   r~   r   ry   r8   r   ro   rr   )r$   r/   r(   Z
token_inforx   r%   r&   r0   ^  s   

z%DefaultAzureCredential.get_token_info)r8   r9   r:   r;   r   r'   r<   r   r   r,   r   r   r0   __classcell__r%   r%   rx   r&   r=   <   s"    S -,#r=   )1loggingrh   typingr   r   r   r   Zazure.core.credentialsr   r   r   r	   r
   rL   r   
_constantsr   Z_internal.utilsr   r   r   r   Zazure_powershellr   rc   r   Zbrowserr   Zchainedr   rZ   r   r\   r   rw   r   Z	azure_clir   Zazd_clir   Zvscoder   r[   r   	getLoggerr8   r~   r   r=   r%   r%   r%   r&   <module>   s*   
