a
    AWe                     @   s^   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 G dd dZ
dS )    N)datetime)Config)	Blacklistc                   @   s>   e Zd ZedddZedd Zedd Zedd	 Zd
S )TokenService   c                 C   sr   | |t t |d  d}t|}|d}t|tjd 	 }|d |d }t
|ddS )aS  
        Generate a token.

        Args:
            email (str): The email associated with the token.
            token_type (str): The type of the token.
            expiration_time_in_hours (int, optional): The expiration time of the token in hours. Defaults to 4 hours.

        Returns:
            str: The generated token.
        i  )emailtypeexputf-8   -_-   =)inttimejsondumpsencodehashlibsha1r   
SECRET_KEY	hexdigestbase64urlsafe_b64encoderstripdecode)r   
token_typeZexpiration_time_in_hoursdata	json_dataencoded_data	signature
token_data r    /app/app/services/token.pygenerate_token   s    

zTokenService.generate_tokenc              
   C   s   zt | dd }|d\}}t|t jd  }||	dkrt
|	d}|d tt kr|d|fW S W dS d|fW S  ty } zdt|fW  Y d}~S d}~0 0 dS )	a  
        Verify the validity and expiration of an activation token.

        Args:
            token (str): The token to be verified.

        Returns:
            Tuple[bool, Union[str, str]]: A tuple containing a boolean indicating whether
            the token is valid and either the associated email or an error message.

        Note:
            This function decodes and verifies tokens by checking its signature and 
            expiration time. If the token is valid and not expired, it returns True 
            and the associated . If the token is valid but expired, it returns False 
            and an error message. If an exception occurs during the verification 
            process, it returns False and an error message.

        r
   s   ==r   r	   T)FzToken has expiredFN)r   urlsafe_b64decoder   splitr   r   r   r   r   r   r   loadsr   r   	Exceptionstr)tokendecoded_tokenr   r   Zcomputed_signaturer   er    r    r!   verify_token&   s    

zTokenService.verify_tokenc                 C   s   t j| |d duS )z
        Check if a token is blacklisted (revoked).

        Args:
            token (str): The token to check.

        Returns:
            bool: True if the token is blacklisted, False if not.
        userjtiN)r   objectsfirstr,   r    r    r!   is_blacklistedX   s    zTokenService.is_blacklistedc              	   C   s.   t tt | ||||t d}|  dS )a  
        Blacklist a token with a given reason.

        Args:
            token (str): The token to blacklist.
            token_type (str): The type of token (e.g., 'access' or 'refresh').
            user (User): The user associated with the token.
            jti (str): The unique JWT ID (if applicable).
            reason (str): The reason for blacklisting (e.g., 'expired' or 'revoked').

        Returns:
            bool: True if the token is successfully blacklisted, False if not.
        )	public_idr(   r   r-   r.   reasonZ
revoked_atN)r   r'   uuiduuid4r   utcnowsave)r(   r   r-   r.   r3   Zblacklist_entryr    r    r!   blacklist_tokene   s    
	zTokenService.blacklist_tokenN)r   )__name__
__module____qualname__staticmethodr"   r+   r1   r8   r    r    r    r!   r      s   
0
r   )r   r4   r   r   r   r   app.config.settingsr   Zapp.models.blacklistr   r   r    r    r    r!   <module>   s   