o
    i:                     @   s   d dl Z d dlZd dlZd dlZd dlZeeZdd Zdd Z	G dd de
ZG dd	 d	e
ZG d
d deZeZeZeje_dS )    Nc                 C   s   z| j ddW S    |  Y S )Nzutf-8)encoding)encode)raw r   O/home/app/Keep/.python/lib/python3.10/site-packages/msal/oauth2cli/assertion.py
_str2bytes   s   r   c                 C   s   t t|  S N)base64urlsafe_b64encodebinasciia2b_hexdecode)Z
thumbprintr   r   r   _encode_thumbprint   s   r   c                   @   s&   e Zd Z		dddZ	dddZdS )	AssertionCreatorNX  c           	      K   s   t d)a+  Create an assertion in bytes, based on the provided claims.

        All parameter names are defined in https://tools.ietf.org/html/rfc7521#section-5
        except the expires_in is defined here as lifetime-in-seconds,
        which will be automatically translated into expires_at in UTC.
        z Will be implemented by sub-class)NotImplementedError)	selfaudienceissuersubject
expires_at
expires_in	issued_atassertion_idkwargsr   r   r   create_normal_assertion   s   	z(AssertionCreator.create_normal_assertionc                    s*   t |||||f fdd	t|d ddS )zCreate an assertion as a callable,
        which will then compute the assertion later when necessary.

        This is a useful optimization to reuse the client assertion.
        c                    s    j | ||fd|i|S )Nr   )r   )aiser   r   r   r   <lambda>)   s   z@AssertionCreator.create_regenerative_assertion.<locals>.<lambda><   r   )r   )AutoRefreshermax)r   r   r   r   r   r   r   r    r   create_regenerative_assertion!   s   z.AssertionCreator.create_regenerative_assertion)Nr   NN)Nr   )__name__
__module____qualname__r   r%   r   r   r   r   r      s    
r   c                   @   s"   e Zd ZdZdddZdd ZdS )	r#   a  Cache the output of a factory, and auto-refresh it when necessary. Usage::

        r = AutoRefresher(time.time, expires_in=5)
        for i in range(15):
            print(r())  # the timestamp change only after every 5 seconds
            time.sleep(1)
      c                 C   s   || _ || _i | _d S r   )_factory_expires_in_buf)r   factoryr   r   r   r   __init__6   s   
zAutoRefresher.__init__c                 C   s\   d\}}t   }| j|d|kr#td ||  ||| j i| _ntd | j|S )N)r   valuer   zRegenerating new assertionzReusing still valid assertion)timer,   getloggerdebugr*   r+   )r   Z
EXPIRES_ATZVALUEnowr   r   r   __call__:   s   

zAutoRefresher.__call__N)r)   )r&   r'   r(   __doc__r.   r5   r   r   r   r   r#   .   s    
r#   c                   @   s.   e Zd Z	dddddZ			d	ddZdS )
JwtAssertionCreatorN)sha256_thumbprintc                C   sB   || _ || _|p	i | _|rt|| jd< |rt|| jd< dS dS )a6  Construct a Jwt assertion creator.

        Args:

            key (str):
                An unencrypted private key for signing, in a base64 encoded string.
                It can also be a cryptography ``PrivateKey`` object,
                which is how you can work with a previously-encrypted key.
                See also https://github.com/jpadilla/pyjwt/pull/525
            algorithm (str):
                "RS256", etc.. See https://pyjwt.readthedocs.io/en/latest/algorithms.html
                RSA and ECDSA algorithms require "pip install cryptography".
            sha1_thumbprint (str): The x5t aka X.509 certificate SHA-1 thumbprint.
            headers (dict): Additional headers, e.g. "kid" or "x5c" etc.
            sha256_thumbprint (str): The x5t#S256 aka X.509 certificate SHA-256 thumbprint.
        zx5t#S256Zx5tN)key	algorithmheadersr   )r   r9   r:   Zsha1_thumbprintr;   r8   r   r   r   r.   F   s   
zJwtAssertionCreator.__init__r   c
                 K   s   ddl }t }|||p||p|| |p||ptt d}|r%||d< ||	p*i  z|j|| j| j| j	d}t
|W S    | jdsM| jdrRtd  )	zCreate a JWT Assertion.

        Parameters are defined in https://tools.ietf.org/html/rfc7523#section-3
        Key-value pairs in additional_claims will be added into payload as-is.
        r   N)ZaudZisssubexpZiatZjtiZnbf)r:   r;   ZRSESzSome algorithms requires "pip install cryptography". See https://pyjwt.readthedocs.io/en/latest/installation.html#cryptographic-dependencies-optional)jwtr0   struuiduuid4updater   r9   r:   r;   r   
startswithr2   	exception)r   r   r   r   r   r   r   r   Z
not_beforeZadditional_claimsr   r?   r4   payloadZstr_or_bytesr   r   r   r   c   s.   	

z+JwtAssertionCreator.create_normal_assertion)NN)NNr   NNNN)r&   r'   r(   r.   r   r   r   r   r   r7   E   s    r7   )r0   r   r	   rA   logging	getLoggerr&   r2   r   r   objectr   r#   r7   ZSignerZ	JwtSignerr   Zsign_assertionr   r   r   r   <module>   s    
A