
    ([f                     Z    d Z ddlmZ ddlmZmZ  G d d          Z G d d          ZdS )	z
    authlib.oauth2.rfc6749.resource_protector
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

    .. _`Section 7`: https://tools.ietf.org/html/rfc6749#section-7
   )scope_to_list)MissingAuthorizationErrorUnsupportedTokenTypeErrorc                   F    e Zd ZdZdZd	dZed             Zd Zd Z	d Z
dS )
TokenValidatorziBase token validator class. Subclass this validator to register
    into ResourceProtector instance.
    bearerNc                 "    || _         || _        d S N)realmextra_attributes)selfr   r   s      ]/var/www/piapp/venv/lib/python3.11/site-packages/authlib/oauth2/rfc6749/resource_protector.py__init__zTokenValidator.__init__   s    
 0    c                     |sdS t          |           } | sdS t          |           } |D ]6}t          t          |                    }|                     |          r dS 7dS )NFT)r   set
issuperset)token_scopesrequired_scopesscoperesource_scopess       r   scope_insufficientz!TokenValidator.scope_insufficient   s     	5$\22 	4<(($ 	 	E!-"6"677O&&77 uu tr   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                     dS )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_request4   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_tokenD   s     "###r   r
   )__name__
__module____qualname____doc__
TOKEN_TYPEr   staticmethodr   r   r!   r%   r   r   r   r   r      s          J1 1 1 1   \ 
$ 
$ 
$   $ $ $ $ $r   r   c                   2    e Zd Zd ZdefdZd Zd Zd ZdS )ResourceProtectorc                 0    i | _         d | _        d | _        d S r
   )_token_validators_default_realm_default_auth_type)r   s    r   r   zResourceProtector.__init__U   s     !#""&r   	validatorc                     | j         s|j        | _        |j        | _         |j        | j        vr|| j        |j        <   dS dS )zRegister 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_validatorZ   sS     & 	;"+/D&/&:D#t'===;DD"9#7888 >=r   c                     | 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_validatore   sI    *..z/?/?/A/ABB	 	Z+D,CTEXYYYr   c                 0   |j                             d          }|st          | j        | j                  |                    dd          }t          |          dk    rt          | 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_authorizationl   s     ""?33 	Z+D,CTEXYYY jjq)){q  +D,CTEXYYY#. 
L,,Z88	,&&r   c                     |                      |          \  }}|                    |           |                    |          } |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   se    "&"B"B7"K"K	<""7+++,,\:: 	 BB6BBBr   N)	r&   r'   r(   r   r   r4   r9   rB   r!   r   r   r   r-   r-   T   sp        ' ' '
	E. 	E 	E 	E 	E  ' ' '4    r   r-   N)r)   utilr   errorsr   r   r   r-   r   r   r   <module>rG      s            H H H H H H H HD$ D$ D$ D$ D$ D$ D$ D$N8 8 8 8 8 8 8 8 8 8r   