o
    ‘¸õaƒ$  ã                   @   sN   d Z ddlZddlmZ ddlmZ ddlmZ e e	¡Z
G dd	„ d	eƒZdS )
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.
é    N)Ú	urlencodeé   )Úerrorsé   )ÚBaseEndpointc                   @   s.   e Zd ZdZdd„ Z		d
dd„Zdd	„ ZdS )ÚAccessTokenEndpointa®  An 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.
    c                 C   sR   | j  |j|¡|_|  ¡ |  ¡ d |j¡dœ}| |¡ | j  ||¡ t| 	¡ ƒS )aH  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: OAuthlib request.
        :type request: oauthlib.common.Request
        :returns: The token as an urlencoded string.
        ú )Úoauth_tokenÚoauth_token_secretÚoauth_authorized_realms)
Úrequest_validatorÚ
get_realmsÚresource_owner_keyÚrealmsÚtoken_generatorÚjoinÚupdateÚsave_access_tokenr   Úitems)ÚselfÚrequestÚcredentialsÚtoken© r   úP/usr/lib/python3/dist-packages/oauthlib/oauth1/rfc5849/endpoints/access_token.pyÚcreate_access_token    s   
ÿ
ü
z'AccessTokenEndpoint.create_access_tokenÚGETNc              
   C   sœ   ddi}z/|   ||||¡}|  |¡\}}	|r.|  ||pi ¡}
| j |j|j|¡ ||
dfW S i ddfW S  tjyM } z||j	|j
fW  Y d}~S d}~ww )a  Create 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-urlencodedéÈ   Ni‘  )Ú_create_requestÚvalidate_access_token_requestr   r   Úinvalidate_request_tokenÚ
client_keyr   r   ÚOAuth1ErrorÚ
urlencodedÚstatus_code)r   ÚuriÚhttp_methodÚbodyÚheadersr   Úresp_headersr   ÚvalidÚprocessed_requestr   Úer   r   r   Úcreate_access_token_response6   s&   0ÿý€ÿz0AccessTokenEndpoint.create_access_token_responsec                 C   sv  |   |¡ |  |¡ |jstjdd�‚| j |j¡s tjdd�‚|js)tjdd�‚| j |j¡s6tjdd�‚| jj	|j
|j|j||jd�sId|fS | j |j
|¡}|sX| jj|_
| j |j
|j|¡}|si| jj|_| j |j
|j|j|¡}| j|dd	�}||jd
< ||jd< ||jd< ||jd< t||||fƒ}|s·t d¡ t d|¡ t d|¡ t d|¡ t d|¡ ||fS )aJ  Validate an access token request.

        :param request: OAuthlib request.
        :type request: oauthlib.common.Request
        :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Missing resource owner.)Údescriptionz"Invalid resource owner key format.zMissing verifier.zInvalid verifier format.)Úrequest_tokenFT)Úis_token_requestÚclientÚresource_ownerÚverifierÚ	signaturez&[Failure] request verification failed.zValid client:, %szValid token:, %szValid verifier:, %szValid signature:, %s)Ú_check_transport_securityÚ_check_mandatory_parametersr   r   ÚInvalidRequestErrorr   Úcheck_request_tokenr3   Úcheck_verifierÚvalidate_timestamp_and_noncer!   Ú	timestampÚnonceÚvalidate_client_keyÚdummy_clientÚvalidate_request_tokenÚdummy_request_tokenÚvalidate_verifierÚ_check_signatureÚvalidator_logÚallÚlogÚinfo)r   r   Úvalid_clientÚvalid_resource_ownerÚvalid_verifierÚvalid_signatureÚvr   r   r   r   w   sp   


ÿÿÿÿÿþ	ÿ
	
ÿ
ü



ÿ
z1AccessTokenEndpoint.validate_access_token_request)r   NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r-   r   r   r   r   r   r      s    	
ÿAr   )rO   ÚloggingÚoauthlib.commonr   Ú r   Úbaser   Ú	getLoggerrL   rE   r   r   r   r   r   Ú<module>   s   	
