3
uAY &                 @   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d ZG dd deZG dd deZG dd deZG dd deZdS )z]
requests.auth
~~~~~~~~~~~~~

This module contains the authentication handlers for Requests.
    N)	b64encode   )urlparsestr
basestring)extract_cookies_to_jar)to_native_string)parse_dict_headerz!application/x-www-form-urlencodedzmultipart/form-datac             C   s   t | ts&tjdj| td t| } t |tsLtjdj|td t|}t | tr`| jd} t |trt|jd}dtt	dj
| |fj  }|S )zReturns a Basic Auth string.zNon-string usernames will no longer be supported in Requests 3.0.0. Please convert the object you've passed in ({0!r}) to a string or bytes object in the near future to avoid problems.)categoryzNon-string passwords will no longer be supported in Requests 3.0.0. Please convert the object you've passed in ({0!r}) to a string or bytes object in the near future to avoid problems.latin1zBasic    :)
isinstancer   warningswarnformatDeprecationWarningr   encoder   r   joinstrip)usernamepasswordZauthstr r   //usr/lib/python3/dist-packages/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__K   s    zAuthBase.__call__N)__name__
__module____qualname____doc__r   r   r   r   r   r   H   s   r   c               @   s0   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__R   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__V   s    zHTTPBasicAuth.__eq__c             C   s
   | |k S )Nr   )r   r'   r   r   r   __ne__\   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#   O   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   g   s    zHTTPProxyAuth.__call__N)r   r    r!   r"   r   r   r   r   r   r,   d   s   r,   c               @   sP   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   	threadingZlocal_thread_local)r   r   r   r   r   r   r$   o   s    zHTTPDigestAuth.__init__c             C   s@   t | jds<d| j_d| j_d| j_i | j_d | j_d | j_d S )NinitT r   )hasattrr/   r0   
last_noncenonce_countchalposnum_401_calls)r   r   r   r   init_per_thread_stateu   s    z$HTTPDigestAuth.init_per_thread_statec                sj  | j jd }| j jd }| j jjd}| j jjd}| j jjd}d |dkrTd}n|j }|dksl|dkrzd	d
 }	|	 n|dkrdd }
|
  fdd} dkrdS d}t|}|jpd}|jr|d|j 7 }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r d|||f }|s||d||f }n<|dksd|jdkrd|||d|f }|||}ndS || j _	d| j||||f }|r(|d| 7 }|r:|d| 7 }|rL|d| 7 }|rb|d ||f 7 }d!| S )"z
        :rtype: str
        realmnonceqop	algorithmopaqueNZMD5zMD5-SESSc             S   s"   t | tr| jd} tj| j S )Nzutf-8)r   r   r   hashlibZmd5	hexdigest)xr   r   r   md5_utf8   s    

z4HTTPDigestAuth.build_digest_header.<locals>.md5_utf8ZSHAc             S   s"   t | tr| jd} tj| j S )Nzutf-8)r   r   r   r>   sha1r?   )r@   r   r   r   sha_utf8   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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/   r5   getupperr   pathZqueryr   r   r3   r4   r   r   timeZctimeosurandomr>   rB   r?   split)r   methodurlr9   r:   r;   r<   r=   Z
_algorithmrA   rC   ZKDZentdigZp_parsedrO   ZA1ZA2ZHA1ZHA2ZncvaluerD   ZcnonceZrespdigZnoncebitbaser   )rF   r   build_digest_header   sr    

z"HTTPDigestAuth.build_digest_headerc             K   s   |j rd| j_dS )z)Reset num_401_calls counter on redirects.r   N)Zis_redirectr/   r7   )r   r   kwargsr   r   r   handle_redirect   s    zHTTPDigestAuth.handle_redirectc             K   s"  d|j   kodk n  s&d| j_|S | jjdk	rD|jjj| jj |jjdd}d|j	 koh| jjdk r| j jd7  _t
jd	t
jd
}t|jd|dd| j_|j |j  |jj }t|j|j|j |j|j | j|j|j|jd< |jj|f|}|jj| ||_|S d| j_|S )zo
        Takes the given response and tries digest-auth, if needed.

        :rtype: requests.Response
        i  i  r   Nzwww-authenticater1   Zdigest   zdigest )flags)countr*   )Zstatus_coder/   r7   r6   Zrequestbodyseekr+   rM   lowerrecompile
IGNORECASEr	   subr5   Zcontentclosecopyr   Z_cookiesrawZprepare_cookiesrW   rT   rU   Z
connectionsendhistoryappend)r   r   rX   Zs_authZpatZprepZ_rr   r   r   
handle_401   s.    	
zHTTPDigestAuth.handle_401c             C   s~   | j   | jjr&| j|j|j|jd< y|jj | j_	W n t
k
rT   d | j_	Y nX |jd| j |jd| j d| j_|S )Nr*   Zresponser   )r8   r/   r3   rW   rT   rU   r+   r]   tellr6   AttributeErrorZregister_hookrj   rY   r7   )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$   r8   rW   rY   rj   r   r(   r)   r   r   r   r   r-   l   s   
Z,r-   )r"   rQ   r`   rP   r>   r.   r   base64r   compatr   r   r   Zcookiesr   Z_internal_utilsr   Zutilsr	   ZCONTENT_TYPE_FORM_URLENCODEDZCONTENT_TYPE_MULTI_PARTr   objectr   r#   r,   r-   r   r   r   r   <module>   s$   ,