
    k7i#                         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  S
SKJr  S
SKJr  S
SKJr   " S S\5      rS r\	" \5      rg)    N)contextmanager)g)json)request)
LocalProxy)OAuth2Error)ResourceProtector)MissingAuthorizationError   )raise_http_exception)FlaskJsonRequest)token_authenticatedc                   F    \ rS rSrSrS rS	S jr\S	S j5       rS
S jr	Sr
g)r	      a7  A protecting method for resource servers. Creating a ``require_oauth``
decorator easily with ResourceProtector::

    from authlib.integrations.flask_oauth2 import ResourceProtector

    require_oauth = ResourceProtector()

    # add bearer token validator
    from authlib.oauth2.rfc6750 import BearerTokenValidator
    from project.models import Token


    class MyBearerTokenValidator(BearerTokenValidator):
        def authenticate_token(self, token_string):
            return Token.query.filter_by(access_token=token_string).first()


    require_oauth.register_token_validator(MyBearerTokenValidator())

    # protect resource with require_oauth


    @app.route("/user")
    @require_oauth(["profile"])
    def user_profile():
        user = User.get(current_token.user_id)
        return jsonify(user.to_dict())

c                     UR                   n[        R                  " [        UR	                  5       5      5      nUR                  5       n[        X#U5        g)zRaise HTTPException for OAuth2Error. Developers can re-implement
this method to customize the error response.

:param error: OAuth2Error
:raise: HTTPException
N)status_coder   dumpsdictget_bodyget_headersr   )selferrorstatusbodyheaderss        n/home/james-whalen/.local/lib/python3.13/site-packages/authlib/integrations/flask_oauth2/resource_protector.pyraise_error_response&ResourceProtector.raise_error_response1   sA     ""zz$u~~/01##%V73    Nc                     [        [        5      nXS'   U H#  n[        X$   [        5      (       d  M  X$   /X$'   M%     U R                  " SSU0UD6n[
        R                  " XS9  U[        l        U$ )z{A method to acquire current valid token with the given scope.

:param scopes: a list of scope values
:return: token object
scopesr   )token )	r   _req
isinstancestrvalidate_requestr   sendr   authlib_server_oauth2_token)r   r!   kwargsr   claimr"   s         r   acquire_tokenResourceProtector.acquire_token=   so     #4(!xE&---!'  %%@g@@  3(-%r   c              #      #     U R                  U5      v   g! [         a  nU R                  U5         SnAgSnAff = f7f)a-  The with statement of ``require_oauth``. Instead of using a
decorator, you can use a with statement instead::

    @app.route("/api/user")
    def user_api():
        with require_oauth.acquire("profile") as token:
            user = User.get(token.user_id)
            return jsonify(user.to_dict())
N)r,   r   r   )r   r!   r   s      r   acquireResourceProtector.acquireN   s;     	-$$V,, 	-%%e,,	-s$   A  A 
=8A =A c                 *   ^ ^^ UmUTS'   UUU 4S jnU$ )Nr!   c                 N   >^  [         R                  " T 5      UU UU4S j5       nU$ )Nc                     >  TR                   " S0 TD6  T" U 0 UD6$ ! [         a/  nT(       a  T" U 0 UD6s S nA$ TR                  U5         S nAN<S nAf[         a  nTR                  U5         S nAN_S nAff = f)Nr#   )r,   r
   r   r   )argsr*   r   claimsfoptionalr   s      r   	decorated>ResourceProtector.__call__.<locals>.wrapper.<locals>.decoratedd   s{    5&&00 $)&)) 1 5 $1&11--e44" 5--e445s*    
A9AA9AA9A44A9)	functoolswraps)r6   r8   r5   r7   r   s   ` r   wrapper+ResourceProtector.__call__.<locals>.wrapperc   s&    __Q	*  	* r   r#   )r   r!   r7   r*   r<   r5   s   ` `  @r   __call__ResourceProtector.__call__^   s    !x	 r   r#   )N)NF)__name__
__module____qualname____firstlineno____doc__r   r,   r   r/   r>   __static_attributes__r#   r   r   r	   r	      s*    <
4" - -r   r	   c                  .    [         R                  " S5      $ )Nr)   )r   getr#   r   r   _get_current_tokenrH   u   s    55.//r   )r:   
contextlibr   flaskr   r   r   r$   werkzeug.localr   authlib.oauth2r   r	   _ResourceProtectorauthlib.oauth2.rfc6749r
   errorsr   requestsr   signalsr   rH   current_tokenr#   r   r   <module>rS      sK     %   ! % & B < ( & (`* `F0 -.r   