3

V'ícß'ã@sÌdZddlZddlZddlZddlZddlZddlZddlmZddl	m
Z
mZmZddl
mZddlmZddlmZd	Zd
Zdd„ZGd
d„deƒZGdd„deƒZGdd„deƒZGdd„deƒZdS)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-urlencodedzmultipart/form-datacCsšt|tƒs&tjdj|ƒtdt|ƒ}t|tƒsPtjdjt|ƒƒtdt|ƒ}t|tƒrd|jdƒ}t|tƒrx|jdƒ}dt	t
dj||fƒƒjƒƒ}|S)zReturns a Basic Auth string.z·Non-string usernames will no longer be supported in Requests 3.0.0. Please convert the object you've passed in ({!r}) to a string or bytes object in the near future to avoid problems.)Úcategoryz·Non-string passwords will no longer be supported in Requests 3.0.0. Please convert the object you've passed in ({!r}) to a string or bytes object in the near future to avoid problems.Úlatin1zBasic ó:)
Ú
isinstancerÚwarningsÚwarnÚformatÚDeprecationWarningrÚtypeÚencoderrÚjoinÚstrip)ÚusernameÚpasswordÚauthstr©rú1/tmp/pip-build-gk9425m9/requests/requests/auth.pyÚ_basic_auth_strs&






rc@seZdZdZdd„ZdS)ÚAuthBasez4Base class that all auth implementations derive fromcCstdƒ‚dS)NzAuth hooks must be callable.)ÚNotImplementedError)ÚselfÚrrrrÚ__call__KszAuthBase.__call__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r rrrrrHsrc@s0eZdZdZdd„Zdd„Zdd„Zdd	„Zd
S)Ú
HTTPBasicAuthz?Attaches HTTP Basic Authentication to the given Request object.cCs||_||_dS)N)rr)rrrrrrÚ__init__RszHTTPBasicAuth.__init__cCs(t|jt|ddƒk|jt|ddƒkgƒS)Nrr)ÚallrÚgetattrr)rÚotherrrrÚ__eq__VszHTTPBasicAuth.__eq__cCs
||kS)Nr)rr)rrrÚ__ne__\szHTTPBasicAuth.__ne__cCst|j|jƒ|jd<|S)NÚ
Authorization)rrrÚheaders)rrrrrr _szHTTPBasicAuth.__call__N)r!r"r#r$r&r*r+r rrrrr%Os
r%c@seZdZdZdd„ZdS)Ú
HTTPProxyAuthz=Attaches HTTP Proxy Authentication to a given Request object.cCst|j|jƒ|jd<|S)NzProxy-Authorization)rrrr-)rrrrrr gszHTTPProxyAuth.__call__N)r!r"r#r$r rrrrr.dsr.c@sPeZdZdZdd„Zdd„Zdd„Zdd	„Zd
d„Zdd
„Z	dd„Z
dd„ZdS)ÚHTTPDigestAuthz@Attaches HTTP Digest Authentication to the given Request object.cCs||_||_tjƒ|_dS)N)rrÚ	threadingÚlocalÚ
_thread_local)rrrrrrr&oszHTTPDigestAuth.__init__cCs@t|jdƒs<d|j_d|j_d|j_i|j_d|j_d|j_dS)NÚinitTÚr)Úhasattrr2r3Ú
last_nonceÚnonce_countÚchalÚposÚ
num_401_calls)rrrrÚinit_per_thread_stateusz$HTTPDigestAuth.init_per_thread_statecs–|jjd}|jjd}|jjjdƒ}|jjjdƒ}|jjjdƒ}d‰|dkrTd}n|jƒ}|dksl|dkrzd	d
„}	|	‰n@|dkrdd
„}
|
‰n*|dkr¦dd„}|‰n|dkrºdd„}|‰‡fdd„}
ˆdkrÒdSd}t|ƒ}|jpæd}|jrü|d|j7}d|j||jf}d||f}ˆ|ƒ}ˆ|ƒ}||jj	krJ|jj
d7_
nd|j_
d|jj
}t|jj
ƒjdƒ}||jdƒ7}|t
jƒjdƒ7}|tjdƒ7}tj|ƒjƒdd…}|dkrЈd|||fƒ}|sê|
|d||fƒ}n<|dksd|jd ƒkr"d!|||d|f}|
||ƒ}ndS||j_	d"|j||||f}|rT|d#|7}|rf|d$|7}|rx|d%|7}|rŽ|d&||f7}d'|S)(z
        :rtype: str
        ÚrealmÚnonceÚqopÚ	algorithmÚopaqueNÚMD5zMD5-SESScSs"t|tƒr|jdƒ}tj|ƒjƒS)Nzutf-8)r
rrÚhashlibÚmd5Ú	hexdigest)ÚxrrrÚmd5_utf8‘s

z4HTTPDigestAuth.build_digest_header.<locals>.md5_utf8ÚSHAcSs"t|tƒr|jdƒ}tj|ƒjƒS)Nzutf-8)r
rrrBÚsha1rD)rErrrÚsha_utf8—s

z4HTTPDigestAuth.build_digest_header.<locals>.sha_utf8zSHA-256cSs"t|tƒr|jdƒ}tj|ƒjƒS)Nzutf-8)r
rrrBÚsha256rD)rErrrÚsha256_utf8s

z7HTTPDigestAuth.build_digest_header.<locals>.sha256_utf8zSHA-512cSs"t|tƒr|jdƒ}tj|ƒjƒS)Nzutf-8)r
rrrBÚsha512rD)rErrrÚsha512_utf8£s

z7HTTPDigestAuth.build_digest_header.<locals>.sha512_utf8csˆd||fƒS)Nz%s:%sr)ÚsÚd)Ú	hash_utf8rrÚ<lambda>©sz4HTTPDigestAuth.build_digest_header.<locals>.<lambda>ú/ú?z%s:%s:%sz%s:%srz%08xzutf-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)r2r8ÚgetÚupperrÚpathÚqueryrrr6r7rrÚtimeÚctimeÚosÚurandomrBrHrDÚsplit)rÚmethodÚurlr<r=r>r?r@Ú
_algorithmrFrIrKrMÚKDÚentdigÚp_parsedrZÚA1ÚA2ÚHA1ÚHA2ÚncvaluerNÚcnonceÚrespdigÚnoncebitÚbaser)rPrÚbuild_digest_headers~

z"HTTPDigestAuth.build_digest_headercKs|jrd|j_dS)z)Reset num_401_calls counter on redirects.rN)Úis_redirectr2r:)rrÚkwargsrrrÚhandle_redirectåszHTTPDigestAuth.handle_redirectcKs"d|jkodkns&d|j_|S|jjdk	rD|jjj|jjƒ|jjddƒ}d|j	ƒkoh|jjdkr|jjd7_t
jd	t
jd
}t
|jd|ddƒ|j_|j|jƒ|jjƒ}t|j|j|jƒ|j|jƒ|j|j|jƒ|jd<|jj|f|Ž}|jj|ƒ||_|Sd|j_|S)
zo
        Takes the given response and tries digest-auth, if needed.

        :rtype: requests.Response
        iiôrNzwww-authenticater4Údigestézdigest )Úflags)Úcountr,)Ústatus_coder2r:r9ÚrequestÚbodyÚseekr-rXÚlowerÚreÚcompileÚ
IGNORECASEr	Úsubr8ÚcontentÚcloseÚcopyrÚ_cookiesÚrawÚprepare_cookiesrprarbÚ
connectionÚsendÚhistoryÚappend)rrrrÚs_authÚpatÚprepÚ_rrrrÚ
handle_401ês.	
zHTTPDigestAuth.handle_401cCs~|jƒ|jjr&|j|j|jƒ|jd<y|jjƒ|j_	Wnt
k
rTd|j_	YnX|jd|jƒ|jd|j
ƒd|j_|S)Nr,Úresponser)r;r2r6rprarbr-rzÚtellr9ÚAttributeErrorÚ
register_hookrrsr:)rrrrrr szHTTPDigestAuth.__call__cCs(t|jt|ddƒk|jt|ddƒkgƒS)Nrr)r'rr(r)rr)rrrr**szHTTPDigestAuth.__eq__cCs
||kS)Nr)rr)rrrr+0szHTTPDigestAuth.__ne__N)r!r"r#r$r&r;rprsrr r*r+rrrrr/ls
f,r/)r$r^r}r\rBr0rÚbase64rÚcompatrrrÚcookiesrÚ_internal_utilsrÚutilsr	ÚCONTENT_TYPE_FORM_URLENCODEDÚCONTENT_TYPE_MULTI_PARTrÚobjectrr%r.r/rrrrÚ<module>s$,