o
    Df                      @   s`   d Z ddlZddlmZmZ ddlmZ ddlmZm	Z	m
Z
mZ eeZG dd	 d	eeZdS )
a  
    authlib.oauth2.rfc6749.grants.refresh_token
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    A special grant endpoint for refresh_token grant_type. Refreshing an
    Access Token per `Section 6`_.

    .. _`Section 6`: https://tools.ietf.org/html/rfc6749#section-6
    N   )	BaseGrantTokenEndpointMixin   )scope_to_list)InvalidRequestErrorInvalidScopeErrorInvalidGrantErrorUnauthorizedClientErrorc                   @   s`   e Zd ZdZdZdZdd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZdS )RefreshTokenGrantzA special grant endpoint for refresh_token grant_type. Refreshing an
    Access Token per `Section 6`_.

    .. _`Section 6`: https://tools.ietf.org/html/rfc6749#section-6
    refresh_tokenFc                 C   s*   |   }td| || jst |S )NzValidate token request of %r)"authenticate_token_endpoint_clientlogdebugcheck_grant_type
GRANT_TYPEr
   )selfclient r   d/home/ubuntu/webapp/venv/lib/python3.10/site-packages/authlib/oauth2/rfc6749/grants/refresh_token.py_validate_request_client"   s
   z*RefreshTokenGrant._validate_request_clientc                 C   s@   | j jd}|d u rtd| |}|r||st |S )Nr   z#Missing "refresh_token" in request.)requestformgetr   authenticate_refresh_tokencheck_clientr	   )r   r   r   tokenr   r   r   _validate_request_token.   s   
z)RefreshTokenGrant._validate_request_tokenc                 C   sJ   | j j}|sd S | }|st tt|}|tt|s#t d S )N)r   scope	get_scoper   setr   
issuperset)r   r   r   original_scoper   r   r   _validate_token_scope8   s   z'RefreshTokenGrant._validate_token_scopec                 C   s0   |   }|| j_| |}| | || j_dS )a&  If the authorization server issued a refresh token to the client, the
        client makes a refresh request to the token endpoint by adding the
        following parameters using the "application/x-www-form-urlencoded"
        format per Appendix B with a character encoding of UTF-8 in the HTTP
        request entity-body, per Section 6:

        grant_type
             REQUIRED.  Value MUST be set to "refresh_token".

        refresh_token
             REQUIRED.  The refresh token issued to the client.

        scope
             OPTIONAL.  The scope of the access request as described by
             Section 3.3.  The requested scope MUST NOT include any scope
             not originally granted by the resource owner, and if omitted is
             treated as equal to the scope originally granted by the
             resource owner.


        For example, the client makes the following HTTP request using
        transport-layer security (with extra line breaks for display purposes
        only):

        .. code-block:: http

            POST /token HTTP/1.1
            Host: server.example.com
            Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
            Content-Type: application/x-www-form-urlencoded

            grant_type=refresh_token&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA
        N)r   r   r   r   r#   r   )r   r   r   r   r   r   validate_token_requestE   s
   "

z(RefreshTokenGrant.validate_token_requestc                 C   sv   | j j}| |}|std| j j}| ||}td|| || j _| 	| | j
d|d | | d|| jfS )a	  If valid and authorized, the authorization server issues an access
        token as described in Section 5.1.  If the request failed
        verification or is invalid, the authorization server returns an error
        response as described in Section 5.2.
        z"There is no "user" for this token.zIssue token %r to %rprocess_token)r      )r   r   authenticate_userr   r   issue_tokenr   r   user
save_tokenexecute_hookrevoke_old_credentialTOKEN_RESPONSE_HEADER)r   r   r)   r   r   r   r   r   create_token_responsem   s   


z'RefreshTokenGrant.create_token_responsec                 C   s*   | j j}|s
| }| j||| jd}|S )N)r)   r   include_refresh_token)r   r   r   generate_tokenINCLUDE_NEW_REFRESH_TOKEN)r   r)   r   r   r   r   r   r   r(      s   zRefreshTokenGrant.issue_tokenc                 C      t  )a  Get token information with refresh_token string. Developers MUST
        implement this method in subclass::

            def authenticate_refresh_token(self, refresh_token):
                token = Token.get(refresh_token=refresh_token)
                if token and not token.refresh_token_revoked:
                    return token

        :param refresh_token: The refresh token issued to the client
        :return: token
        NotImplementedErrorr   r   r   r   r   r      s   z,RefreshTokenGrant.authenticate_refresh_tokenc                 C   r2   )a"  Authenticate the user related to this credential. Developers MUST
        implement this method in subclass::

            def authenticate_user(self, credential):
                return User.get(credential.user_id)

        :param refresh_token: Token object
        :return: user
        r3   r5   r   r   r   r'      s   
z#RefreshTokenGrant.authenticate_userc                 C   r2   )al  The authorization server MAY revoke the old refresh token after
        issuing a new refresh token to the client. Developers MUST implement
        this method in subclass::

            def revoke_old_credential(self, refresh_token):
                credential.revoked = True
                credential.save()

        :param refresh_token: Token object
        r3   r5   r   r   r   r,      s   z'RefreshTokenGrant.revoke_old_credentialN)__name__
__module____qualname____doc__r   r1   r   r   r#   r$   r.   r(   r   r'   r,   r   r   r   r   r      s    
(r   )r9   loggingbaser   r   utilr   errorsr   r   r	   r
   	getLoggerr6   r   r   r   r   r   r   <module>   s    

