o
    Mf/                  
   @   s.  d Z ddlZddlmZmZmZmZ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mZmZmZ dd	lmZ eeZeeeef ZzddlaW n& e y`   daY n e!y{ Z" ze#d
ee" daW Y dZ"["ndZ"["ww dee dee dee fddZ$G dd de
Z%dS )zNetwork Authentication Helpers

Contains interface (MultiDomainBasicAuth) and associated glue code for
providing credentials in the context of network requests.
    N)AnyDictListOptionalTuple)AuthBaseHTTPBasicAuth)RequestResponse)get_netrc_auth)	getLogger)ask	ask_inputask_passwordremove_auth_from_urlsplit_auth_netloc_from_url)AuthInfo*Keyring is skipped due to an exception: %surlusernamereturnc              
   C   s   | rt sdS zCzt j}W n	 ty   Y nw td|  || |}|dur,|j|jfW S W dS |rDtd|  t | |}|rG||fW S W dS W dS  tyf } zt	dt
| da W Y d}~dS d}~ww )z3Return the tuple auth for a given url from keyring.Nz'Getting credentials from keyring for %sz$Getting password from keyring for %sr   )keyringget_credentialAttributeErrorloggerdebugr   passwordget_password	Exceptionwarningstr)r   r   r   credr   exc r#   \/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/pip/_internal/network/auth.pyget_keyring_auth(   s>   


	r%   c                	   @   s  e Zd Z	d#dedeee  ddfddZdedee fd	d
Z		d$dededede	fddZ
dedeeee ee f fddZdedefddZdedeee ee ef fddZdefddZdededefddZdededdfdd Zdededdfd!d"ZdS )%MultiDomainBasicAuthTN	prompting
index_urlsr   c                 C   s   || _ || _i | _d | _d S )N)r'   r(   	passwords_credentials_to_save)selfr'   r(   r#   r#   r$   __init__J   s   
zMultiDomainBasicAuth.__init__r   c                 C   sB   |r| j sdS | j D ]}t|dd }||r|  S q
dS )a  Return the original index URL matching the requested URL.

        Cached or dynamically generated credentials may work against
        the original index URL rather than just the netloc.

        The provided url should have had its username and password
        removed already. If the original index url had credentials then
        they will be included in the return value.

        Returns None if no matching index was found, or if --no-index
        was specified by the user.
        N/)r(   r   rstrip
startswith)r+   r   uprefixr#   r#   r$   _get_index_urlW   s   


z#MultiDomainBasicAuth._get_index_urlForiginal_urlallow_netrcallow_keyringc                 C   s   t |\}}}|\}}|dur|durtd| |S | |}	|	r3t |	}
|
r3|
\}	}}td|	 |	rO|d durO|\}}|durO|durOtd| |S |r_t|}|r_td| |S |rut|	|pjt||}|rutd| |S ||fS )z2Find and return credentials for the specified URL.NzFound credentials in url for %szFound index url %sr   z%Found credentials in index url for %sz!Found credentials in netrc for %sz#Found credentials in keyring for %s)r   r   r   r2   r   r%   )r+   r3   r4   r5   r   netlocurl_user_passwordr   r   	index_url
index_info_index_url_user_password
netrc_authkr_authr#   r#   r$   _get_new_credentialsm   s>   



z)MultiDomainBasicAuth._get_new_credentialsc           	      C   s   t |\}}}| |\}}|du s|du r/|| jv r/| j| \}}|du s*||kr/||}}|dus7|durF|p:d}|p>d}||f| j|< |durN|dus]|du rV|du s]J d| |||fS )a_  Return the credentials to use for the provided URL.

        If allowed, netrc and keyring may be used to obtain the
        correct credentials.

        Returns (url_without_credentials, username, password). Note
        that even if the original URL contains credentials, this
        function may return a different username and password.
        N z%Could not load credentials from url: )r   r>   r)   )	r+   r3   r   r6   r:   r   r   unpwr#   r#   r$   _get_url_and_credentials   s   

z-MultiDomainBasicAuth._get_url_and_credentialsreqc                 C   sH   |  |j\}}}||_|d ur|d urt|||}|d| j |S )Nresponse)rB   r   r   register_hook
handle_401)r+   rC   r   r   r   r#   r#   r$   __call__   s   zMultiDomainBasicAuth.__call__r6   c                 C   sb   t d| d}|sdS t||}|r(|d d ur(|d d ur(|d |d dfS td}||dfS )	Nz	User for z: )NNFr      Fz
Password: T)r   r%   r   )r+   r6   r   authr   r#   r#   r$   _prompt_for_password   s   

z)MultiDomainBasicAuth._prompt_for_passwordc                 C   s   t sdS tdddgdkS )NFz#Save credentials to keyring [y/N]: yn)r   r   )r+   r#   r#   r$    _should_save_password_to_keyring   s   z5MultiDomainBasicAuth._should_save_password_to_keyringrespkwargsc           	      K   s  |j dkr|S | js|S tj|j}| j|jddd\}}d}|s-|s-| |j\}}}d | _	|d urM|d urM||f| j
|j< |rM|  rM|j||f| _	|j |j  t|pYd|p\d|j}|d| j | j	rr|d| j |jj|fi |}|j| |S )N  FT)r4   r5   r?   rD   )status_coder'   urllibparseurlparser   r>   rJ   r6   r*   r)   rM   contentrawrelease_connr   requestrE   warn_on_401save_credentials
connectionsendhistoryappend)	r+   rN   rO   parsedr   r   saverC   new_respr#   r#   r$   rF      s6   


zMultiDomainBasicAuth.handle_401c                 K   s"   |j dkrtd|jj dS dS )z6Response callback to warn about incorrect credentials.rP   z)401 Error, Credentials not correct for %sN)rQ   r   r   rX   r   )r+   rN   rO   r#   r#   r$   rY   .  s   
z MultiDomainBasicAuth.warn_on_401c                 K   st   t dusJ dt sdS | j}d| _|r6|jdk r8ztd t j|  W dS  ty5   td Y dS w dS dS )z1Response callback to save credentials on success.Nz'should never reach here without keyringi  zSaving credentials to keyringzFailed to save credentials)r   r*   rQ   r   infoset_passwordr   	exception)r+   rN   rO   credsr#   r#   r$   rZ   6  s   
z%MultiDomainBasicAuth.save_credentials)TN)TF)__name__
__module____qualname__boolr   r   r    r,   r2   r   r>   r   rB   r	   rG   rJ   rM   r
   r   rF   rY   rZ   r#   r#   r#   r$   r&   I   sH    


8
0
6r&   )&__doc__urllib.parserR   typingr   r   r   r   r   pip._vendor.requests.authr   r   pip._vendor.requests.modelsr	   r
   pip._vendor.requests.utilsr   pip._internal.utils.loggingr   pip._internal.utils.miscr   r   r   r   r    pip._internal.vcs.versioncontrolr   rf   r   r    Credentialsr   ImportErrorr   r"   r   r%   r&   r#   r#   r#   r$   <module>   s2    "!