
    i[f                         d dl Z d dlmZmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ  G d d	e          Zd
 Z ee          ZdS )    N)gjsonResponse)request)
LocalProxy)default_json_headers)ResourceProtector)OAuth1Errorc                   J    e Zd ZdZ	 	 d
dZ	 	 ddZd Zd Zd Zd Z	dd	Z
dS )r	   a  A protecting method for resource servers. Initialize a resource
    protector with the these method:

    1. query_client
    2. query_token,
    3. exists_nonce

    Usually, a ``query_client`` method would look like (if using SQLAlchemy)::

        def query_client(client_id):
            return Client.query.filter_by(client_id=client_id).first()

    A ``query_token`` method accept two parameters, ``client_id`` and ``oauth_token``::

        def query_token(client_id, oauth_token):
            return Token.query.filter_by(client_id=client_id, oauth_token=oauth_token).first()

    And for ``exists_nonce``, if using cache, we have a built-in hook to create this method::

        from authlib.integrations.flask_oauth1 import create_exists_nonce_func

        exists_nonce = create_exists_nonce_func(cache)

    Then initialize the resource protector with those methods::

        require_oauth = ResourceProtector(
            app, query_client=query_client,
            query_token=query_token, exists_nonce=exists_nonce,
        )
    Nc                 p    || _         || _        || _        || _        |r|                     |           d S d S N)query_clientquery_token_exists_nonceappinit_app)selfr   r   r   exists_nonces        h/var/www/piapp/venv/lib/python3.11/site-packages/authlib/integrations/flask_oauth1/resource_protector.py__init__zResourceProtector.__init__)   sK    (&) 	MM#	 	    c                     ||| _         ||| _        ||| _        |j                            d          }|r#t          |t          t          f          r|| _        || _	        d S )N"OAUTH1_SUPPORTED_SIGNATURE_METHODS)
r   r   r   configget
isinstancelisttupleSUPPORTED_SIGNATURE_METHODSr   )r   r   r   r   r   methodss         r   r   zResourceProtector.init_app3   sp    # ,D"*D#!-D*..!EFF 	7z'D%=99 	7/6D,r   c                 ,    |                      |          S r   )r   )r   	client_ids     r   get_client_by_idz"ResourceProtector.get_client_by_idB   s      +++r   c                 B    |                      |j        |j                  S r   )r   r"   token)r   r   s     r   get_token_credentialz&ResourceProtector.get_token_credentialE   s     17=AAAr   c                     | j         st          d          |j        }|j        }|j        }|                      ||||          S )Nz$"exists_nonce" function is required.)r   RuntimeError	timestampr"   r%   )r   noncer   r)   r"   r%   s         r   r   zResourceProtector.exists_nonceH   sN    ! 	GEFFF%	%	!!%IuEEEr   c                     |                      t          j        t          j        t          j                            d          t          j                  }|j        t          _	        |j        S )NT)flat)
validate_request_reqmethodurlformto_dictheaders
credentialr    authlib_server_oauth1_credential)r   reqs     r   acquire_credentialz$ResourceProtector.acquire_credentialQ   sP    ##KHI4((L	
 
 .1^*~r   c                       fd}|S )Nc                 J     t          j                    fd            }|S )Nc                     	                                   nf# t          $ rY}t          |                                          }t	          t          j        |          |j        t                    cY d }~S d }~ww xY w | i |S )N)statusr3   )	r7   r
   dictget_bodyr   r   dumpsstatus_coder   )argskwargserrorbodyfr   s       r   	decoratedz>ResourceProtector.__call__.<locals>.wrapper.<locals>.decorated]   s    ++----"    0 011D#
4(($0 4         q$)&)))s    
A;AA60A;6A;)	functoolswraps)rD   rE   r   s   ` r   wrapperz+ResourceProtector.__call__.<locals>.wrapper\   s?    _Q
* 
* 
* 
* 
*  
* r    )r   scoperH   s   `  r   __call__zResourceProtector.__call__[   s#    	 	 	 	 	 r   )NNNN)NNNr   )__name__
__module____qualname____doc__r   r   r#   r&   r   r7   rK   rI   r   r   r	   r	   
   s         < /304    <@"   , , ,B B BF F F       r   r	   c                  *    t          j        d          S )Nr5   )r   r   rI   r   r   _get_current_credentialrQ   m   s    53444r   )rF   flaskr   r   r   r   r.   werkzeug.localr   authlib.constsr   authlib.oauth1r	   _ResourceProtectorauthlib.oauth1.errorsr
   rQ   current_credentialrI   r   r   <module>rY      s        # # # # # # # # # # ! ! ! ! ! ! % % % % % % / / / / / / B B B B B B - - - - - -` ` ` ` `* ` ` `F5 5 5  Z 788   r   