o
    ^[2h                     @   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_jwtT<   c                 C   s   || _ || _|| _d S )N)	token_url_validate_jtileeway)selfr	   validate_jtir    r   n/home/skpark/git/infrasmart_work/infrasmart/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   s\   zt j|||  d}|j| jd W |S  ty- } ztd| t|j	d|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_options)r   zAssertion Error: %rdescriptionN)
r   decoder.   r%   r   r   r   r   r   r1   )r   r!   r"   claimser   r   r   r   7   s   
z1JWTBearerClientAssertion.process_assertion_claimsc                 C   s$   | | jdr	|S td| j d)Ntokenz,The client cannot authenticate with method: r0   )check_endpoint_auth_methodr   r   )r   r   r   r   r   r   L   s
   
z,JWTBearerClientAssertion.authenticate_clientc                    s    fdd}|S )Nc                    s0   |d } |}|st dd|_|| S )Nr(   z)The client does not exist on this server.r0   )r   r   resolve_client_public_key)headerspayload	client_idr   r   r   r   r   r   r"   T   s   zEJWTBearerClientAssertion.create_resolve_key_func.<locals>.resolve_keyr   )r   r   r   r"   r   r;   r   r   S   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   r3   r+   r   r   r   r   c   s   z%JWTBearerClientAssertion.validate_jtic                 C   r<   )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   r8   r   r   r   r7   p   s   z2JWTBearerClientAssertion.resolve_client_public_keyN)Tr   )__name__
__module____qualname____doc__r   CLIENT_ASSERTION_TYPEr   r   r#   r.   r   r   r   r   r7   r   r   r   r   r      s    

r   c                 C   s   | d |kS )Nr(   r   )r3   r'   r   r   r   r,   {   s   r,   )loggingauthlib.joser   authlib.jose.errorsr   rfc6749r   r   	getLoggerr?   r   r   r,   r   r   r   r   <module>   s    
o