o
    f>h                     @   s<   d dl Zd dlZddgZG dd deZG dd deZdS )    NJwtJwtDecodeErrorc                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   F/var/www/html/venv/lib/python3.10/site-packages/twilio/jwt/__init__.pyr      s    c                   @   s   e Zd ZdZe ZdZddeddfddZdd Zd	d
 Z	e
dddZedd Zedd ZdddZe
dddZdd ZdS )r   z(Base class for building a Json Web TokenHS256Ni  c                 C   sN   || _ 	 || _	 || _	 |p| j| _	 || _	 || _	 || _	 d | _d | _	d S N)

secret_keyissuersubject	ALGORITHM	algorithmnbfttlvalid_until_Jwt__decoded_payload_Jwt__decoded_headers)selfr   r   r   r   r   r   r   r   r   r   __init__   s    

zJwt.__init__c                 C   s   t d)z+:rtype: dict the payload of the JWT to sendz Subclass must provide a payload.)NotImplementedErrorr   r   r   r   _generate_payload.   s   zJwt._generate_payloadc                 C   s   i S )zP:rtype dict: Additional headers to include in the JWT, defaults to an empty dictr   r   r   r   r   _generate_headers2   s   zJwt._generate_headersc              
   C   sL   t ||dd|dd|dd|dd|ddd}||_||_|S )z
        Class specific implementation of from_jwt which should take jwt components and return
        and instance of this Class with jwt information loaded.
        :return: Jwt object containing the headers, payload and key
        issNsubalgexpr   )r   r   r   r   r   r   )r   getr   r   )clsheaderspayloadkeyjwtr   r   r   	_from_jwt6   s   




zJwt._from_jwtc                 C   s   | j r| j S |   }| j|d< tt | j |d< | jd ur5| j| jkr0tt |d< n| j|d< | j	r=| j	|d< | j
rE| j
|d< |S )Nr   r   r   r   )r   r   copyr   inttimer   r   GENERATEr   r   )r   r"   r   r   r   r"   I   s   




zJwt.payloadc                 C   s.   | j r| j S |   }d|d< | j|d< |S )NJWTtypr   )r   r   r&   r   )r   r!   r   r   r   r!   ]   s   
zJwt.headersc                 C   sP   | j std| j }| j }|rtt | |d< tj|| j | j	|dS )z
        Encode this JWT object into a JWT string
        :param int ttl: override the ttl configured in the constructor
        :rtype: str The JWT string
        z+JWT does not have a signing key configured.r   )r   r!   )
r   
ValueErrorr!   r&   r"   r'   r(   jwt_libencoder   )r   r   r!   r"   r   r   r   to_jwtg   s   

z
Jwt.to_jwt c              
   C   s   |rdnd}z,t |}|d}|| jkr"td| d| j dt j||| jg|dddd}W n tyH } z
tt|d	t	|d
}~ww | 
|||S )av  
        Decode a JWT string into a Jwt object
        :param str jwt: JWT string
        :param Optional[str] key: key used to verify JWT signature, if not provided then validation
                                  is skipped.
        :raises JwtDecodeError if decoding JWT fails for any reason.
        :return: A DecodedJwt object containing the jwt information.
        TFr   zIncorrect decoding algorithm z, expecting .)verify_signature
verify_exp
verify_nbf)
algorithmsoptionsmessageN)r-   get_unverified_headerr   r   r,   decode	Exceptionr   getattrstrr%   )r    r$   r#   verifyr!   r   r"   er   r   r   from_jwt{   s0   





zJwt.from_jwtc                 C   s   d |  S )Nz<JWT {}>)formatr/   r   r   r   r   __str__   s   zJwt.__str__r
   )r0   )r   r   r   __doc__objectr)   r   r   r   r   classmethodr%   propertyr"   r!   r/   r?   rA   r   r   r   r   r      s,    



	$)r$   r-   r(   __all__r:   r   rC   r   r   r   r   r   <module>   s
    