
    k7i                         S r SSKrSSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SS	K	J
r
  S
SKJr  S
SKJr  \R                  " \5      r " S S\\5      rg)zauthlib.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   )InvalidGrantError)InvalidRequestError)InvalidScopeError)UnauthorizedClientError)hooked)scope_to_list   )	BaseGrant)TokenEndpointMixinc                   `    \ rS rSrSrSrSrS rS rS r	S r
\S	 5       rS
 rS rS rS rSrg)RefreshTokenGrant   zA 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                     U R                  5       n[        R                  SU5        UR                  U R                  5      (       d  [        SU R                   S35      eU$ )NzValidate token request of %rz0The client is not authorized to use 'grant_type=')"authenticate_token_endpoint_clientlogdebugcheck_grant_type
GRANT_TYPEr   )selfclients     e/home/james-whalen/.local/lib/python3.13/site-packages/authlib/oauth2/rfc6749/grants/refresh_token.py_validate_request_client*RefreshTokenGrant._validate_request_client$   s\     88:		0&9&&t77)B4??BSSTU      c                     U R                   R                  R                  S5      nUc  [        S5      eU R	                  U5      nU(       a  UR                  U5      (       d
  [        5       eU$ )Nr   z#Missing 'refresh_token' in request.)requestformgetr   authenticate_refresh_tokencheck_clientr   )r   r   r   tokens       r   _validate_request_token)RefreshTokenGrant._validate_request_token2   s_    ))--o> %&KLL//>E..v66#%%r   c                 "   U R                   R                  R                  nU(       d  g UR                  5       nU(       d
  [	        5       e[        [        U5      5      nUR                  [        [        U5      5      5      (       d
  [	        5       eg )N)r   payloadscope	get_scoper   setr	   
issuperset)r   r$   r)   original_scopes       r   _validate_token_scope'RefreshTokenGrant._validate_token_scope<   sn    $$***#%%]>:;((]5-A)BCC#%% Dr   c                     U R                  5       nXR                  l        U R                  U5      nU R	                  U5        X R                  l        g)af  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   s      r   validate_token_request(RefreshTokenGrant.validate_token_requestI   sE    D ..0$44V<""=1%2"r   c                 n   U R                   R                  nU R                  U5      nU(       d  [        S5      eU R                   R                  nU R                  X!5      n[        R                  SXC5        X R                   l        U R                  U5        U R                  U5        SX@R                  4$ )zIf 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   )r   r   authenticate_userr   r   issue_tokenr   r   user
save_tokenrevoke_old_credentialTOKEN_RESPONSE_HEADER)r   r   r7   r   r$   s        r   create_token_response'RefreshTokenGrant.create_token_responseq   s     22%%m4%&JKK$$  5		(%8 ""=1E5555r   c                     U R                   R                  R                  nU(       d  UR                  5       nU R	                  UUU R
                  S9nU$ )N)r7   r)   include_refresh_token)r   r(   r)   r*   generate_tokenINCLUDE_NEW_REFRESH_TOKEN)r   r7   r   r)   r$   s        r   r6   RefreshTokenGrant.issue_token   sS    $$**!++-E##"&"@"@ $ 

 r   c                     [        5       e)ar  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   s     r   r"   ,RefreshTokenGrant.authenticate_refresh_token   s     "##r   c                     [        5       e)zAuthenticate 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
rC   rE   s     r   r5   #RefreshTokenGrant.authenticate_user   s     "##r   c                     [        5       e)a4  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
rC   rE   s     r   r9   'RefreshTokenGrant.revoke_old_credential   s     "##r    N)__name__
__module____qualname____firstlineno____doc__r   r@   r   r%   r.   r1   r   r;   r6   r"   r5   r9   __static_attributes__rK   r   r   r   r      sS     !J !&&&3P 6 6(
$
$$r   r   )rP   loggingerrorsr   r   r   r   hooksr   utilr	   baser   r   	getLoggerrL   r   r   rK   r   r   <module>rX      sH     & ( & ,     $!_$	#5 _$r   