3 ,[; @sdZddlmZmZddlZddlZddlZejeZ ddl Z y ddl Z Wne k rhddl jZ YnXe jddkr~eZneZddlmZmZmZddlmZmZddlmZmZd Zd Zd ZeeefZd Z d Z!dZ"dZ#Gddde$Z%dS)z oauthlib.oauth1.rfc5849 ~~~~~~~~~~~~~~ This module is an implementation of various logic needed for signing and checking OAuth 1.0 RFC 5849 requests. )absolute_importunicode_literalsN)Request urlencodegenerate_nonce)generate_timestamp to_unicode) parameters signaturez HMAC-SHA1zRSA-SHA1Z PLAINTEXTZ AUTH_HEADERZQUERYZBODYz!application/x-www-form-urlencodedc @seZdZdZeejeeje ej iZ e ddZ ddddeedddddddf ddZdd Zd d Zd d ZdddZdddZdS)Clientz2A client used to sign OAuth 1.0 RFC 5849 requests.cCs||j|<dS)N)SIGNATURE_METHODS)clsZ method_nameZmethod_callbackr/usr/lib/python3.6/__init__.pyregister_signature_method3sz Client.register_signature_methodNzutf-8csfdd}|||_|||_|||_|||_|||_|||_|||_|||_|| |_|| |_ ||_ || |_ || |_ |||_ dS)aCreate an OAuth 1 client. :param client_key: Client key (consumer key), mandatory. :param resource_owner_key: Resource owner key (oauth token). :param resource_owner_secret: Resource owner secret (oauth token secret). :param callback_uri: Callback used when obtaining request token. :param signature_method: SIGNATURE_HMAC, SIGNATURE_RSA or SIGNATURE_PLAINTEXT. :param signature_type: SIGNATURE_TYPE_AUTH_HEADER (default), SIGNATURE_TYPE_QUERY or SIGNATURE_TYPE_BODY depending on where you want to embed the oauth credentials. :param rsa_key: RSA key used with SIGNATURE_RSA. :param verifier: Verifier used when obtaining an access token. :param realm: Realm (scope) to which access is being requested. :param encoding: If you provide non-unicode input you may use this to have oauthlib automatically convert. :param decoding: If you wish that the returned uri, headers and body from sign be encoded back from unicode, then set decoding to your preferred encoding, i.e. utf-8. :param nonce: Use this nonce instead of generating one. (Mainly for testing) :param timestamp: Use this timestamp instead of using current. (Mainly for testing) csrt|S|S)N)r )x)encodingrrXsz!Client.__init__..N) client_key client_secretresource_owner_keyresource_owner_secretsignature_methodsignature_type callback_urirsa_keyverifierrealmrdecodingnonce timestamp)selfrrrrrrrrrrrr r!r"encoder)rr__init__7s!              zClient.__init__cCspt|j}|drdnd|d<|dr,dnd|d<|dr@dnd|d<djdd|jD}d|jj|fS) Nrz****rrz, css|]\}}d||fVqdS)z%s=%sNr).0kvrrr osz"Client.__repr__..z<%s %s>)varscopyjoinitems __class____name__)r#attrsZ attribute_strrrr__repr__is  zClient.__repr__c Cs|jtkrtj|j|jS|j|\}}}tjtj|j ||d}t j dj |tj |}tj||jdd}t j dj |t j dj |tj|j||}t j dj ||j|jkrtd|j|j||} t j d j | | S) awGet an OAuth signature to be used in signing a request To satisfy `section 3.4.1.2`_ item 2, if the request argument's headers dict attribute contains a Host item, its value will replace any netloc part of the request argument's uri attribute value. .. _`section 3.4.1.2`: https://tools.ietf.org/html/rfc5849#section-3.4.1.2 )Z uri_querybodyheaderszCollected params: {0}ZHostNzNormalized params: {0}zNormalized URI: {0}z#Signing: signature base string: {0}zInvalid signature method.zSignature: {0})rSIGNATURE_PLAINTEXTr Zsign_plaintextrr_renderZcollect_parametersurlparseZquerylogdebugformatZnormalize_parametersZnormalize_base_string_urigetZconstruct_base_string http_methodr ValueError) r#requesturir3r2Zcollected_paramsZnormalized_paramsZnormalized_uriZ base_stringZsigrrrget_oauth_signaturers,    zClient.get_oauth_signaturecCs|jdkrtn|j}|jdkr&tn|j}d|fd|fdd|jfd|jfg}|jrd|jd|jf|jrz|jd |jf|j r|jd |j f|j j d d}|o|j d d k}|j dk r|r|jdtjtj|j jdjjdf|S)zMGet the basic OAuth parameters to be used in generating a signature. NZ oauth_nonceZoauth_timestamp oauth_version1.0Zoauth_signature_methodZoauth_consumer_keyZ oauth_tokenZoauth_callbackZoauth_verifierz Content-Typez!application/x-www-form-urlencodedrZoauth_body_hashzutf-8)r@rA)r!rr"rrrrappendrrr3r:findr2base64Z b64encodehashlibZsha1r$Zdigestdecode)r#r=r!r"Zparams content_typeZcontent_type_eligiblerrrget_oauth_paramss$ ,zClient.get_oauth_paramsFcCs|j|j|j}}}|jtkr6tj|j|j|d}n^|jtkrp|j dk rptj |j|j }|rft |}d|d<n$|jt krtj |j|j}ntd|||fS)aRender a signed request according to signature type Returns a 3-tuple containing the request URI, headers, and body. If the formencode argument is True and the body contains parameters, it is escaped and returned as a valid formencoded string. )rNz!application/x-www-form-urlencodedz Content-Typez!Unknown signature type specified.)r>r3r2rSIGNATURE_TYPE_AUTH_HEADERr Zprepare_headers oauth_paramsSIGNATURE_TYPE_BODY decoded_bodyZprepare_form_encoded_bodyrSIGNATURE_TYPE_QUERYZprepare_request_uri_queryr<)r#r= formencoderr>r3r2rrrr5s     zClient._renderGETcCs|t|||||jd}|jjdd}|o.|jd}|tk} |jdk } |rT| rTtdnp| rh| rhtdn\| r| rtdj|p~dtn<|j t kr| o| o| rtd n|j dkr| rtd |j ||_ |j jd |j|f|j|d|p|jd\}}}|jrrtjd|j|j|j}|r6|j|jn|}i} x.|jD]"\} } | j|j| | j|j<qHW| }|||fS)aFSign a request Signs an HTTP request with the specified parts. Returns a 3-tuple of the signed request's URI, headers, and body. Note that http_method is not returned as it is unaffected by the OAuth signing process. Also worth noting is that duplicate parameters will be included in the signature, regardless of where they are specified (query, body). The body argument may be a dict, a list of 2-tuples, or a formencoded string. The Content-Type header must be 'application/x-www-form-urlencoded' if it is present. If the body argument is not one of the above, it will be returned verbatim as it is unaffected by the OAuth signing process. Attempting to sign a request with non-formencoded data using the OAuth body signature type is invalid and will raise an exception. If the body does contain parameters, it will be returned as a properly- formatted formencoded string. Body may not be included if the http_method is either GET or HEAD as this changes the semantic meaning of the request. All string data MUST be unicode or be encoded with the same encoding scheme supplied to the Client constructor, default utf-8. This includes strings inside body dicts, for example. )rz Content-TypeNz multipart/z?Headers indicate a multipart body but body contains parameters.z?Headers indicate a formencoded body but body was not decodable.zGBody contains parameters but Content-Type header was {0} instead of {1}znot setz=Body signatures may only be used with form-urlencoded contentrOHEADz*GET/HEAD requests should not include body.Zoauth_signatureT)rNrz%Encoding URI, headers and body to %s.)rOrP)rrr3r: startswithCONTENT_TYPE_FORM_URLENCODEDrLr<r9rrKupperrHrJrBr?r5rr r7r8r$r-)r#r>r;r2r3rr=rGZ multipartZshould_have_paramsZ has_paramsZ new_headersr'r(rrrsignsJ        z Client.sign)FN)rONNN)r/ __module__ __qualname____doc__SIGNATURE_HMACr Zsign_hmac_sha1_with_client SIGNATURE_RSAZsign_rsa_sha1_with_clientr4Zsign_plaintext_with_clientr classmethodrrIr%r1r?rHr5rTrrrrr *s&  ) *! %r )&rWZ __future__rrrDrEZloggingZ getLoggerr/r7sysr6 ImportErrorZ urllib.parseparse version_infobytesZ bytes_typestrZoauthlib.commonrrrrr r r rXrYr4rrIrMrKrRobjectr rrrrs2