U ��.eo+� @sdZddlZddlmZmZddlmZddlmZ ddl m Z m Z m Z mZmZddlmZer�ddlmZdd lmZmZmZdd lmZeeeefZe�e�Zz ddlZWnLe k r�dZYn6e!k r�Z"ze�#d ee"��dZW5dZ"["XYnXd d �Z$Gdd�de�Z%dS)z�Network Authentication Helpers Contains interface (MultiDomainBasicAuth) and associated glue code for providing credentials in the context of network requests. �N)�AuthBase� HTTPBasicAuth)�get_netrc_auth)�parse)�ask� ask_input� ask_password�remove_auth_from_url�split_auth_netloc_from_url)�MYPY_CHECK_RUNNING)�Values)�Dict�Optional�Tuple)�AuthInfo�*Keyring is skipped due to an exception: %sc Cs�|rts dSz�z tj}Wntk r,Yn4Xt�d|�|||�}|dk rZ|j|jfWSWdS|r�t�d|�t�||�}|r�||fWSWn2tk r�}zt� dt |��W5d}~XYnXdS)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�debug�username�passwordZ get_password� Exception�warning�str)�urlrrZcredr�exc�r�>/usr/lib/python3.8/site-packages/pip/_internal/network/auth.py�get_keyring_auth.s,     �r c@s`eZdZddd�Zdd�Zddd�Zd d �Zd d �Zd d�Zdd�Z dd�Z dd�Z dd�Z dS)�MultiDomainBasicAuthTNcCs||_||_i|_d|_dS)N)� prompting� index_urls� passwords�_credentials_to_save)�selfr"r#rrr�__init__MszMultiDomainBasicAuth.__init__cCsB|r |jsdS|jD](}t|��d�d}|�|�r|SqdS)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�prefixrrr�_get_index_urlYs   z#MultiDomainBasicAuth._get_index_urlcCs�t|�\}}}|\}}|dk r6|dk r6t�d|�|S|�|�} | rft| �} | rf| \} } } t�d| �| r�| ddk r�| \}}|dk r�|dk r�t�d|�| S|r�t|�} | r�t�d|�| S|r�t| |�p�t||�}|r�t�d|�|S||fS)z2Find and return credentials for the specified URL.NzFound credentials in url for %szFound index url %srz%Found credentials in index url for %sz!Found credentials in netrc for %sz#Found credentials in keyring for %s)r rrr-rr )r&� original_urlZ allow_netrcZ allow_keyringr�netlocZurl_user_passwordrrZ index_urlZ index_info�_Zindex_url_user_passwordZ netrc_authZkr_authrrr�_get_new_credentialsns>�        � z)MultiDomainBasicAuth._get_new_credentialscCs�t|�\}}}|j�|d�\}}|dkr>|dkr>|�|�\}}|dk sN|dk rl|pTd}|p\d}||f|j|<|dk r||dk s�|dkr�|dks�td�|���|||fS)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. �NNN�z'Could not load credentials from url: {})r r$�getr1�AssertionError�format)r&r.rr/r0rrrrr�_get_url_and_credentials�s& �����z-MultiDomainBasicAuth._get_url_and_credentialscCsH|�|j�\}}}||_|dk r6|dk r6t||�|�}|�d|j�|S)N�response)r7rr� register_hook� handle_401)r&�reqrrrrrr�__call__�s zMultiDomainBasicAuth.__call__cCsFtd|�}|sdSt||�}|r4|d|ddfStd�}||dfS)Nz User for %s: r2r�Fz Password: T)rr r)r&r/rZauthrrrr�_prompt_for_password�s  z)MultiDomainBasicAuth._prompt_for_passwordcCstsdStdddg�dkS)NFz#Save credentials to keyring [y/N]: �y�n)rr)r&rrr� _should_save_password_to_keyring�sz5MultiDomainBasicAuth._should_save_password_to_keyringc Ks�|jdkr|S|js|St�|j�}|�|j�\}}}d|_|dk rv|dk rv||f|j|j<|rv|� �rv|j||f|_|j |j � �t |p�d|p�d�|j�}|�d|j�|jr�|�d|j�|jj|f|�}|j�|�|S)N�r3r8)� status_coder"� urllib_parseZurlparserr>r/r%r$rAZcontent�rawZ release_connr�requestr9� warn_on_401�save_credentialsZ connection�send�history�append) r&�resp�kwargsZparsedrrZsaver;Znew_resprrrr:�s(     zMultiDomainBasicAuth.handle_401cKs|jdkrt�d|jj�dS)z6Response callback to warn about incorrect credentials.rBz)401 Error, Credentials not correct for %sN)rCrrrFr)r&rLrMrrrrGs  �z MultiDomainBasicAuth.warn_on_401cKsntdk std��tsdS|j}d|_|rj|jdkrjzt�d�tj|�Wntk rht�d�YnXdS)z1Response callback to save credentials on success.Nz'should never reach here without keyringi�zSaving credentials to keyringzFailed to save credentials) rr5r%rCr�infoZ set_passwordrZ exception)r&rLrMZcredsrrrrHs z%MultiDomainBasicAuth.save_credentials)TN)TT) �__name__� __module__� __qualname__r'r-r1r7r<r>rAr:rGrHrrrrr!Ks � 2( -r!)&�__doc__ZloggingZpip._vendor.requests.authrrZpip._vendor.requests.utilsrZpip._vendor.six.moves.urllibrrDZpip._internal.utils.miscrrrr r Zpip._internal.utils.typingr Zoptparser �typingr rrZ pip._internal.vcs.versioncontrolrrZ CredentialsZ getLoggerrOrr� ImportErrorrrrr r!rrrr�s0        �