Ë
    †Kài  ã                   óT   — d Z ddlmZ ddlmZ ddlmZ  G d„ d«      Z G d„ d«      Zy	)
zØauthlib.oauth2.rfc6749.resource_protector.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Implementation of Accessing Protected Resources per `Section 7`_.

.. _`Section 7`: https://tools.ietf.org/html/rfc6749#section-7
é   )ÚMissingAuthorizationError)ÚUnsupportedTokenTypeError)Úscope_to_listc                   ó>   — e Zd ZdZdZd	d„Zed„ «       Zd„ Zd„ Z	d„ Z
y)
ÚTokenValidatorziBase token validator class. Subclass this validator to register
    into ResourceProtector instance.
    ÚbearerNc                 ó    — || _         || _        y ©N)ÚrealmÚextra_attributes)Úselfr   r   s      úu/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/authlib/oauth2/rfc6749/resource_protector.pyÚ__init__zTokenValidator.__init__   s   € ØˆŒ
Ø 0ˆÕó    c                 ó˜   — |syt        | «      } | syt        | «      } |D ])  }t        t        |«      «      }| j                  |«      sŒ) y y)NFT)r   ÚsetÚ
issuperset)Útoken_scopesÚrequired_scopesÚscopeÚresource_scopess       r   Úscope_insufficientz!TokenValidator.scope_insufficient   sV   € áØä$ \Ó2ˆÙØä˜<Ó(ˆØ$ò 	ˆEÜ!¤-°Ó"6Ó7ˆOØ×&Ñ& Õ7Ùð	ð
 r   c                 ó   — t        «       ‚)a_  A method to query token from database with the given token string.
        Developers MUST re-implement this method. For instance::

            def authenticate_token(self, token_string):
                return get_token_from_database(token_string)

        :param token_string: A string to represent the access_token.
        :return: token
        ©ÚNotImplementedError)r   Útoken_strings     r   Úauthenticate_tokenz!TokenValidator.authenticate_token*   s   € ô "Ó#Ð#r   c                  ó   — y)a@  A method to validate if the HTTP request is valid or not. Developers MUST
        re-implement this method.  For instance, your server requires a
        "X-Device-Version" in the header::

            def validate_request(self, request):
                if "X-Device-Version" not in request.headers:
                    raise InvalidRequestError()

        Usually, you don't have to detect if the request is valid or not. If you have
        to, you MUST re-implement this method.

        :param request: instance of HttpRequest
        :raise: InvalidRequestError
        N© )r   Úrequests     r   Úvalidate_requestzTokenValidator.validate_request6   s    r   c                 ó   — t        «       ‚)a4  A method to validate if the authorized token is valid, if it has the
        permission on the given scopes. Developers MUST re-implement this method.
        e.g, check if token is expired, revoked::

            def validate_token(self, token, scopes, request):
                if not token:
                    raise InvalidTokenError()
                if token.is_expired() or token.is_revoked():
                    raise InvalidTokenError()
                if not match_token_scopes(token, scopes):
                    raise InsufficientScopeError()
        r   )r   ÚtokenÚscopesr    s       r   Úvalidate_tokenzTokenValidator.validate_tokenF   s   € ô "Ó#Ð#r   r
   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú
TOKEN_TYPEr   Ústaticmethodr   r   r!   r%   r   r   r   r   r      s6   „ ñð €Jó1ð ñó ðò 
$òó $r   r   c                   ó0   — e Zd Zd„ Zdefd„Zd„ Zd„ Zd„ Zy)ÚResourceProtectorc                 ó.   — i | _         d | _        d | _        y r
   )Ú_token_validatorsÚ_default_realmÚ_default_auth_type)r   s    r   r   zResourceProtector.__init__W   s   € Ø!#ˆÔØ"ˆÔØ"&ˆÕr   Ú	validatorc                 óÄ   — | j                   s"|j                  | _        |j                  | _         |j                  | j                  vr|| j                  |j                  <   yy)z„Register a token validator for a given Authorization type.
        Authlib has a built-in BearerTokenValidator per rfc6750.
        N)r1   r   r0   r*   r/   )r   r2   s     r   Úregister_token_validatorz*ResourceProtector.register_token_validator\   sV   € ð ×&Ò&Ø"+§/¡/ˆDÔØ&/×&:Ñ&:ˆDÔ#à×Ñ t×'=Ñ'=Ñ=Ø;DˆD×"Ñ" 9×#7Ñ#7Ò8ð >r   c                 óœ   — | j                   j                  |j                  «       «      }|s t        | j                  | j
                  «      ‚|S )z;Get token validator from registry for the given token type.)r/   ÚgetÚlowerr   r1   r0   )r   Ú
token_typer2   s      r   Úget_token_validatorz%ResourceProtector.get_token_validatorg   sJ   € à×*Ñ*×.Ñ.¨z×/?Ñ/?Ó/AÓBˆ	ÙÜ+Ø×'Ñ'¨×)<Ñ)<óð ð Ðr   c                 ó0  — |j                   j                  d«      }|s t        | j                  | j                  «      ‚|j                  dd«      }t        |«      dk7  r t        | j                  | j                  «      ‚|\  }}| j                  |«      }||fS )aË  Parse the token and token validator from request Authorization header.
        Here is an example of Authorization header::

            Authorization: Bearer a-token-string

        This method will parse this header, if it can find the validator for
        ``Bearer``, it will return the validator and ``a-token-string``.

        :return: validator, token_string
        :raise: MissingAuthorizationError
        :raise: UnsupportedTokenTypeError
        ÚAuthorizationNr   é   )	Úheadersr6   r   r1   r0   ÚsplitÚlenr   r9   )r   r    ÚauthÚtoken_partsr8   r   r2   s          r   Úparse_request_authorizationz-ResourceProtector.parse_request_authorizationp   s   € ð ‰×"Ñ" ?Ó3ˆÙÜ+Ø×'Ñ'¨×)<Ñ)<óð ð
 —j‘j  qÓ)ˆÜˆ{Ó˜qÒ Ü+Ø×'Ñ'¨×)<Ñ)<óð ð $/Ñ ˆ
LØ×,Ñ,¨ZÓ8ˆ	Ø˜,Ð&Ð&r   c                 óœ   — | j                  |«      \  }}|j                  |«       |j                  |«      } |j                  |||fi |¤Ž |S )z(Validate the request and return a token.)rB   r!   r   r%   )r   r$   r    Úkwargsr2   r   r#   s          r   r!   z"ResourceProtector.validate_requestŽ   sS   € à"&×"BÑ"BÀ7Ó"KÑˆ	<Ø×"Ñ" 7Ô+Ø×,Ñ,¨\Ó:ˆØ ˆ	× Ñ  ¨°ÑB¸6ÒBØˆr   N)	r&   r'   r(   r   r   r4   r9   rB   r!   r   r   r   r-   r-   V   s$   „ ò'ð
	E°.ó 	Eòò'ó<r   r-   N)r)   Úerrorsr   r   Úutilr   r   r-   r   r   r   ú<module>rG      s,   ðñõ .Ý -Ý ÷E$ñ E$÷P>ò >r   