ó
j·Ã^c           @   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 „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s]   
requests.auth
~~~~~~~~~~~~~

This module contains the authentication handlers for Requests.
iÿÿÿÿN(   t	   b64encodei   (   t   urlparset   strt
   basestring(   t   extract_cookies_to_jar(   t   to_native_string(   t   parse_dict_headers!   application/x-www-form-urlencodeds   multipart/form-datac         C   së   t  |  t ƒ s: t j d j |  ƒ d t ƒt |  ƒ }  n  t  | t ƒ sz t j d j t | ƒ ƒ d t ƒt | ƒ } n  t  |  t ƒ r› |  j d ƒ }  n  t  | t ƒ r¼ | j d ƒ } n  d t	 t
 d j |  | f ƒ ƒ j ƒ  ƒ } | S(   s   Returns a Basic Auth string.s·   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.t   categorys·   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.t   latin1s   Basic t   :(   t
   isinstanceR   t   warningst   warnt   formatt   DeprecationWarningR   t   typet   encodeR   R    t   joint   strip(   t   usernamet   passwordt   authstr(    (    s*   site-packages/pip/_vendor/requests/auth.pyt   _basic_auth_str   s&    
	%t   AuthBasec           B   s   e  Z d  Z d „  Z RS(   s4   Base class that all auth implementations derive fromc         C   s   t  d ƒ ‚ d  S(   Ns   Auth hooks must be callable.(   t   NotImplementedError(   t   selft   r(    (    s*   site-packages/pip/_vendor/requests/auth.pyt   __call__K   s    (   t   __name__t
   __module__t   __doc__R   (    (    (    s*   site-packages/pip/_vendor/requests/auth.pyR   H   s   t   HTTPBasicAuthc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s?   Attaches HTTP Basic Authentication to the given Request object.c         C   s   | |  _  | |  _ d  S(   N(   R   R   (   R   R   R   (    (    s*   site-packages/pip/_vendor/requests/auth.pyt   __init__R   s    	c         C   s:   t  |  j t | d d  ƒ k |  j t | d d  ƒ k g ƒ S(   NR   R   (   t   allR   t   getattrt   NoneR   (   R   t   other(    (    s*   site-packages/pip/_vendor/requests/auth.pyt   __eq__V   s    c         C   s   |  | k S(   N(    (   R   R$   (    (    s*   site-packages/pip/_vendor/requests/auth.pyt   __ne__\   s    c         C   s    t  |  j |  j ƒ | j d <| S(   Nt   Authorization(   R   R   R   t   headers(   R   R   (    (    s*   site-packages/pip/_vendor/requests/auth.pyR   _   s    (   R   R   R   R    R%   R&   R   (    (    (    s*   site-packages/pip/_vendor/requests/auth.pyR   O   s
   			t   HTTPProxyAuthc           B   s   e  Z d  Z d „  Z RS(   s=   Attaches HTTP Proxy Authentication to a given Request object.c         C   s    t  |  j |  j ƒ | j d <| S(   Ns   Proxy-Authorization(   R   R   R   R(   (   R   R   (    (    s*   site-packages/pip/_vendor/requests/auth.pyR   g   s    (   R   R   R   R   (    (    (    s*   site-packages/pip/_vendor/requests/auth.pyR)   d   s   t   HTTPDigestAuthc           B   sV   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   s@   Attaches HTTP Digest Authentication to the given Request object.c         C   s%   | |  _  | |  _ t j ƒ  |  _ d  S(   N(   R   R   t	   threadingt   localt   _thread_local(   R   R   R   (    (    s*   site-packages/pip/_vendor/requests/auth.pyR    o   s    		c         C   sa   t  |  j d ƒ s] t |  j _ d |  j _ d |  j _ i  |  j _ d  |  j _ d  |  j _	 n  d  S(   Nt   initt    i    (
   t   hasattrR-   t   TrueR.   t
   last_noncet   nonce_countt   chalR#   t   post   num_401_calls(   R   (    (    s*   site-packages/pip/_vendor/requests/auth.pyt   init_per_thread_stateu   s    c            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 „  }	 |	 ‰  nZ | d	 k rÎ d
 „  }
 |
 ‰  n< | d k rì d „  } | ‰  n | d k r
d „  } | ‰  n  ‡  f d †  } ˆ  d" k r)d" Sd" } t | ƒ } | j pGd } | j rg| d | j 7} n  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 k r†ˆ  d | | | f ƒ } n  | s¨| | d | | f ƒ } nP | d k sÉd | j d ƒ k rôd | | | d | f } | | | ƒ } n d" S| |  j  _
 d |  j | | | | f } | r7| d | 7} n  | rN| d | 7} n  | re| d | 7} n  | r‚| d  | | f 7} n  d! | S(#   s   
        :rtype: str
        t   realmt   noncet   qopt	   algorithmt   opaquet   MD5s   MD5-SESSc         S   s4   t  |  t ƒ r! |  j d ƒ }  n  t j |  ƒ j ƒ  S(   Ns   utf-8(   R
   R   R   t   hashlibt   md5t	   hexdigest(   t   x(    (    s*   site-packages/pip/_vendor/requests/auth.pyt   md5_utf8‘   s    t   SHAc         S   s4   t  |  t ƒ r! |  j d ƒ }  n  t j |  ƒ j ƒ  S(   Ns   utf-8(   R
   R   R   R>   t   sha1R@   (   RA   (    (    s*   site-packages/pip/_vendor/requests/auth.pyt   sha_utf8—   s    s   SHA-256c         S   s4   t  |  t ƒ r! |  j d ƒ }  n  t j |  ƒ j ƒ  S(   Ns   utf-8(   R
   R   R   R>   t   sha256R@   (   RA   (    (    s*   site-packages/pip/_vendor/requests/auth.pyt   sha256_utf8   s    s   SHA-512c         S   s4   t  |  t ƒ r! |  j d ƒ }  n  t j |  ƒ j ƒ  S(   Ns   utf-8(   R
   R   R   R>   t   sha512R@   (   RA   (    (    s*   site-packages/pip/_vendor/requests/auth.pyt   sha512_utf8£   s    c            s   ˆ  d |  | f ƒ S(   Ns   %s:%s(    (   t   st   d(   t	   hash_utf8(    s*   site-packages/pip/_vendor/requests/auth.pyt   <lambda>©   s    t   /t   ?s   %s:%s:%ss   %s:%si   s   %08xs   utf-8i   i   t   autht   ,s   %s:%s:%s:%s:%ss>   username="%s", realm="%s", nonce="%s", uri="%s", response="%s"s   , opaque="%s"s   , algorithm="%s"s   , digest="%s"s    , qop="auth", nc=%s, cnonce="%s"s	   Digest %sN(   R-   R4   t   getR#   t   upperR   t   patht   queryR   R   R2   R3   R   R   t   timet   ctimet   ost   urandomR>   RD   R@   t   split(   R   t   methodt   urlR8   R9   R:   R;   R<   t
   _algorithmRB   RE   RG   RI   t   KDt   entdigt   p_parsedRT   t   A1t   A2t   HA1t   HA2t   ncvalueRJ   t   cnoncet   respdigt   noncebitt   base(    (   RL   s*   site-packages/pip/_vendor/requests/auth.pyt   build_digest_header   s~    										!c         K   s   | j  r d |  j _ n  d S(   s)   Reset num_401_calls counter on redirects.i   N(   t   is_redirectR-   R6   (   R   R   t   kwargs(    (    s*   site-packages/pip/_vendor/requests/auth.pyt   handle_redirectå   s    	c         K   sŽ  d | j  k o d k  n s/ d |  j _ | S|  j j d k	 r] | j j j |  j j ƒ n  | 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(   so   
        Takes the given response and tries digest-auth, if needed.

        :rtype: requests.Response
        i  iô  i   s   www-authenticateR/   t   digesti   s   digest t   flagst   countR'   N(   t   status_codeR-   R6   R5   R#   t   requestt   bodyt   seekR(   RR   t   lowert   ret   compilet
   IGNORECASER   t   subR4   t   contentt   closet   copyR   t   _cookiest   rawt   prepare_cookiesRj   R[   R\   t
   connectiont   sendt   historyt   append(   R   R   Rl   t   s_autht   patt   prept   _r(    (    s*   site-packages/pip/_vendor/requests/auth.pyt
   handle_401ê   s.    	$$
	c         C   s§   |  j  ƒ  |  j j r8 |  j | j | j ƒ | j d <n  y | j j ƒ  |  j _	 Wn t
 k
 rp d  |  j _	 n X| j d |  j ƒ | j d |  j ƒ d |  j _ | S(   NR'   t   responsei   (   R7   R-   R2   Rj   R[   R\   R(   Rs   t   tellR5   t   AttributeErrorR#   t   register_hookRˆ   Rm   R6   (   R   R   (    (    s*   site-packages/pip/_vendor/requests/auth.pyR     s    
"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$   (    (    s*   site-packages/pip/_vendor/requests/auth.pyR%   *  s    c         C   s   |  | k S(   N(    (   R   R$   (    (    s*   site-packages/pip/_vendor/requests/auth.pyR&   0  s    (   R   R   R   R    R7   Rj   Rm   Rˆ   R   R%   R&   (    (    (    s*   site-packages/pip/_vendor/requests/auth.pyR*   l   s   		
	f		,		(   R   RX   Rv   RV   R>   R+   R   t   base64R    t   compatR   R   R   t   cookiesR   t   _internal_utilsR   t   utilsR   t   CONTENT_TYPE_FORM_URLENCODEDt   CONTENT_TYPE_MULTI_PARTR   t   objectR   R   R)   R*   (    (    (    s*   site-packages/pip/_vendor/requests/auth.pyt   <module>   s$   	,