U
    å7Úco+  ã                
   @   s  d Z ddl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ršddlmZ dd	lmZmZmZ dd
lmZ eeeef Ze e¡ZzddlZW nL e k
rÈ   dZY n6 e!k
rü Z" ze #dee"ƒ¡ dZW 5 dZ"["X Y nX dd„ Z$G dd„ deƒZ%dS )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              
   C   sÄ   | rt sdS z€z
t j}W n tk
r,   Y n4X t d| ¡ || |ƒ}|dk	rZ|j|jfW S W dS |rŠt d| ¡ t  | |¡}|rŠ||fW S W n2 tk
r¾ } zt 	dt
|ƒ¡ W 5 d}~X Y nX dS )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ÚpasswordÚget_passwordÚ	ExceptionÚwarningÚstr)Úurlr   r   Úcredr   Úexc© r    ú>/tmp/pip-unpacked-wheel-xvghy_sv/pip/_internal/network/auth.pyÚget_keyring_auth.   s,    

 ÿr"   c                   @   s`   e Zd Zddd„Zdd„ Zddd„Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚMultiDomainBasicAuthTNc                 C   s   || _ || _i | _d | _d S )N)Ú	promptingÚ
index_urlsÚ	passwordsÚ_credentials_to_save)Úselfr$   r%   r    r    r!   Ú__init__M   s    zMultiDomainBasicAuth.__init__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_urlY   s    


z#MultiDomainBasicAuth._get_index_urlc                 C   sò   t |ƒ\}}}|\}}|dk	r6|dk	r6t d|¡ |S |  |¡}	|	rft |	ƒ}
|
rf|
\}	}}t d|	¡ |	rž|d dk	rž|\}}|dk	rž|dk	ržt d|¡ |S |r¾t|ƒ}|r¾t d|¡ |S |rêt|	|ƒpÔt||ƒ}|rêt 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   r/   r   r"   )r(   Úoriginal_urlÚallow_netrcÚallow_keyringr   ÚnetlocÚurl_user_passwordr   r   Ú	index_urlÚ
index_infoÚ_Úindex_url_user_passwordÚ
netrc_authÚkr_authr    r    r!   Ú_get_new_credentialsn   s>    ÿ



þz)MultiDomainBasicAuth._get_new_credentialsc                 C   s¤   t |ƒ\}}}| j |d¡\}}|dkr>|dkr>|  |¡\}}|dk	sN|dk	rl|pTd}|p\d}||f| j|< |dk	r||dk	sš|dkrŒ|dksšt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.
        ©NNNÚ z'Could not load credentials from url: {})r
   r&   Úgetr;   ÚAssertionErrorÚformat)r(   r0   r   r3   r7   r   r   r    r    r!   Ú_get_url_and_credentials    s&    
þþüüûz-MultiDomainBasicAuth._get_url_and_credentialsc                 C   sH   |   |j¡\}}}||_|d k	r6|d k	r6t||ƒ|ƒ}| d| j¡ |S )NÚresponse)rA   r   r   Úregister_hookÚ
handle_401)r(   Úreqr   r   r   r    r    r!   Ú__call__È   s    zMultiDomainBasicAuth.__call__c                 C   sF   t d| ƒ}|sdS t||ƒ}|r4|d |d dfS tdƒ}||dfS )NzUser for %s: r<   r   é   Fz
Password: T)r   r"   r   )r(   r3   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_keyringc           	      K   sà   |j dkr|S | js|S t |j¡}|  |j¡\}}}d | _|d k	rv|d k	rv||f| j|j< |rv|  	¡ rv|j||f| _|j
 |j ¡  t|pŽd|p”dƒ|jƒ}| d| j¡ | jrÀ| d| j¡ |jj|f|Ž}|j |¡ |S )Né‘  r=   rB   )Ústatus_coder$   Úurllib_parseÚurlparser   rI   r3   r'   r&   rL   ÚcontentÚrawÚrelease_connr   ÚrequestrC   Úwarn_on_401Úsave_credentialsÚ
connectionÚsendÚhistoryÚappend)	r(   ÚrespÚkwargsÚparsedr   r   ÚsaverE   Únew_respr    r    r!   rD   é   s(    

zMultiDomainBasicAuth.handle_401c                 K   s   |j dkrt d|jj¡ dS )z6Response callback to warn about incorrect credentials.rM   z)401 Error, Credentials not correct for %sN)rN   r   r   rT   r   )r(   r[   r\   r    r    r!   rU     s
    
 ÿz MultiDomainBasicAuth.warn_on_401c                 K   sn   t dk	stdƒ‚t sdS | j}d| _|rj|jdk rjzt d¡ t j|Ž  W n tk
rh   t d¡ Y nX 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?   r'   rN   r   ÚinfoÚset_passwordr   Ú	exception)r(   r[   r\   Úcredsr    r    r!   rV     s    
z%MultiDomainBasicAuth.save_credentials)TN)TT)Ú__name__Ú
__module__Ú__qualname__r)   r/   r;   rA   rF   rI   rL   rD   rU   rV   r    r    r    r!   r#   K   s   
  ÿ
2(-r#   )&Ú__doc__ÚloggingZpip._vendor.requests.authr   r   Zpip._vendor.requests.utilsr   Zpip._vendor.six.moves.urllibr   rO   Úpip._internal.utils.miscr   r   r   r	   r
   Úpip._internal.utils.typingr   Úoptparser   Útypingr   r   r   Ú pip._internal.vcs.versioncontrolr   r   ZCredentialsÚ	getLoggerrd   r   r   ÚImportErrorr   r   r   r"   r#   r    r    r    r!   Ú<module>   s0   	
 ÿ