o
    Df$                     @   sT   d dl Z d dlmZ d dlmZ ddlmZ dZe e	Z
G dd dZd	d
 ZdS )    N)jwt)	JoseError   )InvalidClientErrorz6urn:ietf:params:oauth:client-assertion-type:jwt-bearerc                   @   sZ   e Zd ZdZeZdZdddZdd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd ZdS )JWTBearerClientAssertionz]Implementation of Using JWTs for Client Authentication, which is
    defined by RFC7523.
    client_assertion_jwtTc                 C   s   || _ || _d S )N)	token_url_validate_jti)selfr   validate_jti r   V/home/ubuntu/webapp/venv/lib/python3.10/site-packages/authlib/oauth2/rfc7523/client.py__init__   s   
z!JWTBearerClientAssertion.__init__c                 C   s\   |j }|d}|d}|tkr%|r%| ||}| || | |jS td| j	 d S )Nclient_assertion_typeclient_assertionzAuthenticate via %r failed)
formgetASSERTION_TYPEcreate_resolve_key_funcprocess_assertion_claimsauthenticate_clientclientlogdebugCLIENT_AUTH_METHOD)r
   query_clientrequestdataassertion_type	assertionresolve_keyr   r   r   __call__   s   

z!JWTBearerClientAssertion.__call__c                 C   s>   dt dddid| jdddid}| jrd| jd|d< |S )zCreate a claims_options for verify JWT payload claims. Developers
        MAY overwrite this method to create a more strict options.T)	essentialvalidater"   )r"   value)isssubaudexpjti)_validate_issr   r	   r   )r
   optionsr   r   r   create_claims_options!   s   
z.JWTBearerClientAssertion.create_claims_optionsc              
   C   sN   zt j|||  d}|  W |S  ty& } z	td| t d}~ww )aa  Extract JWT payload claims from request "assertion", per
        `Section 3.1`_.

        :param assertion: assertion string value in the request
        :param resolve_key: function to resolve the sign key
        :return: JWTClaims
        :raise: InvalidClientError

        .. _`Section 3.1`: https://tools.ietf.org/html/rfc7523#section-3.1
        )claims_optionszAssertion Error: %rN)r   decoder,   r#   r   r   r   r   )r
   r   r    claimser   r   r   r   0   s   
z1JWTBearerClientAssertion.process_assertion_claimsc                 C   s   | | jdr	|S t )Ntoken)check_endpoint_auth_methodr   r   )r
   r   r   r   r   r   F   s   z,JWTBearerClientAssertion.authenticate_clientc                    s    fdd}|S )Nc                    s,   |d } |}|st  |_|| S Nr&   )r   r   resolve_client_public_key)headerspayload	client_idr   r   r   r
   r   r   r    L   s   zEJWTBearerClientAssertion.create_resolve_key_func.<locals>.resolve_keyr   )r
   r   r   r    r   r8   r   r   K   s   
z0JWTBearerClientAssertion.create_resolve_key_funcc                 C      t  )af  Validate if the given ``jti`` value is used before. Developers
        MUST implement this method::

            def validate_jti(self, claims, jti):
                key = 'jti:{}-{}'.format(claims['sub'], jti)
                if redis.get(key):
                    return False
                redis.set(key, 1, ex=3600)
                return True
        NotImplementedError)r
   r/   r)   r   r   r   r   X   s   z%JWTBearerClientAssertion.validate_jtic                 C   r9   )aN  Resolve the client public key for verifying the JWT signature.
        A client may have many public keys, in this case, we can retrieve it
        via ``kid`` value in headers. Developers MUST implement this method::

            def resolve_client_public_key(self, client, headers):
                return client.public_key
        r:   )r
   r   r5   r   r   r   r4   e   s   z2JWTBearerClientAssertion.resolve_client_public_keyN)T)__name__
__module____qualname____doc__r   CLIENT_ASSERTION_TYPEr   r   r!   r,   r   r   r   r   r4   r   r   r   r   r   
   s    

r   c                 C   s   | d |kS r3   r   )r/   r%   r   r   r   r*   p   s   r*   )loggingauthlib.joser   authlib.jose.errorsr   rfc6749r   r   	getLoggerr<   r   r   r*   r   r   r   r   <module>   s    
f