U
    H4fz                     @   s>   d Z ddlZddlmZmZ ddlmZ G dd dejZ	dS )z*The module containing HTTPProxyDigestAuth.    N)cookiesutils   )_digest_auth_compatc                       sf   e Zd ZdZejdejdZ fddZe	dd Z
e
jdd Z
 fd	d
Zdd Zdd Z  ZS )HTTPProxyDigestAuthaO  HTTP digest authentication between proxy

    :param stale_rejects: The number of rejects indicate that:
        the client may wish to simply retry the request
        with a new encrypted response, without reprompting the user for a
        new username and password. i.e., retry build_digest_header
    :type stale_rejects: int
    zdigest )flagsc                    s$   t t| j|| d| _|   d S )Nr   )superr   __init__stale_rejectsinit_per_thread_state)selfargskwargs	__class__ L/tmp/pip-unpacked-wheel-_z368jfd/requests_toolbelt/auth/http_proxy_digest.pyr	      s    zHTTPProxyDigestAuth.__init__c                 C   s    t | dd }|d kr| jS |jS N_thread_localgetattrZ_stale_rejectsr
   )r   thread_localr   r   r   r
      s    z!HTTPProxyDigestAuth.stale_rejectsc                 C   s&   t | dd }|d kr|| _n||_d S r   r   )r   valuer   r   r   r   r
   "   s    c                    s,   zt t|   W n tk
r&   Y nX d S )N)r   r   r   AttributeError)r   r   r   r   r   *   s    z)HTTPProxyDigestAuth.init_per_thread_statec                 K   s.  |j dkr&| jdk r&|jd}|dkr6tdn| j|sF|S t| jj	d|dd| _
d	|jjkrd
| j
kr| j
d
  dkr|  jd7  _n| j
d
  dkrtd|j |  |j }t|j|j|j ||j | |j|j|jd	< |jj|f|}|j| ||_|S |S dS )zHandle HTTP 407 only once, otherwise give up

        :param r: current response
        :returns: responses, along with the new response
        i     zproxy-authenticateNzRproxy server violated RFC 7235:407 response MUST contain header proxy-authenticate r   )countProxy-AuthorizationstaletruefalsezUser or password is invalid)status_coder
   headersgetIOError_patmatchr   parse_dict_headersubchalrequestlowercontentclosecopyr   extract_cookies_to_jar_cookiesrawprepare_cookiesbuild_digest_headermethodurl
connectionsendhistoryappend)r   rr   s_authprep_rr   r   r   
handle_4071   s@    
 
zHTTPProxyDigestAuth.handle_407c                 C   s6   |    | jr$| |j|j|jd< |d| j |S )Nr   response)r   
last_noncer3   r4   r5   r"   register_hookr>   )r   r:   r   r   r   __call___   s     
zHTTPProxyDigestAuth.__call__)__name__
__module____qualname____doc__recompile
IGNORECASEr%   r	   propertyr
   setterr   r>   rB   __classcell__r   r   r   r   r   
   s   

.r   )
rF   rG   requestsr   r   r   r   authHTTPDigestAuthr   r   r   r   r   <module>   s   