
    ci                         d dl 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 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y)    N)Response)g)json)request)
LocalProxy)default_json_headers)ResourceProtector)OAuth1Errorc                   B    e Zd ZdZ	 d
dZddZd Zd Zd Zd Z	dd	Z
y)r	   aM  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                 d    || _         || _        || _        || _        |r| j	                  |       y y N)query_clientquery_token_exists_nonceappinit_app)selfr   r   r   exists_nonces        [/tmp/pip-target-z3e9_cxr/lib/python/authlib/integrations/flask_oauth1/resource_protector.py__init__zResourceProtector.__init__2   s7     )&)MM#     c                     ||| _         ||| _        ||| _        |j                  j	                  d      }|rt        |t        t        f      r|| _        || _	        y )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_app=   s^    # ,D"*D#!-D**..!EFz'D%=9/6D,r   c                 $    | j                  |      S r   )r   )r   	client_ids     r   get_client_by_idz"ResourceProtector.get_client_by_idK   s      ++r   c                 N    | j                  |j                  |j                        S r   )r   r"   token)r   r   s     r   get_token_credentialz&ResourceProtector.get_token_credentialN   s     1 17==AAr   c                     | j                   st        d      |j                  }|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_nonceQ   sM    !!EFF%%	%%	!!%IuEEr   c                     | j                  t        j                  t        j                  t        j                  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_credentialZ   sQ    ##KK499#4#4$#4#?
 .1^^*~~r   c                       fd}|S )Nc                 F     t        j                          fd       }|S )Nc                      	 j                           | i |S # t        $ rR}t        |j                               }t	        t        j                  |      |j                  t              cY d }~S d }~ww xY w)N)statusr3   )	r7   r
   dictget_bodyr   r   dumpsstatus_coder   )argskwargserrorbodyfr   s       r   	decoratedz>ResourceProtector.__call__.<locals>.wrapper.<locals>.decoratedc   sn    ++- $)&)) #  01D#

4($00 4 s    	A6AA1+A61A6)	functoolswraps)rD   rE   r   s   ` r   wrapperz+ResourceProtector.__call__.<locals>.wrapperb   s&    __Q
*  
* r    )r   scoperH   s   `  r   __call__zResourceProtector.__call__a   s    	  r   )NNNN)NNNr   )__name__
__module____qualname____doc__r   r   r#   r&   r   r7   rK   rI   r   r   r	   r	      s4    !H KO	,BFr   r	   c                  ,    t        j                  d      S )Nr5   )r   r   rI   r   r   _get_current_credentialrQ   u   s    553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      sC        ! % / B -d* dN5   78 r   