o
    i$                     @   s   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 eZG dd dZdS )    N)OptionalUnionAnycast)AccessTokenAccessTokenInfoTokenRequestOptionsSupportsTokenInfo   )CredentialUnavailableError)EnvironmentVariables)
within_dac)log_get_token   )CertificateCredential)ClientSecretCredential)UsernamePasswordCredentialc                   @   s   e Zd ZdZdeddfddZdddZd	eddfd
dZdddZe	dddde
dee
 dee
 dedef
ddZe	ddde
dee defddZdS )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                 K   s  d | _ tdd tjD r&tdtjtj tjtj tjtj	 d|| _ nitdd tj
D rXtdtjtj tjtj	 tjtj tjtjttjtjdd|| _ n7tdd tjD rtdtjtj tjtj tjtj tjtj	dd	|| _ tjd
tt rdndd | j rtd| j jj d S ttj
tj tj }dd |D }|rt|drtj ntj!dd"| d S td d S )Nc                 s        | ]}t j|d uV  qd S Nosenvironget.0v r   ^/home/app/Keep/.python/lib/python3.10/site-packages/azure/identity/_credentials/environment.py	<genexpr>?       z1EnvironmentCredential.__init__.<locals>.<genexpr>)	client_idclient_secret	tenant_idc                 s   r   r   r   r   r   r   r    r!   F   r"   F)r#   r%   Zcertificate_pathpasswordZsend_certificate_chainc                 s   r   r   r   r   r   r   r    r!   Q   r"   T)r#   usernamer&   r%   Z_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 %sc                 S   s   g | ]	}|t jv r|qS r   )r   r   r   r   r   r    
<listcomp>j   s    z2EnvironmentCredential.__init__.<locals>.<listcomp>Z_within_dacz[Incomplete environment configuration for EnvironmentCredential. These variables are set: %sz, z#No environment configuration found.r   )#_credentialallr   ZCLIENT_SECRET_VARSr   r   r   ZAZURE_CLIENT_IDZAZURE_CLIENT_SECRETZAZURE_TENANT_IDZ	CERT_VARSr   ZAZURE_CLIENT_CERTIFICATE_PATHr   Z!AZURE_CLIENT_CERTIFICATE_PASSWORDboolZ#AZURE_CLIENT_SEND_CERTIFICATE_CHAINZUSERNAME_PASSWORD_VARSr   ZAZURE_USERNAMEZAZURE_PASSWORDwarningswarnDeprecationWarningr   _LOGGERinfo	__class____name__setlogloggingINFOWARNINGjoin)selfr   Zexpected_variablesZset_variablesr   r   r    __init__<   sj   











zEnvironmentCredential.__init__c                 C   s   | j r| j   | S r   )r+   	__enter__r;   r   r   r    r=   t   s   
zEnvironmentCredential.__enter__argsc                 G   s   | j r| j j|  d S d S r   )r+   __exit__)r;   r?   r   r   r    r@   y   s   zEnvironmentCredential.__exit__c                 C   s   |    dS )z)Close the credential's transport session.N)r@   r>   r   r   r    close}   s   zEnvironmentCredential.closeclaimsr%   scopesrC   r%   c                O   s,   | j s
d}t|d| 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.messagerB   )r+   r   	get_token)r;   rC   r%   rD   r   rG   r   r   r    rH      s
   
zEnvironmentCredential.get_token)optionsrI   c                G   s,   | j s
d}t|d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.
        rE   rF   rI   )r+   r   r   r	   get_token_info)r;   rI   rD   rG   r   r   r    rJ      s
   
z$EnvironmentCredential.get_token_info)r   r   )r   N)r4   
__module____qualname____doc__r   r<   r=   r@   rA   r   strr   r   rH   r   r   rJ   r   r   r   r    r      s*    #
8
&r   ) r7   r   typingr   r   r   r   r.   Zazure.core.credentialsr   r   r   r	    r   
_constantsr   	_internalr   Z_internal.decoratorsr   Zcertificater   r$   r   Zuser_passwordr   ZEnvironmentCredentialTypes	getLoggerr4   r1   r   r   r   r   r    <module>   s   
