a
    ”˜ëjz  ã                   @   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|i |¤Ž d| _|  ¡  d S )Nr   )Úsuperr   Ú__init__Ústale_rejectsÚinit_per_thread_state)ÚselfÚargsÚkwargs©Ú	__class__© úg/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/requests_toolbelt/auth/http_proxy_digest.pyr	      s    zHTTPProxyDigestAuth.__init__c                 C   s    t | dd ƒ}|d u 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 u r|| _n||_d S r   r   )r   Úvaluer   r   r   r   r
   "   s    c                    s*   zt t| ƒ ¡  W n ty$   Y n0 d S )N)r   r   r   ÚAttributeError)r   r   r   r   r   *   s    z)HTTPProxyDigestAuth.init_per_thread_statec                 K   s2  |j dk�r*| jdk �r*|j d¡}|du r6tdƒ‚n| j |¡sF|S t | jj	d|dd�¡| _
d	|jjv r²d
| j
v r²| j
d
  ¡ dkr˜|  jd7  _n| j
d
  ¡ dkr²tdƒ‚|j | ¡  |j ¡ }t |j|j|j¡ | |j¡ |  |j|j¡|jd	< |jj|fi |¤Ž}|j |¡ ||_|S |S dS )z’Handle 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   