
    k7i                     T    S r SSKJr  SSKJr  SSKJr   " S S5      r " S S5      rg	)
zauthlib.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                   H    \ rS rSrSrSrSS jr\S 5       rS r	S r
S	 rS
rg)TokenValidator   zaBase token validator class. Subclass this validator to register
into ResourceProtector instance.
bearerNc                     Xl         X l        g N)realmextra_attributes)selfr   r   s      c/home/james-whalen/.local/lib/python3.13/site-packages/authlib/oauth2/rfc6749/resource_protector.py__init__TokenValidator.__init__   s    
 0    c                     U(       d  g[        U 5      n U (       d  g[        U 5      n U H/  n[        [        U5      5      nU R                  U5      (       d  M/    g   g)NFT)r   set
issuperset)token_scopesrequired_scopesscoperesource_scopess       r   scope_insufficient!TokenValidator.scope_insufficient   sT    $\2<($E!-"67O&&77 %
 r   c                     [        5       e)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_token!TokenValidator.authenticate_token*   s     "##r   c                     g)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_requestTokenValidator.validate_request6   s    r   c                     [        5       e)a  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_tokenTokenValidator.validate_tokenF   s     "##r   )r   r   r   )__name__
__module____qualname____firstlineno____doc__
TOKEN_TYPEr   staticmethodr   r    r%   r*   __static_attributes__r#   r   r   r   r      s6     J1   
$ $r   r   c                   :    \ rS rSrS rS\4S jrS rS rS r	Sr
g	)
ResourceProtectorV   c                 .    0 U l         S U l        S U l        g r   )_token_validators_default_realm_default_auth_type)r   s    r   r   ResourceProtector.__init__W   s    !#""&r   	validatorc                     U R                   (       d"  UR                  U l        UR                  U l         UR                  U R                  ;  a  XR                  UR                  '   gg)ztRegister a token validator for a given Authorization type.
Authlib has a built-in BearerTokenValidator per rfc6750.
N)r:   r   r9   r1   r8   )r   r<   s     r   register_token_validator*ResourceProtector.register_token_validator\   sT     &&"+//D&/&:&:D#t'='==;D""9#7#78 >r   c                     U R                   R                  UR                  5       5      nU(       d   [        U R                  U R
                  5      eU$ )z;Get token validator from registry for the given token type.)r8   getlowerr   r:   r9   )r   
token_typer<   s      r   get_token_validator%ResourceProtector.get_token_validatorg   sJ    **..z/?/?/AB	+'')<)<  r   c                 8   UR                   R                  S5      nU(       d   [        U R                  U R                  5      eUR                  SS5      n[        U5      S:w  a   [        U R                  U R                  5      eUu  pEU R                  U5      nXe4$ )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      )	headersrA   r   r:   r9   splitlenr   rD   )r   r$   authtoken_partsrC   r   r<   s          r   parse_request_authorization-ResourceProtector.parse_request_authorizationp   s     ""?3+'')<)< 
 jjq){q +'')<)<  $/ 
,,Z8	&&r   c                     U R                  U5      u  pEUR                  U5        UR                  U5      nUR                  " XaU40 UD6  U$ )z(Validate the request and return a token.)rN   r%   r    r*   )r   r)   r$   kwargsr<   r   r(   s          r   r%   "ResourceProtector.validate_request   sM    "&"B"B7"K	""7+,,\:  B6Br   )r:   r9   r8   N)r,   r-   r.   r/   r   r   r>   rD   rN   r%   r3   r#   r   r   r5   r5   V   s$    '
	E. 	E'<r   r5   N)r0   errorsr   r   utilr   r   r5   r#   r   r   <module>rU      s,    . - E$ E$P> >r   