cA4dZddlZddlZddlZddlZddlZddlmZddlm Z m Z m ZddlmZ ddlmZmZdZd Zej2d ej4Zej2d Zd Zej<d Zd%dZ dZ dZ dZ dZ!e"ee"dzZ#dZ$dZ%dZ&dZ'defdZ(dZ)dZ*defdZ+dZ,d&dZ-dZ.d'd Z/Gd!d"e0Z1Gd#d$Z2y#e$r ddlmZmZYwxYw)(z| oauthlib.common ~~~~~~~~~~~~~~ This module provides data structures and utilities common to all implementations of OAuth. N)quoteunquote urlencode) get_debug) SystemRandomrandbits)r getrandbits>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789z_ !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}z&([^&;]*(?:password|token)[^=]*=)[^&;]+z&%[^0-9A-Fa-f]|%[0-9A-Fa-f][^0-9A-Fa-f]zAABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-oauthlibct|tr|jdn|}t||}t|tr|j d}|SNutf-8) isinstancestrencode_quotebytesdecode)ssafes 1/usr/lib/python3/dist-packages/oauthlib/common.pyrr+sE'3/QAq$A!U HHW  Hc^t|}t|tr|jd}|Sr)_unquoterrr)rs rrr5s* A!U HHW  Hrctt|}t|}t|tr|S|j dSr)encode_params_utf8 _urlencoderrr)params utf8_params urlencodeds rrr?s7$V,KK(J*c"  ))rcg}|D]Z\}}|jt|tr|jdn|t|tr|jdn|f\|S)ziEnsures that all parameters in a list of 2-element tuples are encoded to bytestrings using UTF-8 r)appendrrr)rencodedkvs rrrHsbG=1!+As!3AHHW !+As!3AHHW < == Nrcg}|D]Z\}}|jt|tr|jdn|t|tr|jdn|f\|S)zfEnsures that all parameters in a list of 2-element tuples are decoded to unicode using UTF-8. r)r#rrr)rdecodedr%r&s rdecode_params_utf8r)TsbG?1!+Au!5AHHW 1!+Au!5AHHW 1> ?? Nrz=&;:%+~,*@!()/?'$c|r4t|tks"d}t|t|tz |fztj |r tdt j |d}t|S)aDecode a query string in x-www-form-urlencoded format into a sequence of two-element tuples. Unlike urlparse.parse_qsl(..., strict_parsing=True) urldecode will enforce correct formatting of the query string by validation. If validation fails a ValueError will be raised. urllib.parse_qsl will only raise errors if any of name-value pairs omits the equals sign. zError trying to decode a non urlencoded string. Found invalid characters: %s in the string: '%s'. Please ensure the request/response body is x-www-form-urlencoded.z%Invalid hex encoding in query string.Tkeep_blank_values)setr! ValueErrorINVALID_HEX_PATTERNsearchurlparse parse_qslr))queryerrorrs r urldecoder5csv SZ:-* #e*z"95!AABB !!%(@AA   >F f %%rcTt|ttfr t|}|St |drD t |tt|t r|jn|}t|}|Sd}|S#t$rd}Y|SwxYw#t$rd}Y|St$rd}Y|SwxYw)a*Extract parameters and return them as a list of 2-tuples. Will successfully extract parameters from urlencoded query strings, dicts, or lists of 2-tuples. Empty strings/dicts/lists will return an empty list of parameters. Any other input will result in a return value of None. N__iter__) rrrr5r.hasattrdictlistitemsr) TypeError)rawrs rextract_paramsr>s#s|$ s^F M j ! 0 I C)>#))+CHF'/F M M F M   F M F M s( A9 B 9 BB B' B'&B'cRtttdtzS)aGenerate pseudorandom nonce that is unlikely to repeat. Per `section 3.3`_ of the OAuth 1 RFC 5849 spec. Per `section 3.2.1`_ of the MAC Access Authentication spec. A random 64-bit number is appended to the epoch timestamp for both randomness and to decrease the likelihood of collisions. .. _`section 3.2.1`: https://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01#section-3.2.1 .. _`section 3.3`: https://tools.ietf.org/html/rfc5849#section-3.3 @)rr generate_timestamprrgenerate_noncerCs! s8B< #5#77 88rcNtttjS)aDGet seconds since epoch (UTC). Per `section 3.3`_ of the OAuth 1 RFC 5849 spec. Per `section 3.2.1`_ of the MAC Access Authentication spec. .. _`section 3.2.1`: https://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01#section-3.2.1 .. _`section 3.3`: https://tools.ietf.org/html/rfc5849#section-3.3 )rinttimerBrrrArAs s499;  rcbtdjfdt|DS)aXGenerates a non-guessable OAuth token OAuth (1 and 2) does not specify the format of tokens except that they should be strings of random characters. Tokens should not be guessable and entropy when generating the random characters is important. Which is why SystemRandom is used instead of the default random.choice method. c3@K|]}jywN)choice).0xcharsrands r z!generate_token..s=!4;;u%=s)rjoinrange)lengthrOrPs `@rgenerate_tokenrUs% >D 77=uV}= ==rcddl}tjj}|j|tj|j zd}|j |j|j||d}t|d}|S)Nr)seconds)scopeexpRS256UTF-8) jwtdatetimeutcnowrX timedelta expires_inupdateclaimsr to_unicode) private_pemrequestr\nowrbtokens rgenerate_signed_tokenrhsw    " " $CX''0B0BCCF  MM'..! JJv{G 4E ug &E Lrc4ddl}|j||dgS)NrrZ) algorithms)r\r) public_pemrgr\s rverify_signed_tokenrls ::eZWI: >>rct||S)zGenerates an OAuth client_id OAuth 2 specify the format of client_id in https://tools.ietf.org/html/rfc6749#appendix-A. )rU)rTrOs rgenerate_client_idrns &% ((rct|tr|j}tj|d}|j |t |S)z)Extend a query with a list of two-tuples.Tr+)rr9r;r1r2extendr)r3r queryparamss radd_params_to_qsrrsB&$$$UdCKv [ !!rctj|\}}}}}}|r t||}n t||}tj||||||fS)z5Add a list of two-tuples to the uri query components.)r1rr urlunparse) urirfragmentschnetpathparr3fras radd_params_to_urir|sX&.&7&7&<#CdCsF+ /   S$UC@ AArct|t|k7ryd}t||D]\}}|t|t|z z}!|dk(S)a Near-constant time string comparison. Used in order to avoid timing attacks on sensitive information such as secret keys during request verification (`rootLabs`_). .. _`rootLabs`: http://rdist.root.org/2010/01/07/timing-independent-array-comparison/ Fr)lenzipord)abresultrNys rsafe_string_equalsrsU 1vQ FAq "1#a&3q6/!" Q;rc xt|tr|St|tr t|St|drR t |t|dr|j }|Dcic]\}}t |t |c}}S|Scc}}w#t$rY|St$rfd|DcYSwxYw)z:Convert a number of different types of objects to unicode.)encodingr7r;c36K|]}t|ywrKrc)rMirs rrQzto_unicode..s:Jq(+:s) rrrr8r9r;rcr<r.)datarr%r&s ` rrcrcs$ $4(++tZ  W JtW%zz|QUVAJq(+Z8-DDV V KW   K ;:T: : ;s B*!B B9#B98B9cXeZdZdZiZdZdZfdZfdZd dZ fdZ fdZ xZ S) CaseInsensitiveDictz3Basic case insensitive dict with strings only keys.ct|Dcic]}|j|c}|_|D] }||||< ycc}wrKlowerproxy)selfrr%s r__init__zCaseInsensitiveDict.__init__-s?,01qaggil1  A1gDG 2s5c:|j|jvSrKr)rr%s r __contains__z CaseInsensitiveDict.__contains__2swwyDJJ&&rc|j|j}t| ||j|j=yrK)rrsuper __delitem__rr%key __class__s rrzCaseInsensitiveDict.__delitem__5s6jj# C JJqwwy !rc\|j|j}t| |SrK)rrr __getitem__rs rrzCaseInsensitiveDict.__getitem__:s'jj#w"3''rc||vr||S|SrKrB)rr%defaults rgetzCaseInsensitiveDict.get>st)tAw00rc`t|||||j|j<yrK)r __setitem__rr)rr%r&rs rrzCaseInsensitiveDict.__setitem__As& Aq! ! 1779rc~t||i|t|i|D]}||j|j <!yrK)rrar9rr)rargskwargsr%rs rrazCaseInsensitiveDict.updateEsA ''t&v& &A$%DJJqwwy ! &rrK) __name__ __module__ __qualname____doc__rrrrrrrra __classcell__)rs@rrr's3= E '" (1"&&rrcXeZdZdZ d dZdZdZedZedZ edZ y) Requesta:A malleable representation of a signable HTTP request. Body argument may contain any data, but parameters will only be decoded if they are one of: * urlencoded query string * dict * list of 2-tuples Anything else will be treated as raw body data to be passed through unmolested. NcHfd}|||_|||_t||xsi|_|||_t |j|_g|_i|_iddddddddddddddd dd dd dd dd ddddddddddddddddddddddddd |_ |jjtt|j|jjt|j xsgy)Nc$r t|S|SrKr)rNrs rz"Request.__init__..]sh:a2Ar access_tokenclient client_id client_secretcodecode_challengecode_challenge_method code_verifierextra_credentials grant_type redirect_uri refresh_token request_token response_typerXscopesstate) rgusertoken_type_hint response_modenoncedisplaypromptrbmax_age ui_locales id_token_hint login_hint acr_values)ru http_methodrheadersbodyr> decoded_body oauth_params validator_log_paramsrar9r5 uri_query)rrurrrrrs ` rrzRequest.__init__ZsF#;!+.*6'-R+@A 4L *4995! D! d!  !  T ! D ! d !  $T!  T!  !  $!  D!  T!  T!  T!  T! d!! " T#! $#"!A!  D D4>>!:;< D!2!2!8b9:rcR||jvr|j|St|rK)rAttributeError)rnames r __getattr__zRequest.__getattr__s( 4<< <<% % & &rctsy|j}|jj}|rtj dt |}d|vrd|d<dj|j|j||S)Nzz  Authorizationz zF) rrrcopySANITIZE_PATTERNsubrformatrur)rrrs r__repr__zRequest.__repr__sv{1yy,,##% #''TCD g %'4GO $W^^ HHd&&7 7rcTtj|jjSrK)r1rur3rs rrzRequest.uri_querys  *000rcb|jsgStj|jddS)NT)r,strict_parsing)rr1r2rs ruri_query_paramszRequest.uri_query_paramss,~~I!!$..D157 7rctjt}d|jxsg|jzD}|D]}||xxdz cc<|j Dcgc] \}}|dkDs |c}}Scc}}w)Nc3&K|] }|d yw)rNrB)rMps rrQz+Request.duplicate_params..sPaDPsr) collections defaultdictrErrr;)r seen_keysall_keysr%cs rduplicate_paramszRequest.duplicate_paramss{++C0 P"//529N9NNP A aLA L 'oo/9da1q5999s % A;3A;)GETNNr) rrrrrrrpropertyrrrrBrrrrKs] CG!0;d' 71177 ::rr)/)F)r[)3rrr]loggingrerF urllib.parseparser1rrrrrrrIrsecretsrr ImportErrorrandomr UNICODE_ASCII_CHARACTER_SETCLIENT_ID_CHARACTER_SETcompile IGNORECASErr/ always_safe getLoggerlogrr)r-r!r5r>rCrArUrhrlrnrrr|rrcr9rrrBrrrs< =. ,N2::GW bjj!JK" g #  *   $8 9 9 &B: 9 !$? >$? !(?)"B$2!&$!&He:e:k =<<=sC C0/C0