ó
ñ¡Rc           @   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 m Z d d l m	 Z	 m
 Z
 d d l m Z d d l m Z e j e ƒ 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_headers!   application/x-www-form-urlencodeds   multipart/form-datac         C   s0   d t  d |  | f j d ƒ ƒ j ƒ  j d ƒ S(   s   Returns a Basic Auth string.s   Basic s   %s:%st   latin1(   R    t   encodet   stript   decode(   t   usernamet   password(    (    s1   /usr/lib/python2.7/dist-packages/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(    (    s1   /usr/lib/python2.7/dist-packages/requests/auth.pyt   __call__%   s    (   t   __name__t
   __module__t   __doc__R   (    (    (    s1   /usr/lib/python2.7/dist-packages/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
   (    (    s1   /usr/lib/python2.7/dist-packages/requests/auth.pyt   __init__+   s    	c         C   s    t  |  j |  j ƒ | j d <| S(   Nt   Authorization(   R   R	   R
   t   headers(   R   R   (    (    s1   /usr/lib/python2.7/dist-packages/requests/auth.pyR   /   s    (   R   R   R   R   R   (    (    (    s1   /usr/lib/python2.7/dist-packages/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   (    (    s1   /usr/lib/python2.7/dist-packages/requests/auth.pyR   6   s    (   R   R   R   R   (    (    (    s1   /usr/lib/python2.7/dist-packages/requests/auth.pyR   4   s   t   HTTPDigestAuthc           B   s2   e  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:   | |  _  | |  _ d |  _ d |  _ i  |  _ d  |  _ d  S(   Nt    i    (   R	   R
   t
   last_noncet   nonce_countt   chalt   Nonet   pos(   R   R	   R
   (    (    s1   /usr/lib/python2.7/dist-packages/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 } | j r| d | j 7} n  d |  j | |  j f } d | | f } ˆ  | ƒ } ˆ  | ƒ } | |  j	 k rr|  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 | | | | | f } | d k r9ˆ  d | | | f ƒ } n  | d  k ra| | d | | f ƒ } n7 | d k s‚d | j d ƒ k r”| | | ƒ } 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(    (    s1   /usr/lib/python2.7/dist-packages/requests/auth.pyt   md5_utf8S   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*   (    (    s1   /usr/lib/python2.7/dist-packages/requests/auth.pyt   sha_utf8Y   s    c            s   ˆ  d |  | f ƒ S(   Ns   %s:%s(    (   t   st   d(   t	   hash_utf8(    s1   /usr/lib/python2.7/dist-packages/requests/auth.pyt   <lambda>_   s    t   ?s   %s:%s:%ss   %s:%si   s   %08xs   utf-8i   i   s   %s:%s:%s:%s:%st   autht   ,s>   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'   R-   R)   t   split(   R   t   methodt   urlR    R!   R"   R#   R$   t
   _algorithmR+   R.   t   KDt   entdigt   p_parsedR8   t   A1t   A2t   HA1t   HA2t   ncvalueR/   t   cnoncet   noncebitt   respdigt   base(    (   R1   s1   /usr/lib/python2.7/dist-packages/requests/auth.pyt   build_digest_headerE   sn    								!	c         K   sk  |  j  d k	 r( | j j j |  j  ƒ n  t |  d d ƒ } | j j d d ƒ } d | j ƒ  k rW| d k  rWt	 |  d | d ƒ 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 | ƒ | | _ | St	 |  d d ƒ | S(   s:   Takes the given response and tries digest-auth, if needed.t   num_401_callsi   s   www-authenticateR   t   digesti   s   digest t   flagst   countR   N(   R   R   t   requestt   bodyt   seekt   getattrR   R6   t   lowert   setattrt   ret   compilet
   IGNORECASER   t   subR   t   contentt   rawt   release_connt   copyR   t   _cookiest   prepare_cookiesRN   R?   R@   t
   connectiont   sendt   historyt   append(   R   R   t   kwargsRO   t   s_autht   patt   prept   _r(    (    s1   /usr/lib/python2.7/dist-packages/requests/auth.pyt
   handle_401˜   s*    !	c         C   sl   |  j  r+ |  j | j | j ƒ | j d <n  y | j j ƒ  |  _ Wn t k
 rT n X| j	 d |  j
 ƒ | S(   NR   t   response(   R   RN   R?   R@   R   RT   t   tellR   t   AttributeErrort   register_hookRl   (   R   R   (    (    s1   /usr/lib/python2.7/dist-packages/requests/auth.pyR   »   s    	"(   R   R   R   R   RN   Rl   R   (    (    (    s1   /usr/lib/python2.7/dist-packages/requests/auth.pyR   ;   s
   		S	#(   R   R<   RY   R:   R'   t   loggingt   base64R    t   compatR   R   t   cookiesR   t   utilsR   t	   getLoggerR   t   logt   CONTENT_TYPE_FORM_URLENCODEDt   CONTENT_TYPE_MULTI_PARTR   t   objectR   R   R   R   (    (    (    s1   /usr/lib/python2.7/dist-packages/requests/auth.pyt   <module>   s"   	