ó  fH^c@sdZddlZddlZddlZddlZddlZddlZddlmZddl m Z m Z m Z ddl mZddlmZddlmZd Zd Zd „Zd efd „ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZdS(s] requests.auth ~~~~~~~~~~~~~ This module contains the authentication handlers for Requests. iÿÿÿÿN(t b64encodei(turlparsetstrt basestring(textract_cookies_to_jar(tto_native_string(tparse_dict_headers!application/x-www-form-urlencodedsmultipart/form-datacCsåt|tƒs:tjdj|ƒdtƒt|ƒ}nt|tƒsttjdj|ƒdtƒt|ƒ}nt|tƒr•|jdƒ}nt|tƒr¶|jdƒ}ndtt dj ||fƒƒj ƒƒ}|S(sReturns a Basic Auth string.s·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.tcategorys·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.tlatin1sBasic t:( t isinstanceRtwarningstwarntformattDeprecationWarningRtencodeRRtjointstrip(tusernametpasswordtauthstr((srequests/auth.pyt_basic_auth_strs&   %tAuthBasecBseZdZd„ZRS(s4Base class that all auth implementations derive fromcCstdƒ‚dS(NsAuth hooks must be callable.(tNotImplementedError(tselftr((srequests/auth.pyt__call__Ks(t__name__t __module__t__doc__R(((srequests/auth.pyRHst HTTPBasicAuthcBs2eZdZd„Zd„Zd„Zd„ZRS(s?Attaches HTTP Basic Authentication to the given Request object.cCs||_||_dS(N(RR(RRR((srequests/auth.pyt__init__Rs cCs:t|jt|ddƒk|jt|ddƒkgƒS(NRR(tallRtgetattrtNoneR(Rtother((srequests/auth.pyt__eq__VscCs ||k S(N((RR#((srequests/auth.pyt__ne__\scCs t|j|jƒ|jd<|S(Nt Authorization(RRRtheaders(RR((srequests/auth.pyR_s(RRRRR$R%R(((srequests/auth.pyROs    t HTTPProxyAuthcBseZdZd„ZRS(s=Attaches HTTP Proxy Authentication to a given Request object.cCs t|j|jƒ|jd<|S(NsProxy-Authorization(RRRR'(RR((srequests/auth.pyRgs(RRRR(((srequests/auth.pyR(dstHTTPDigestAuthcBsVeZdZd„Zd„Zd„Zd„Zd„Zd„Zd„Z d„Z RS( s@Attaches HTTP Digest Authentication to the given Request object.cCs%||_||_tjƒ|_dS(N(RRt threadingtlocalt _thread_local(RRR((srequests/auth.pyRos  cCsat|jdƒs]t|j_d|j_d|j_i|j_d|j_d|j_ ndS(Ntinitti( thasattrR,tTrueR-t last_noncet nonce_counttchalR"tpost num_401_calls(R((srequests/auth.pytinit_per_thread_stateus     csŠ|jjd}|jjd}|jjjdƒ}|jjjdƒ}|jjjdƒ}d"‰|d"krzd}n |jƒ}|dksž|dkr°d„} | ‰nZ|d krÎd „} | ‰n<|d krìd „} | ‰n|d kr d„} | ‰n‡fd†} ˆd"kr)d"Sd"}t|ƒ}|jpGd}|jrg|d|j7}nd|j||j f}d||f}ˆ|ƒ}ˆ|ƒ}||jj krÏ|jj d7_ n d|j_ d|jj }t |jj ƒj dƒ}||j dƒ7}|tjƒj dƒ7}|tjdƒ7}tj|ƒjƒd }|dkr†ˆd|||fƒ}n|s¨| |d||fƒ}nP|dksÉd|jdƒkrôd|||d|f}| ||ƒ}nd"S||j_ d|j||||f}|r7|d|7}n|rN|d|7}n|re|d|7}n|r‚|d ||f7}nd!|S(#s :rtype: str trealmtnoncetqopt algorithmtopaquetMD5sMD5-SESScSs4t|tƒr!|jdƒ}ntj|ƒjƒS(Nsutf-8(R RRthashlibtmd5t hexdigest(tx((srequests/auth.pytmd5_utf8‘stSHAcSs4t|tƒr!|jdƒ}ntj|ƒjƒS(Nsutf-8(R RRR=tsha1R?(R@((srequests/auth.pytsha_utf8—ssSHA-256cSs4t|tƒr!|jdƒ}ntj|ƒjƒS(Nsutf-8(R RRR=tsha256R?(R@((srequests/auth.pyt sha256_utf8ssSHA-512cSs4t|tƒr!|jdƒ}ntj|ƒjƒS(Nsutf-8(R RRR=tsha512R?(R@((srequests/auth.pyt sha512_utf8£scsˆd||fƒS(Ns%s:%s((tstd(t hash_utf8(srequests/auth.pyt©st/t?s%s:%s:%ss%s:%sis%08xsutf-8iitautht,s%s:%s:%s:%s:%ss>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 %sN(R,R3tgetR"tupperRtpathtqueryRRR1R2RRttimetctimetosturandomR=RCR?tsplit(RtmethodturlR7R8R9R:R;t _algorithmRARDRFRHtKDtentdigtp_parsedRStA1tA2tHA1tHA2tncvalueRItcnoncetrespdigtnoncebittbase((RKsrequests/auth.pytbuild_digest_headers~                     ! cKs|jrd|j_ndS(s)Reset num_401_calls counter on redirects.iN(t is_redirectR,R5(RRtkwargs((srequests/auth.pythandle_redirectås cKsŽd|jkodkns/d|j_|S|jjd k r]|jjj|jjƒn|jj ddƒ}d|j ƒkr~|jjdkr~|jjd7_t j dd t j ƒ}t|jd|d dƒƒ|j_|j|jƒ|jjƒ}t|j|j|jƒ|j|jƒ|j|j|jƒ|jd <|jj||}|jj|ƒ||_|Sd|j_|S( so Takes the given response and tries digest-auth, if needed. :rtype: requests.Response iiôiswww-authenticateR.tdigestisdigest tflagstcountR&N(t status_codeR,R5R4R"trequesttbodytseekR'RQtlowertretcompilet IGNORECASERtsubR3tcontenttclosetcopyRt_cookiestrawtprepare_cookiesRiRZR[t connectiontsendthistorytappend(RRRkts_authtpattprept_r((srequests/auth.pyt handle_401ês.  $$   cCs§|jƒ|jjr8|j|j|jƒ|jds$       ,