
    k7id6                         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
  SS
KJr  SSKJr   " S S\5      rg)    )add_params_to_uri)is_valid_url   )
BaseServer)AccessDeniedError)InvalidClientError)InvalidRequestError)InvalidTokenError)MethodNotAllowedError)MissingRequiredParameterError)OAuth1Errorc                       \ rS rSr/ SQrSrS rS rS rS r	S r
SS
 jrS rSS jrS rS rS rS rS rS rS rSrg	)AuthorizationServer   ))zContent-Typez!application/x-www-form-urlencoded)zCache-Controlzno-store)Pragmazno-cachePOSTc                 H    U R                  UR                  5      nX!l        U$ N)get_client_by_id	client_idclient)selfrequestr   s      e/home/james-whalen/.local/lib/python3.13/site-packages/authlib/oauth1/rfc5849/authorization_server.py_get_clientAuthorizationServer._get_client   s"    &&w'8'89    c                     [        5       er   NotImplementedErrorr   r   s     r   create_oauth1_request)AuthorizationServer.create_oauth1_request       !##r   c                     [        5       er   r   )r   status_codepayloadheaderss       r   handle_response#AuthorizationServer.handle_response   r$   r   c                 t    U R                  UR                  UR                  5       UR                  5       5      $ r   )r)   r&   get_bodyget_headers)r   errors     r   handle_error_response)AuthorizationServer.handle_error_response"   s2    ##u~~/1B1B1D
 	
r   c                    UR                   R                  5       U R                  :w  a
  [        5       eUR                  (       d  [        S5      eUR                  nUR                  (       d  [        S5      eUS:w  a  [        U5      (       d  [        S5      eU R                  U5      nU(       d
  [        5       eU R                  U5        U R                  U5        U$ )z0Validate HTTP request for temporary credentials.oauth_consumer_keyoauth_callbackoobzInvalid "oauth_callback" value)methodupperTEMPORARY_CREDENTIALS_METHODr   r   r   redirect_urir   r	   r   r   validate_timestamp_and_noncevalidate_oauth_signature)r   r   r3   r   s       r   &validate_temporary_credentials_request:AuthorizationServer.validate_temporary_credentials_request'   s     >>!T%F%FF'))   /0DEE !--##/0@AA U"<+G+G%&FGG!!'*$&&))'2%%g.r   Nc                 <    U R                  U5      nU R                  U5        U R	                  U5      nSUR                  5       4SUR                  5       4S/nU R                  SX@R                  5      $ ! [         a  nU R                  U5      s SnA$ SnAff = f)a  Validate temporary credentials token request and create response
for temporary credentials token. Assume the endpoint of temporary
credentials request is ``https://photos.example.net/initiate``:

.. code-block:: http

    POST /initiate HTTP/1.1
    Host: photos.example.net
    Authorization: OAuth realm="Photos",
        oauth_consumer_key="dpf43f3p2l4k3l03",
        oauth_signature_method="HMAC-SHA1",
        oauth_timestamp="137131200",
        oauth_nonce="wIjqoS",
        oauth_callback="http%3A%2F%2Fprinter.example.com%2Fready",
        oauth_signature="74KNZJeDHnMBp0EMJ9ZHt%2FXKycU%3D"

The server validates the request and replies with a set of temporary
credentials in the body of the HTTP response:

.. code-block:: http

    HTTP/1.1 200 OK
    Content-Type: application/x-www-form-urlencoded

    oauth_token=hh5s93j4hdidpola&oauth_token_secret=hdhd0244k9j7ao03&
    oauth_callback_confirmed=true

:param request: OAuth1Request instance.
:returns: (status_code, body, headers)
Noauth_tokenoauth_token_secret)oauth_callback_confirmedT   )	r"   r;   r   r/   create_temporary_credentialget_oauth_tokenget_oauth_token_secretr)   TOKEN_RESPONSE_HEADERr   r   r.   
credentialr'   s        r   %create_temporary_credentials_response9AuthorizationServer.create_temporary_credentials_responseF   s    >	5009G77@ 55g>
J6689!:#D#D#FG.

 ##C2L2LMM  	5--e44	5s   "A6 6
B BBBc                     UR                   (       d  [        S5      eU R                  U5      nU(       d
  [        5       eX!l        U$ )z6Validate the request for resource owner authorization.r>   )tokenr   get_temporary_credentialr
   rG   )r   r   rG   s      r   validate_authorization_request2AuthorizationServer.validate_authorization_requests   s=    }}/>>227;
#%% (r   c                    U R                  U5      nU R                  U5        UR                  nUR                  5       nU(       a  US:X  a1  UR	                  5       nU R                  U5      nUR                  5       nUc9  [        5       n[        XGR                  5       5      nU R                  SSSU4/5      $ X!l        U R                  U5      n	SUR                  4SU	4/n
[        XJ5      nU R                  SSSU4/5      $ )a  Validate authorization request and create authorization response.
Assume the endpoint for authorization request is
``https://photos.example.net/authorize``, the client redirects Jane's
user-agent to the server's Resource Owner Authorization endpoint to
obtain Jane's approval for accessing her private photos::

    https://photos.example.net/authorize?oauth_token=hh5s93j4hdidpola

The server requests Jane to sign in using her username and password
and if successful, asks her to approve granting 'printer.example.com'
access to her private photos.  Jane approves the request and her
user-agent is redirected to the callback URI provided by the client
in the previous request (line breaks are for display purposes only)::

    http://printer.example.com/ready?
    oauth_token=hh5s93j4hdidpola&oauth_verifier=hfdp7dh39dks9884

:param request: OAuth1Request instance.
:param grant_user: if granted, pass the grant user, otherwise None.
:returns: (status_code, body, headers)
r4   i.   Locationr>   oauth_verifier)r"   rM   rG   get_redirect_uriget_client_idr   get_default_redirect_urir   r   r,   r)   usercreate_authorization_verifierrK   )r   r   
grant_usertemporary_credentialsr8   r   r   r.   locationverifierparamss              r   create_authorization_response1AuthorizationServer.create_authorization_response   s   , ,,W5++G4 ' 2 2,==?|u4-;;=I**95F!::<L%'E(~~7GHH''R:x2H1IJJ!55g> '--03CX2NO$\:##Cz8.D-EFFr   c                    UR                   (       d  [        S5      eU R                  U5      nU(       d
  [        5       eUR                  (       d  [        S5      eU R                  U5      nU(       d
  [        5       eUR                  R                  S5      nU(       d  [        S5      eUR                  U5      (       d  [        S5      eX1l        U R                  U5        U R                  U5        U$ )z#Validate request for issuing token.r2   r>   rR   zInvalid "oauth_verifier")r   r   r   r   rK   rL   r
   oauth_paramsgetcheck_verifierr	   rG   r9   r:   )r   r   r   rK   r[   s        r   validate_token_request*AuthorizationServer.validate_token_request   s      /0DEE!!'*$&&}}/>>--g6#%%''++,<=/0@AA##H--%&@AA"))'2%%g.r   c                     U R                  U5      n U R                  U5        U R                  U5      nSUR                  5       4SUR                  5       4/nU R	                  U5        U R                  SX@R                  5      $ ! [         a  nU R                  U5      s SnA$ SnAff = f! [         a,  nU R	                  U5        U R                  U5      s SnA$ SnAff = f)an  Validate token request and create token response. Assuming the
endpoint of token request is ``https://photos.example.net/token``,
the callback request informs the client that Jane completed the
authorization process.  The client then requests a set of token
credentials using its temporary credentials (over a secure Transport
Layer Security (TLS) channel):

.. code-block:: http

    POST /token HTTP/1.1
    Host: photos.example.net
    Authorization: OAuth realm="Photos",
        oauth_consumer_key="dpf43f3p2l4k3l03",
        oauth_token="hh5s93j4hdidpola",
        oauth_signature_method="HMAC-SHA1",
        oauth_timestamp="137131201",
        oauth_nonce="walatlh",
        oauth_verifier="hfdp7dh39dks9884",
        oauth_signature="gKgrFCywp7rO0OXSjdot%2FIHF7IU%3D"

The server validates the request and replies with a set of token
credentials in the body of the HTTP response:

.. code-block:: http

    HTTP/1.1 200 OK
    Content-Type: application/x-www-form-urlencoded

    oauth_token=nnch734d00sl2jdk&oauth_token_secret=pfkkdhi9sl3r4s00

:param request: OAuth1Request instance.
:returns: (status_code, body, headers)
Nr>   r?   rA   )
r"   r   r/   rc   delete_temporary_credentialcreate_token_credentialrC   rD   r)   rE   rF   s        r   create_token_response)AuthorizationServer.create_token_response   s    D	5009G	5''0
 11':
J6689!:#D#D#FG
 	((1##C2L2LMM  	5--e44	5
  	5,,W5--e44	5s:   B B/ 
B,B'!B,'B,/
C%9!C C% C%c                     [        5       e)a  Generate and save a temporary credential into database or cache.
A temporary credential is used for exchanging token credential. This
method should be re-implemented::

    def create_temporary_credential(self, request):
        oauth_token = generate_token(36)
        oauth_token_secret = generate_token(48)
        temporary_credential = TemporaryCredential(
            oauth_token=oauth_token,
            oauth_token_secret=oauth_token_secret,
            client_id=request.client_id,
            redirect_uri=request.redirect_uri,
        )
        # if the credential has a save method
        temporary_credential.save()
        return temporary_credential

:param request: OAuth1Request instance
:return: TemporaryCredential instance
r   r!   s     r   rB   /AuthorizationServer.create_temporary_credential       * "##r   c                     [        5       e)a  Get the temporary credential from database or cache. A temporary
credential should share the same methods as described in models of
``TemporaryCredentialMixin``::

    def get_temporary_credential(self, request):
        key = "a-key-prefix:{}".format(request.token)
        data = cache.get(key)
        # TemporaryCredential shares methods from TemporaryCredentialMixin
        return TemporaryCredential(data)

:param request: OAuth1Request instance
:return: TemporaryCredential instance
r   r!   s     r   rL   ,AuthorizationServer.get_temporary_credential  s     "##r   c                     [        5       e)a  Delete temporary credential from database or cache. For instance,
if temporary credential is saved in cache::

    def delete_temporary_credential(self, request):
        key = "a-key-prefix:{}".format(request.token)
        cache.delete(key)

:param request: OAuth1Request instance
r   r!   s     r   rf   /AuthorizationServer.delete_temporary_credential%  s     "##r   c                     [        5       e)ac  Create and bind ``oauth_verifier`` to temporary credential. It
could be re-implemented in this way::

    def create_authorization_verifier(self, request):
        verifier = generate_token(36)

        temporary_credential = request.credential
        user_id = request.user.id

        temporary_credential.user_id = user_id
        temporary_credential.oauth_verifier = verifier
        # if the credential has a save method
        temporary_credential.save()

        # remember to return the verifier
        return verifier

:param request: OAuth1Request instance
:return: A string of ``oauth_verifier``
r   r!   s     r   rW   1AuthorizationServer.create_authorization_verifier1  rl   r   c                     [        5       e)a  Create and save token credential into database. This method would
be re-implemented like this::

    def create_token_credential(self, request):
        oauth_token = generate_token(36)
        oauth_token_secret = generate_token(48)
        temporary_credential = request.credential

        token_credential = TokenCredential(
            oauth_token=oauth_token,
            oauth_token_secret=oauth_token_secret,
            client_id=temporary_credential.get_client_id(),
            user_id=temporary_credential.get_user_id(),
        )
        # if the credential has a save method
        token_credential.save()
        return token_credential

:param request: OAuth1Request instance
:return: TokenCredential instance
r   r!   s     r   rg   +AuthorizationServer.create_token_credentialH  s    , "##r    r   )__name__
__module____qualname____firstlineno__rE   r7   r   r"   r)   r/   r;   rH   rM   r]   rc   rh   rB   rL   rf   rW   rg   __static_attributes__ru   r   r   r   r      sc     $* 
$$

>+NZ+GZ83Nj$.$ 
$$.$r   r   N)authlib.common.urlsr   r   base_serverr   errorsr   r   r	   r
   r   r   r   r   ru   r   r   <module>r~      s1    1 , # % & ' % ) 1 P$* P$r   