o
    iR'                  
   @   s  d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZ d dl	m
Z
mZ d dlmZ d dlmZ ddlmZmZ ed	d
dZedd
dZeeZeee d Zeee d Zeee d ZdedefddZdefddZdeddfddZdeddfddZ deddfddZ!	d0ddd edee d!ee
e  defd"d#Z"d$e#d%e#d&e#de#fd'd(Z$dee% fd)d*Z&de'fd+d,Z(d-edefd.d/Z)dS )1    N)
ContextVar)ascii_lettersdigits)ListOptional)urlparse)ClientAuthenticationError   )EnvironmentVariablesKnownAuthoritieswithin_credential_chainF)default
within_dacz-.z_-.:/z_-. 	authorityreturnc                 C   s>   t | }|jsd| d S |jdkrtd| | dS )zEnsure authority uses https, strip trailing spaces and /.

    :param str authority: authority to normalize
    :return: normalized authority
    :rtype: str
    :raises: ValueError if authority is not a valid https URL
    zhttps://z /httpszL'{}' is an invalid authority. The value must be a TLS protected (https) URL.)r   schemerstrip
ValueErrorformat)r   parsed r   U/home/app/Keep/.python/lib/python3.10/site-packages/azure/identity/_internal/utils.pynormalize_authority   s   	

r   c                  C   s   t jtjtj} t| S N)osenvirongetr
   ZAZURE_AUTHORITY_HOSTr   ZAZURE_PUBLIC_CLOUDr   )r   r   r   r   get_default_authority0   s   r   scopec                 C   "   | rt dd | D rtddS )zRaise ValueError if scope is empty or contains a character invalid for a scope

    :param str scope: scope to validate
    :raises: ValueError if scope is empty or contains a character invalid for a scope.
    c                 s       | ]}|t vV  qd S r   )VALID_SCOPE_CHARACTERS.0cr   r   r   	<genexpr>;       z!validate_scope.<locals>.<genexpr>zeAn invalid scope was provided. Only alphanumeric characters, '.', '-', '_', ':', and '/' are allowed.Nanyr   )r   r   r   r   validate_scope5   
   r*   	tenant_idc                 C   r    )zRaise ValueError if tenant_id is empty or contains a character invalid for a tenant ID.

    :param str tenant_id: tenant ID to validate
    :raises: ValueError if tenant_id is empty or contains a character invalid for a tenant ID.
    c                 s   r!   r   )VALID_TENANT_ID_CHARACTERSr#   r   r   r   r&   G   r'   z%validate_tenant_id.<locals>.<genexpr>zInvalid tenant ID provided. You can locate your tenant ID by following the instructions here: https://learn.microsoft.com/partner-center/find-ids-and-domain-namesNr(   )r,   r   r   r   validate_tenant_idA   r+   r.   subscriptionc                 C   s*   | rt dd | D rtd|  ddS )a  Raise ValueError if subscription is empty or contains a character invalid for a subscription name/ID.

    :param str subscription: subscription ID to validate
    :raises: ValueError if subscription is empty or contains a character invalid for a subscription ID.
    c                 s   r!   r   )VALID_SUBSCRIPTION_CHARACTERSr#   r   r   r   r&   T   r'   z(validate_subscription.<locals>.<genexpr>zSubscription 'z' contains invalid characters. If this is the name of a subscription, use its ID instead. You can locate your subscription by following the instructions listed here: https://learn.microsoft.com/azure/azure-portal/get-subscription-tenant-idNr(   )r/   r   r   r   validate_subscriptionN   s
   
r1   )additionally_allowed_tenantsdefault_tenantr2   c                K   s   |du s|| kr
| S | dkst jtjrtd| | | S | s"|S |du r(g }d|v s0||v r9td| | |S |sA| dkrA|S td|d)	a  Returns the correct tenant for a token request given a credential's configuration.

    :param str default_tenant: The tenant ID configured on the credential.
    :param str tenant_id: The tenant ID requested by the user.
    :keyword list[str] additionally_allowed_tenants: The list of additionally allowed tenants.
    :return: The tenant ID to use for the token request.
    :rtype: str
    :raises: ~azure.core.exceptions.ClientAuthenticationError
    NZadfszA token was request for a different tenant than was configured on the credential, but the configured value was used since multi tenant authentication has been disabled. Configured tenant ID: %s, Requested tenant ID %s*zA token was requested for a different tenant than was configured on the credential, and the requested tenant ID was used to authenticate. Configured tenant ID: %s, Requested tenant ID %sZorganizationsa  The current credential is not configured to acquire tokens for tenant {}. To enable acquiring tokens for this tenant add it to the additionally_allowed_tenants when creating the credential, or add "*" to additionally_allowed_tenants to allow acquiring tokens for any tenant.)message)	r   r   r   r
   Z&AZURE_IDENTITY_DISABLE_MULTITENANTAUTH_LOGGERinfor   r   )r3   r,   r2   _r   r   r   resolve_tenant\   s4   r9   credential_configexclude_flagsuser_excludesc              	   C   s"  t jtjd  }|dkr h d}| D ]}||v||< qn^|dkr4h d}| D ]}||v||< q*nJ|r~dd |  D }||vr]ddgt| }t	dtj d	| d
d
| dd}	|  D ]\}}
|
d|krr|}	 nqc| D ]}||	k||< qu| D ]\}}|dur|||< q|S )a  Process credential exclusions based on environment variable and user overrides.

    This method handles the AZURE_TOKEN_CREDENTIALS environment variable to determine
    which credentials should be excluded from the credential chain, and then applies
    any user-provided exclude overrides which take precedence over environment settings.

    :param credential_config: Configuration mapping for all available credentials, containing
        exclude parameter names, environment names, and default exclude settings
    :type credential_config: dict
    :param exclude_flags: Dictionary of exclude flags for each credential (will be modified)
    :type exclude_flags: dict
    :param user_excludes: User-provided exclude overrides from constructor kwargs
    :type user_excludes: dict

    :return: Dictionary of final exclude flags for each credential
    :rtype: dict

    :raises ValueError: If token_credentials_env contains an invalid credential name
     dev>   
powershellZbrokercliZshared_token_cacheZvisual_studio_codeZdeveloper_cliprod>   Zworkload_identityenvironmentZmanaged_identityc                 S   s   h | ]
}d |v r|d  qS )env_namer   )r$   configr   r   r   	<setcomp>   s    z0process_credential_exclusions.<locals>.<setcomp>zInvalid value for z: z. Valid values are: z, .NrC   )r   r   r   r
   ZAZURE_TOKEN_CREDENTIALSstriplowervaluessortedr   joinitems)r:   r;   r<   Ztoken_credentials_envZdev_credentialsZcred_keyZprod_credentialsZvalid_credentialsZvalid_valuesZselected_cred_keyrD   Z
user_valuer   r   r   process_credential_exclusions   sB   rM   c                  C   s(   z	ddl m}  | W S  ty   Y dS w )zReturn the InteractiveBrowserBrokerCredential class if available, otherwise None.

    :return: InteractiveBrowserBrokerCredential class or None
    :rtype: Optional[type]
    r   "InteractiveBrowserBrokerCredentialN)Zazure.identity.brokerrO   ImportErrorrN   r   r   r   get_broker_credential   s   rQ   c                  C   s@   t  } t| d| d  }t| d| d  }|dkod|v S )Nsystemr   releaser	   linuxZ	microsoft)platformunamegetattrrH   )rV   Zplatform_namerS   r   r   r   is_wsl   s   rX   sc                 C   s   t | d}|dS )Nzutf-8)base64	b64encodeencodedecode)rY   encodedr   r   r   encode_base64   s   
r_   r   )*rZ   r   rU   loggingcontextvarsr   stringr   r   typingr   r   urllib.parser   Zazure.core.exceptionsr   
_constantsr
   r   r   r   	getLogger__name__r6   	frozensetr-   r"   r0   strr   r   r*   r.   r1   r9   dictrM   typerQ   boolrX   r_   r   r   r   r   <module>   sJ   


5>