ó
¨9Vc           @   s  d  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	 d d l
 m Z d d l m Z 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   str(   t   extract_cookies_to_jar(   t   parse_dict_headert   to_native_string(   t   codess!   application/x-www-form-urlencodeds   multipart/form-datac         C   s3   d t  t d |  | f j d ƒ ƒ j ƒ  ƒ } | S(   s   Returns a Basic Auth string.s   Basic s   %s:%st   latin1(   R   R    t   encodet   strip(   t   usernamet   passwordt   authstr(    (    s6   /tmp/pip-build-5Z5nTX/pip/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(    (    s6   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/auth.pyt   __call__'   s    (   t   __name__t
   __module__t   __doc__R   (    (    (    s6   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/auth.pyR   $   s   t   HTTPBasicAuthc           B   s    e  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   (    (    s6   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/auth.pyt   __init__-   s    	c         C   s    t  |  j |  j ƒ | j d <| S(   Nt   Authorization(   R   R
   R   t   headers(   R   R   (    (    s6   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/auth.pyR   1   s    (   R   R   R   R   R   (    (    (    s6   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/auth.pyR   +   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   (    (    s6   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/auth.pyR   8   s    (   R   R   R   R   (    (    (    s6   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/auth.pyR   6   s   t   HTTPDigestAuthc           B   s;   e  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   sC   | |  _  | |  _ d |  _ d |  _ i  |  _ d  |  _ d |  _ d  S(   Nt    i    i   (   R
   R   t
   last_noncet   nonce_countt   chalt   Nonet   post   num_401_calls(   R   R
   R   (    (    s6   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/auth.pyR   ?   s    						c      	      s-  |  j  d } |  j  d } |  j  j d ƒ } |  j  j d ƒ } |  j  j d ƒ } | d  k re d } n | j ƒ  } | d k s‰ | d k r› d „  }	 |	 ‰  n | d	 k r¹ d
 „  }
 |
 ‰  n  ‡  f d †  } ˆ  d  k rØ d  Sd  } t | ƒ } | j pö d } | j r| d | j 7} n  d |  j | |  j f } d | | f } ˆ  | ƒ } ˆ  | ƒ } | |  j	 k rx|  j
 d 7_
 n	 d |  _
 d |  j
 } t |  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  | d  k rN| | d | | f ƒ } nP | d k sod | j d ƒ k ršd | | | d | f } | | | ƒ } n d  S| |  _	 d |  j | | | | f } | rÚ| d | 7} n  | rñ| d | 7} n  | r| d | 7} n  | r%| d | | f 7} n  d | S(   Nt   realmt   noncet   qopt	   algorithmt   opaquet   MD5s   MD5-SESSc         S   s4   t  |  t ƒ r! |  j d ƒ }  n  t j |  ƒ j ƒ  S(   Ns   utf-8(   t
   isinstanceR   R   t   hashlibt   md5t	   hexdigest(   t   x(    (    s6   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/auth.pyt   md5_utf8V   s    t   SHAc         S   s4   t  |  t ƒ r! |  j d ƒ }  n  t j |  ƒ j ƒ  S(   Ns   utf-8(   R)   R   R   R*   t   sha1R,   (   R-   (    (    s6   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/auth.pyt   sha_utf8\   s    c            s   ˆ  d |  | f ƒ S(   Ns   %s:%s(    (   t   st   d(   t	   hash_utf8(    s6   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/auth.pyt   <lambda>b   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 %s(   R   t   getR    t   upperR   t   patht   queryR
   R   R   R   R   R   t   timet   ctimet   ost   urandomR*   R0   R,   t   split(   R   t   methodt   urlR#   R$   R%   R&   R'   t
   _algorithmR.   R1   t   KDt   entdigt   p_parsedR<   t   A1t   A2t   HA1t   HA2t   ncvalueR2   t   cnoncet   respdigt   noncebitt   base(    (   R4   s6   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/auth.pyt   build_digest_headerH   sp    							!	c         K   s   | j  r d |  _ n  d S(   s)   Reset num_401_calls counter on redirects.i   N(   t   is_redirectR"   (   R   R   t   kwargs(    (    s6   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/auth.pyt   handle_redirectž   s    	c         K   s_  |  j  d k	 r( | j j j |  j  ƒ n  t |  d d ƒ } | j j d d ƒ } d | j ƒ  k rR| d k  rR|  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 |  _	 | S(   s:   Takes the given response and tries digest-auth, if needed.R"   i   s   www-authenticateR   t   digesti   s   digest t   flagst   countR   N(   R!   R    t   requestt   bodyt   seekt   getattrR   R:   t   lowerR"   t   ret   compilet
   IGNORECASER   t   subR   t   contentt   rawt   release_connt   copyR   t   _cookiest   prepare_cookiesRR   RC   RD   t
   connectiont   sendt   historyt   append(   R   R   RT   R"   t   s_autht   patt   prept   _r(    (    s6   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/auth.pyt
   handle_401£   s*    !		c         C   sˆ   |  j  r+ |  j | j | j ƒ | j d <n  y | j j ƒ  |  _ Wn t k
 r] d  |  _ n X| j
 d |  j ƒ | j
 d |  j ƒ | S(   NR   t   response(   R   RR   RC   RD   R   RZ   t   tellR!   t   AttributeErrorR    t   register_hookRp   RU   (   R   R   (    (    s6   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/auth.pyR   Æ   s    	"(   R   R   R   R   RR   RU   Rp   R   (    (    (    s6   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/auth.pyR   =   s   			V		#(   R   R@   R^   R>   R*   t   base64R    t   compatR   R   t   cookiesR   t   utilsR   R   t   status_codesR   t   CONTENT_TYPE_FORM_URLENCODEDt   CONTENT_TYPE_MULTI_PARTR   t   objectR   R   R   R   (    (    (    s6   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/auth.pyt   <module>   s    	
