3 ,[$@s^dZddlmZmZddlZddlmZddlmZddl m Z ej e Z Gd d d e ZdS) aR oauthlib.oauth1.rfc5849.endpoints.access_token ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This module is an implementation of the access token provider logic of OAuth 1.0 RFC 5849. It validates the correctness of access 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) AccessTokenEndpointaAn endpoint responsible for providing OAuth 1 access tokens. Typical use is to instantiate with a request validator and invoke the ``create_access_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. cCsR|jj|j||_|j|jdj|jd}|j||jj||t|j S)a*Create and save a new access token. Similar to OAuth 2, indication of granted scopes will be included as a space separated list in ``oauth_authorized_realms``. :param request: An oauthlib.common.Request object. :returns: The token as an urlencoded string.  )Z oauth_tokenZoauth_token_secretZoauth_authorized_realms) request_validatorZ get_realmsresource_owner_keyZrealmsZtoken_generatorjoinupdateZsave_access_tokenritems)selfrequest credentialstokenr"/usr/lib/python3.6/access_token.pycreate_access_token"s   z'AccessTokenEndpoint.create_access_tokenGETNc Csddi}y^|j||||}|j|\}} |rZ|j||p8i} |jj|j|j||| dfSiddfSWn,tjk r} z|| j | j fSd} ~ XnXdS)aCreate an access 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 AccessTokenEndpoint >>> endpoint = AccessTokenEndpoint(your_validator) >>> h, b, s = endpoint.create_access_token_response( ... 'https://your.provider/access_token?foo=bar', ... headers={ ... 'Authorization': 'OAuth oauth_token=234lsdkf....' ... }, ... credentials={ ... 'my_specific': 'argument', ... }) >>> h {'Content-Type': 'application/x-www-form-urlencoded'} >>> b 'oauth_token=lsdkfol23w54jlksdef&oauth_token_secret=qwe089234lkjsdf&oauth_authorized_realms=movies+pics&my_specific=argument' >>> s 200 An response to invalid request would have a different body and status:: >>> b 'error=invalid_request&description=missing+resource+owner+key' >>> s 400 The same goes for an an unauthorized request: >>> b '' >>> s 401 z Content-Typez!application/x-www-form-urlencodedNi) Z_create_requestvalidate_access_token_requestrr Zinvalidate_request_token client_keyr rZ OAuth1ErrorZ urlencodedZ status_code) rZuriZ http_methodZbodyZheadersrZ resp_headersrZvalidZprocessed_requestrerrrcreate_access_token_response7s0  z0AccessTokenEndpoint.create_access_token_responsecCsx|j||j||js&tjdd|jj|js@tjdd|jsRtjdd|jj|jsltjdd|jj |j |j |j ||jdsd|fS|jj |j |}|s|jj|_ |jj|j |j|}|s|jj|_|jj|j |j|j|}|j|dd }||jd <||jd <||jd <||jd <t||||f}|sptjdtjd|tjd|tjd|tjd|||fS)a,Validate an access 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. zMissing resource owner.) descriptionz"Invalid resource owner key format.zMissing verifier.zInvalid verifier format.)Z request_tokenFT)Zis_token_requestZclientZresource_ownerverifierZ signaturez&[Failure] request verification failed.zValid client:, %szValid token:, %szValid verifier:, %szValid signature:, %s)Z_check_transport_securityZ_check_mandatory_parametersr rZInvalidRequestErrorr Zcheck_request_tokenrZcheck_verifierZvalidate_timestamp_and_noncerZ timestampZnonceZvalidate_client_keyZ dummy_clientZvalidate_request_tokenZdummy_request_tokenZvalidate_verifierZ_check_signatureZ validator_logallloginfo)rrZ valid_clientZvalid_resource_ownerZvalid_verifierZvalid_signaturevrrrrxs\                z1AccessTokenEndpoint.validate_access_token_request)rNNN)__name__ __module__ __qualname____doc__rrrrrrrr s   @r )r&Z __future__rrZloggingZoauthlib.commonrrbaserZ getLoggerr#r r rrrr s