o
    Df                     @   sh   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
 d
eZdS )   )IntrospectionEndpoint    )ContinueIteration)default_json_headers)ExpiredTokenError)InvalidClaimError)InvalidTokenError)JWTBearerTokenValidatorc                       sX   e Zd ZdZdZd fdd	Zdd Zdd	 Zd
d Zdd Z	de
de
fddZ  ZS )JWTIntrospectionEndpointa  
    JWTIntrospectionEndpoint inherits from :ref:`specs/rfc7662`
    :class:`~authlib.oauth2.rfc7662.IntrospectionEndpoint` and implements the machinery
    to automatically process the JWT access tokens.

    :param issuer: The issuer identifier for which tokens will be introspected.

    :param \*\*kwargs: Other parameters are inherited from
        :class:`~authlib.oauth2.rfc7662.introspection.IntrospectionEndpoint`.

    ::

        class MyJWTAccessTokenIntrospectionEndpoint(JWTRevocationEndpoint):
            def get_jwks(self):
                ...

            def get_username(self, user_id):
                ...

        authorization_server.register_endpoint(
            MyJWTAccessTokenIntrospectionEndpoint(
                issuer="https://authorization-server.example.org",
            )
        )
        authorization_server.register_endpoint(MyRefreshTokenIntrospectionEndpoint)

    introspectionNc                    s    t  j|d|i| || _d S )Nserver)super__init__issuer)selfr   r   argskwargs	__class__ ]/home/ubuntu/webapp/venv/lib/python3.10/site-packages/authlib/oauth2/rfc9068/introspection.pyr   *   s   
z!JWTIntrospectionEndpoint.__init__c                 C   s*   |  |}| ||}| |}d|tfS )    )authenticate_endpoint_clientauthenticate_tokencreate_introspection_payloadr   )r   requestclienttokenbodyr   r   r   create_endpoint_response.   s   


z1JWTIntrospectionEndpoint.create_endpoint_responsec                 C   s   |  || |jddvrt t| jdd}| j|_z
||jd }W n
 ty0   t w |r<| 	|||r>|S dS dS )r   token_type_hint)access_tokenNN)r   resource_serverr   )
check_paramsformgetr   r	   r   get_jwksr   r   check_permission)r   r   r   	validatorr   r   r   r   r   ;   s   z+JWTIntrospectionEndpoint.authenticate_tokenc              
   C   s   |sddiS z|   W n# ty   ddi Y S  ty/ } z|jdkr(t t d }~ww dd|d |d |d |d	 |d |d
 |d d	}| |d  }rW||d< |S )NactiveFissTBearer	client_idscopesubaudexpiat)	r*   
token_typer-   r.   r/   r0   r+   r1   r2   username)validater   r   
claim_namer   r   get_username)r   r   excpayloadr4   r   r   r   r   O   s2   
z5JWTIntrospectionEndpoint.create_introspection_payloadc                 C   s   t  )zReturn the JWKs that will be used to check the JWT access token signature.
        Developers MUST re-implement this method::

            def get_jwks(self):
                return load_jwks("jwks.json")
        )NotImplementedError)r   r   r   r   r'   n   s   z!JWTIntrospectionEndpoint.get_jwksuser_idreturnc                 C   s   dS )zReturns an username from a user ID.
        Developers MAY re-implement this method::

            def get_username(self, user_id):
                return User.get(id=user_id).username
        Nr   )r   r;   r   r   r   r7   w   s   z%JWTIntrospectionEndpoint.get_username)N)__name__
__module____qualname____doc__ENDPOINT_NAMEr   r    r   r   r'   strr7   __classcell__r   r   r   r   r
   
   s    	r
   N)rfc7662r   authlib.common.errorsr   authlib.constsr   authlib.jose.errorsr   r   authlib.oauth2.rfc6750.errorsr   &authlib.oauth2.rfc9068.token_validatorr	   r
   r   r   r   r   <module>   s    