3 ,[E$@s^dZddlmZmZddlZddlmZddlmZddl m Z ej e Z Gd d d e ZdS) aV oauthlib.oauth1.rfc5849.endpoints.request_token ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This module is an implementation of the request token provider logic of OAuth 1.0 RFC 5849. It validates the correctness of request token requests, creates and persists tokens as well as create the proper response to be returned to the client. )absolute_importunicode_literalsN) urlencode)errors) BaseEndpointc@s*eZdZdZddZd ddZdd ZdS) RequestTokenEndpointaAn endpoint responsible for providing OAuth 1 request tokens. Typical use is to instantiate with a request validator and invoke the ``create_request_token_response`` from a view function. The tuple returned has all information necessary (body, status, headers) to quickly form and return a proper response. See :doc:`/oauth1/validator` for details on which validator methods to implement for this endpoint. cCs8|j|jdd}|j||jj||t|jS)zCreate and save a new request token. :param request: An oauthlib.common.Request object. :param credentials: A dict of extra token credentials. :returns: The token as an urlencoded string. true)Z oauth_tokenZoauth_token_secretZoauth_callback_confirmed)Ztoken_generatorupdaterequest_validatorZsave_request_tokenritems)selfrequest credentialstokenr#/usr/lib/python3.6/request_token.pycreate_request_token"s  z)RequestTokenEndpoint.create_request_tokenGETNc Csddi}yJ|j||||}|j|\}} |rF|j||p8i} || dfSiddfSWn,tjk r~} z|| j| jfSd} ~ XnXdS)aCreate a request token response, with a new request token if valid. :param uri: The full URI of the token request. :param http_method: A valid HTTP verb, i.e. GET, POST, PUT, HEAD, etc. :param body: The request body as a string. :param headers: The request headers as a dict. :param credentials: A list of extra credentials to include in the token. :returns: A tuple of 3 elements. 1. A dict of headers to set on the response. 2. The response body as a string. 3. The response status code as an integer. An example of a valid request:: >>> from your_validator import your_validator >>> from oauthlib.oauth1 import RequestTokenEndpoint >>> endpoint = RequestTokenEndpoint(your_validator) >>> h, b, s = endpoint.create_request_token_response( ... 'https://your.provider/request_token?foo=bar', ... headers={ ... 'Authorization': 'OAuth realm=movies user, oauth_....' ... }, ... credentials={ ... 'my_specific': 'argument', ... }) >>> h {'Content-Type': 'application/x-www-form-urlencoded'} >>> b 'oauth_token=lsdkfol23w54jlksdef&oauth_token_secret=qwe089234lkjsdf&oauth_callback_confirmed=true&my_specific=argument' >>> s 200 An response to invalid request would have a different body and status:: >>> b 'error=invalid_request&description=missing+callback+uri' >>> s 400 The same goes for an an unauthorized request: >>> b '' >>> s 401 z Content-Typez!application/x-www-form-urlencodedNi)Z_create_requestvalidate_request_token_requestrrZ OAuth1ErrorZ urlencodedZ status_code) rZuriZ http_methodZbodyZheadersrZ resp_headersrZvalidZprocessed_requestrerrrcreate_request_token_response2s0  z2RequestTokenEndpoint.create_request_token_responsecCsz|j||j||jr*|jjd|_n|jj|j||_|jj|jsdt j d|j|jjfd|j svt j dd|jj |j|j |j||jdsd|fS|jj|j|}|s|jj|_|jj|j|j|}|jj|j|j |}|j std|j|}||jd<||jd <||jd <||jd <t||||f}|srtjd tjd |tjd|tjd|tjd|||fS)a,Validate a request token request. :param request: An oauthlib.common.Request object. :raises: OAuth1Error if the request is invalid. :returns: A tuple of 2 elements. 1. The validation result (True or False). 2. The request object.  z!Invalid realm %s. Allowed are %r.) descriptionzMissing callback URI.)Z request_tokenFzKRedirect URI must either be provided or set to a default during validation.ZclientrealmcallbackZ signaturez&[Failure] request verification failed.zValid client: %s.zValid realm: %s.zValid callback: %s.zValid signature: %s.)Z_check_transport_securityZ_check_mandatory_parametersrsplitZrealmsr Zget_default_realmsZ client_keyZ check_realmsrZInvalidRequestErrorZ redirect_uriZvalidate_timestamp_and_nonceZ timestampZnonceZresource_owner_keyZvalidate_client_keyZ dummy_clientZvalidate_requested_realmsZvalidate_redirect_uriNotImplementedErrorZ_check_signatureZ validator_logallloginfo)rrZ valid_clientZ valid_realmZvalid_redirectZvalid_signaturevrrrrosP                 z3RequestTokenEndpoint.validate_request_token_request)rNNN)__name__ __module__ __qualname____doc__rrrrrrrr s   s