a
    !f                     @   sd   d Z ddlZddlZddlZddlZddlmZ dZdZe	dddd	Z
e	d
dddZdS )z4Helper methods for creating & verifying XSRF tokens.    N)_helpers   :i      c                 C   s   t tj| dd}|tjt|dd |t |tj|dd |t tjt|phtt dd}|| |	 }t
|t | }|S )a  Generates a URL-safe token for the given user, action, time tuple.

    Args:
        key: secret key to use.
        user_id: the user ID of the authenticated user.
        action_id: a string identifier of the action they requested
                   authorization for.
        when: the time in seconds since the epoch at which the user was
              authorized for this action. If not set the current time is used.

    Returns:
        A string XSRF protection token.
    zutf-8)encoding)hmacnewr   	_to_bytesupdatestr	DELIMITERinttimedigestbase64urlsafe_b64encode)keyuser_id	action_idwhenZdigesterr   token r   ^/var/www/html/python-backend/venv/lib/python3.9/site-packages/oauth2client/contrib/xsrfutil.pygenerate_token    s    


r      c              
   C   s   |sdS z t |}t|td }W n tttjfyD   Y dS 0 |du rVt		 }|| t
krfdS t| |||d}t|t|krdS d}tt|t|D ]\}	}
||	|
A O }q| S )aR  Validates that the given token authorizes the user for the action.

    Tokens are invalid if the time of issue is too old or if the token
    does not match what generateToken outputs (i.e. the token was forged).

    Args:
        key: secret key to use.
        token: a string of the token generated by generateToken.
        user_id: the user ID of the authenticated user.
        action_id: a string identifier of the action they requested
                   authorization for.

    Returns:
        A boolean - True if the user is authorized for the action, False
        otherwise.
    FN)r   r   r   )r   urlsafe_b64decoder   splitr   	TypeError
ValueErrorbinasciiErrorr   DEFAULT_TIMEOUT_SECSr   lenzip	bytearray)r   r   r   r   current_timedecodedZ
token_timeZexpected_tokenZ	differentxyr   r   r   validate_token<   s(    
r*   )r   N)r   N)__doc__r   r    r   r   Zoauth2clientr   r   r"   
positionalr   r*   r   r   r   r   <module>   s   