
) X                 @   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 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)extract_cookies_to_jar)parse_dict_headerto_native_string)codesz!application/x-www-form-urlencodedzmultipart/form-datac             C   s3   d t  t d |  | f j d   j    } | S)zReturns a Basic Auth string.zBasic z%s:%slatin1)r   r   encodestrip)usernamepasswordZauthstr r   =/home/ubuntu/projects/ifolica/build/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__(   s    zAuthBase.__call__N)__name__
__module____qualname____doc__r   r   r   r   r   r   %   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__/   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__3   s    zHTTPBasicAuth.__eq__c             C   s   |  | k S)Nr   )r   r   r   r   r   __ne__9   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   ,   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   D   s    zHTTPProxyAuth.__call__N)r   r   r   r   r   r   r   r   r   r$   A   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   L   s    		zHTTPDigestAuth.__init__c             C   sa   t  |  j d  s] d |  j _ d |  j _ d |  j _ i  |  j _ d  |  j _ d  |  j _ n  d  S)NinitT r   )hasattrr(   r)   
last_noncenonce_countchalposnum_401_calls)r   r   r   r   init_per_thread_stateR   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    f d d   }   d k r d Sd } t |  } | j pd } | j r4| 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  } | d k rY  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 } | r
| d | 7} n  | r!| d | 7} n  | r8| d | 7} n  | rU| d  | | f 7} n  d! | S)"z
        :rtype: str
        realmnonceqop	algorithmopaqueN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_utf8n   s    z4HTTPDigestAuth.build_digest_header.<locals>.md5_utf8SHAc             S   s4   t  |  t  r! |  j d  }  n  t j |   j   S)Nzutf-8)r8   r   r   r9   sha1r;   )r<   r   r   r   sha_utf8t   s    z4HTTPDigestAuth.build_digest_header.<locals>.sha_utf8c                s     d |  | f  S)Nz%s:%sr   )sd)	hash_utf8r   r   <lambda>z   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)r(   r.   getupperr   pathqueryr   r   r,   r-   r   r   timectimeosurandomr9   r?   r;   split)r   methodurlr2   r3   r4   r5   r6   
_algorithmr=   r@   KDentdigp_parsedrM   A1A2HA1HA2ncvaluerA   cnoncerespdignoncebitbaser   )rC   r   build_digest_header\   sr    				!z"HTTPDigestAuth.build_digest_headerc             K   s   | j  r d |  j _ n  d S)z)Reset num_401_calls counter on redirects.r   N)Zis_redirectr(   r0   )r   r   kwargsr   r   r   handle_redirect   s    	zHTTPDigestAuth.handle_redirectc             K   s_  |  j  j d k	 r. | j j j |  j  j  n  | j j d d  } d | j   k rO|  j  j d k  rO|  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
        Nzwww-authenticater*   digest   r   zdigest flagscountr"   )r(   r/   requestbodyseekr#   rK   lowerr0   recompile
IGNORECASEr   subr.   contentclosecopyr   _cookiesrawprepare_cookiesrc   rT   rU   
connectionsendhistoryappend)r   r   rd   s_authpatprep_rr   r   r   
handle_401   s(    $$
	zHTTPDigestAuth.handle_401c             C   s   |  j    |  j j r8 |  j | j | j  | j d <n  y | j j   |  j _	 Wn t
 k
 rq d  |  j _	 Yn X| j d |  j  | j d |  j  d |  j _ | S)Nr"   responser   )r1   r(   r,   rc   rT   rU   r#   rk   tellr/   AttributeErrorregister_hookr   re   r0   )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!      s    zHTTPDigestAuth.__ne__N)r   r   r   r   r   r1   rc   re   r   r   r    r!   r   r   r   r   r%   I   s   
Z&r%   )r   rQ   rn   rO   r9   r&   base64r   compatr   r   cookiesr   utilsr   r   status_codesr	   CONTENT_TYPE_FORM_URLENCODEDCONTENT_TYPE_MULTI_PARTr   objectr   r   r$   r%   r   r   r   r   <module>   s"   
