
º|jß'  ã               @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z d d l	 m
 Z
 m Z m Z d d l m Z d d l m Z d d l m Z d	 Z d
 Z d d „  Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z d S)z]
requests.auth
~~~~~~~~~~~~~

This module contains the authentication handlers for Requests.
é    N)Ú	b64encodeé   )ÚurlparseÚstrÚ
basestring)Úextract_cookies_to_jar)Úto_native_string)Úparse_dict_headerz!application/x-www-form-urlencodedzmultipart/form-datac             C   sß   t  |  t ƒ s7 t j d j |  ƒ d t ƒt |  ƒ }  t  | t ƒ st t j d j t | ƒ ƒ d t ƒt | ƒ } t  |  t ƒ r’ |  j d ƒ }  t  | t ƒ r° | j d ƒ } d t	 t
 d j |  | f ƒ ƒ j ƒ  ƒ } | S)zReturns a Basic Auth string.z·Non-string usernames will no longer be supported in Requests 3.0.0. Please convert the object you've passed in ({!r}) to a string or bytes object in the near future to avoid problems.Úcategoryz·Non-string passwords will no longer be supported in Requests 3.0.0. Please convert the object you've passed in ({!r}) to a string or bytes object in the near future to avoid problems.Úlatin1zBasic ó   :)Ú
isinstancer   ÚwarningsÚwarnÚformatÚDeprecationWarningr   ÚtypeÚencoder   r   ÚjoinÚstrip)ÚusernameÚpasswordÚauthstr© r   ú1/tmp/pip-build-1hwfuv5s/requests/requests/auth.pyÚ_basic_auth_str   s&    
	%r   c               @   s"   e  Z d  Z d Z d d „  Z d S)ÚAuthBasez4Base class that all auth implementations derive fromc             C   s   t  d ƒ ‚ d  S)NzAuth hooks must be callable.)ÚNotImplementedError)ÚselfÚrr   r   r   Ú__call__K   s    zAuthBase.__call__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    r   r   r   r   r   H   s   r   c               @   sF   e  Z d  Z d Z d d „  Z d d „  Z d d „  Z d d	 „  Z d
 S)ÚHTTPBasicAuthz?Attaches HTTP Basic Authentication to the given Request object.c             C   s   | |  _  | |  _ d  S)N)r   r   )r   r   r   r   r   r   Ú__init__R   s    	zHTTPBasicAuth.__init__c             C   s:   t  |  j t | d d  ƒ k |  j t | d d  ƒ k g ƒ S)Nr   r   )Úallr   Úgetattrr   )r   Úotherr   r   r   Ú__eq__V   s    zHTTPBasicAuth.__eq__c             C   s   |  | k S)Nr   )r   r)   r   r   r   Ú__ne__\   s    zHTTPBasicAuth.__ne__c             C   s    t  |  j |  j ƒ | j d <| S)NÚAuthorization)r   r   r   Úheaders)r   r   r   r   r   r    _   s    zHTTPBasicAuth.__call__N)r!   r"   r#   r$   r&   r*   r+   r    r   r   r   r   r%   O   s
   r%   c               @   s"   e  Z d  Z d Z d d „  Z d S)ÚHTTPProxyAuthz=Attaches HTTP Proxy Authentication to a given Request object.c             C   s    t  |  j |  j ƒ | j d <| S)NzProxy-Authorization)r   r   r   r-   )r   r   r   r   r   r    g   s    zHTTPProxyAuth.__call__N)r!   r"   r#   r$   r    r   r   r   r   r.   d   s   r.   c               @   sv   e  Z d  Z d Z 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)ÚHTTPDigestAuthz@Attaches HTTP Digest Authentication to the given Request object.c             C   s%   | |  _  | |  _ t j ƒ  |  _ d  S)N)r   r   Ú	threadingÚlocalÚ_thread_local)r   r   r   r   r   r   r&   o   s    		zHTTPDigestAuth.__init__c             C   s^   t  |  j d ƒ sZ d |  j _ d |  j _ d |  j _ i  |  j _ d  |  j _ d  |  j _ d  S)NÚinitTÚ r   )Úhasattrr2   r3   Ú
last_nonceÚnonce_countÚchalÚposÚnum_401_calls)r   r   r   r   Úinit_per_thread_stateu   s    z$HTTPDigestAuth.init_per_thread_statec                sŠ  |  j  j d } |  j  j d } |  j  j j d ƒ } |  j  j j d ƒ } |  j  j j d ƒ } d ‰  | d k rz d } n | j ƒ  } | d k sž | d k r³ d	 d
 „  }	 |	 ‰  n` | d k rÔ d d „  }
 |
 ‰  n? | d k rõ d d „  } | ‰  n | d k rd d „  } | ‰  ‡  f d d †  } ˆ  d k r5d Sd } t | ƒ } | j pSd } | j rp| d | j 7} d |  j | |  j f } d | | f } ˆ  | ƒ } ˆ  | ƒ } | |  j  j	 k rØ|  j  j
 d 7_
 n d |  j  _
 d |  j  j
 } t |  j  j
 ƒ j d ƒ } | | j d ƒ 7} | t j ƒ  j d ƒ 7} | t j d ƒ 7} t j | ƒ j ƒ  d d … } | d k r’ˆ  d | | | f ƒ } | s´| | d | | f ƒ } nP | d k sÕd | j d  ƒ k r d! | | | d | f } | | | ƒ } n d S| |  j  _	 d" |  j | | | | f } | r@| d# | 7} | rT| d$ | 7} | rh| d% | 7} | r‚| d& | | f 7} d' | S)(z
        :rtype: str
        ÚrealmÚnonceÚqopÚ	algorithmÚopaqueNÚMD5zMD5-SESSc             S   s1   t  |  t ƒ r |  j d ƒ }  t j |  ƒ j ƒ  S)Nzutf-8)r   r   r   ÚhashlibÚmd5Ú	hexdigest)Úxr   r   r   Úmd5_utf8‘   s    z4HTTPDigestAuth.build_digest_header.<locals>.md5_utf8ÚSHAc             S   s1   t  |  t ƒ r |  j d ƒ }  t j |  ƒ j ƒ  S)Nzutf-8)r   r   r   rB   Úsha1rD   )rE   r   r   r   Úsha_utf8—   s    z4HTTPDigestAuth.build_digest_header.<locals>.sha_utf8zSHA-256c             S   s1   t  |  t ƒ r |  j d ƒ }  t j |  ƒ j ƒ  S)Nzutf-8)r   r   r   rB   Úsha256rD   )rE   r   r   r   Úsha256_utf8�   s    z7HTTPDigestAuth.build_digest_header.<locals>.sha256_utf8zSHA-512c             S   s1   t  |  t ƒ r |  j d ƒ }  t j |  ƒ j ƒ  S)Nzutf-8)r   r   r   rB   Úsha512rD   )rE   r   r   r   Úsha512_utf8£   s    z7HTTPDigestAuth.build_digest_header.<locals>.sha512_utf8c                s   ˆ  d |  | f ƒ S)Nz%s:%sr   )ÚsÚd)Ú	hash_utf8r   r   Ú<lambda>©   s    z4HTTPDigestAuth.build_digest_header.<locals>.<lambda>ú/ú?z%s:%s:%sz%s:%sr   z%08xzutf-8é   é   Úauthú,z%s:%s:%s:%s:%sz>username="%s", realm="%s", nonce="%s", uri="%s", response="%s"z, opaque="%s"z, algorithm="%s"z, digest="%s"z , qop="auth", nc=%s, cnonce="%s"z	Digest %s)r2   r8   ÚgetÚupperr   ÚpathÚqueryr   r   r6   r7   r   r   ÚtimeÚctimeÚosÚurandomrB   rH   rD   Úsplit)r   ÚmethodÚurlr<   r=   r>   r?   r@   Ú
_algorithmrF   rI   rK   rM   ÚKDÚentdigÚp_parsedrZ   ÚA1ÚA2ÚHA1ÚHA2ÚncvaluerN   ÚcnonceÚrespdigÚnoncebitÚbaser   )rP   r   Úbuild_digest_header   s~    					!z"HTTPDigestAuth.build_digest_headerc             K   s   | j  r d |  j _ d S)z)Reset num_401_calls counter on redirects.r   N)Úis_redirectr2   r:   )r   r   Úkwargsr   r   r   Úhandle_redirectå   s    	zHTTPDigestAuth.handle_redirectc             K   s‹  d | j  k o d k  n s/ d |  j _ | S|  j j d k	 rZ | j j j |  j j ƒ | j j d d ƒ } d | j	 ƒ  k r{|  j j d k  r{|  j j d 7_ t
 j d	 d
 t
 j ƒ} t | j d | d d ƒƒ |  j _ | j | j ƒ  | j j ƒ  } t | j | j | j ƒ | j | j ƒ |  j | j | j ƒ | j d <| j j | | � } | j j | ƒ | | _ | Sd |  j _ | S)zo
        Takes the given response and tries digest-auth, if needed.

        :rtype: requests.Response
        i�  iô  r   Nzwww-authenticater4   Údigesté   zdigest ÚflagsÚcountr,   )Ústatus_coder2   r:   r9   ÚrequestÚbodyÚseekr-   rX   ÚlowerÚreÚcompileÚ
IGNORECASEr	   Úsubr8   ÚcontentÚcloseÚcopyr   Ú_cookiesÚrawÚprepare_cookiesrp   ra   rb   Ú
connectionÚsendÚhistoryÚappend)r   r   rr   Ús_authÚpatÚprepÚ_rr   r   r   Ú
handle_401ê   s.    	$$
	zHTTPDigestAuth.handle_401c             C   s¥   |  j  ƒ  |  j j r5 |  j | j | j ƒ | j d <y | j j ƒ  |  j _	 Wn t
 k
 rn d  |  j _	 Yn X| j d |  j ƒ | j d |  j ƒ d |  j _ | S)Nr,   Úresponser   )r;   r2   r6   rp   ra   rb   r-   rz   Útellr9   ÚAttributeErrorÚregister_hookr�   rs   r:   )r   r   r   r   r   r      s    
zHTTPDigestAuth.__call__c             C   s:   t  |  j t | d d  ƒ k |  j t | d d  ƒ k g ƒ S)Nr   r   )r'   r   r(   r   )r   r)   r   r   r   r*   *  s    zHTTPDigestAuth.__eq__c             C   s   |  | k S)Nr   )r   r)   r   r   r   r+   0  s    zHTTPDigestAuth.__ne__N)r!   r"   r#   r$   r&   r;   rp   rs   r�   r    r*   r+   r   r   r   r   r/   l   s   
f,r/   )r$   r^   r}   r\   rB   r0   r   Úbase64r   Úcompatr   r   r   Úcookiesr   Ú_internal_utilsr   Úutilsr	   ÚCONTENT_TYPE_FORM_URLENCODEDÚCONTENT_TYPE_MULTI_PARTr   Úobjectr   r%   r.   r/   r   r   r   r   Ú<module>   s$   ,