
R\                 @   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
 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)extract_cookies_to_jar)parse_dict_headerz!application/x-www-form-urlencodedzmultipart/form-datac             C   s0   d t  d |  | f j d   j   j d  S)zReturns a Basic Auth string.zBasic z%s:%slatin1)r   encodestripdecode)usernamepassword r   </var/www/dbchiro/venv/build/pip/pip/_vendor/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__%   s    zAuthBase.__call__N)__name__
__module____qualname____doc__r   r   r   r   r   r   "   s   r   c               @   s.   e  Z d  Z 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__+   s    	zHTTPBasicAuth.__init__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   )   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   6   s    zHTTPProxyAuth.__call__N)r   r   r   r   r   r   r   r   r   r   4   s   r   c               @   sF   e  Z d  Z 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:   | |  _  | |  _ d |  _ d |  _ i  |  _ d  |  _ d  S)N r   )r   r   
last_noncenonce_countchalpos)r   r   r   r   r   r   r   =   s    					zHTTPDigestAuth.__init__c                s6  |  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 d	   }	 |	   n! | d
 k r d d   }
 |
   n    f d d   }   d  k r d  Sd  } t |  } | j } | j r| d | j 7} n  d |  j | |  j f } d | | f }   |  }   |  } | |  j k r{|  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  } d | | | | | f } | d k rH  d | | | f  } n  | d  k rp| | 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) Nrealmnonceqop	algorithmopaqueMD5zMD5-SESSc             S   s4   t  |  t  r! |  j d  }  n  t j |   j   S)Nzutf-8)
isinstancer   r	   hashlibmd5	hexdigest)xr   r   r   md5_utf8S   s    z4HTTPDigestAuth.build_digest_header.<locals>.md5_utf8SHAc             S   s4   t  |  t  r! |  j d  }  n  t j |   j   S)Nzutf-8)r+   r   r	   r,   sha1r.   )r/   r   r   r   sha_utf8Y   s    z4HTTPDigestAuth.build_digest_header.<locals>.sha_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      z%s:%s:%s:%s:%sauth,z>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)r#   getupperr   pathqueryr   r   r!   r"   r   r	   timectimeosurandomr,   r2   r.   split)r   methodurlr%   r&   r'   r(   r)   
_algorithmr0   r3   KDentdigp_parsedr?   A1A2HA1HA2ncvaluer4   cnoncenoncebitrespdigbaser   )r6   r   build_digest_headerE   sn    						!	z"HTTPDigestAuth.build_digest_headerc             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)z:Takes the given response and tries digest-auth, if needed.Nnum_401_callsr   zwww-authenticater    digest   zdigest flagscountr   )r$   requestbodyseekgetattrr   r=   lowersetattrrecompile
IGNORECASEr   subr#   contentrawrelease_conncopyr   _cookiesprepare_cookiesrU   rF   rG   
connectionsendhistoryappend)r   r   kwargsrV   s_authpatprep_rr   r   r   
handle_401   s*    !	zHTTPDigestAuth.handle_401c             C   sm   |  j  r+ |  j | j | j  | j d <n  y | j j   |  _ Wn t k
 rU Yn X| j	 d |  j
  | S)Nr   response)r!   rU   rF   rG   r   r\   tellr$   AttributeErrorregister_hookrt   )r   r   r   r   r   r      s    	"zHTTPDigestAuth.__call__N)r   r   r   r   r   rU   rt   r   r   r   r   r   r   ;   s
   S#r   )r   rC   ra   rA   r,   loggingbase64r   compatr   r   cookiesr   utilsr   	getLoggerr   logCONTENT_TYPE_FORM_URLENCODEDCONTENT_TYPE_MULTI_PARTr   objectr   r   r   r   r   r   r   r   <module>   s"   