U
    %h                     @   s   d dl m Z mZ d dlmZ d dlmZmZ d dlmZ d dl	m
Z
 d dlZd dlZd dlZd dlmZ e  edd	Zed
dZeeddZedgddZdd Zdd Ze
eedddZdeee dddZedddZdS )    )datetime	timedelta)Optional)JWTErrorjwt)CryptContext)SessionN)load_dotenv
SECRET_KEYzyour-secret-key-here	ALGORITHMZHS256ACCESS_TOKEN_EXPIRE_MINUTESZ30Zbcryptauto)Zschemes
deprecatedc                 C   s   t | |S N)pwd_contextverify)Zplain_passwordhashed_password r   '/var/www/html/timesheet/backend/auth.pyverify_password   s    r   c                 C   s
   t | S r   )r   hash)passwordr   r   r   get_password_hash   s    r   )dbusernamer   c                 C   s(   t | |}|sdS t||js$dS |S )NF)crudget_user_by_usernamer   r   )r   r   r   userr   r   r   authenticate_user   s    r   )dataexpires_deltac                 C   sN   |   }|rt | }nt tdd }|d|i tj|ttd}|S )N   )minutesexp)	algorithm)	copyr   utcnowr   updater   encoder
   r   )r   r    Z	to_encodeexpireZencoded_jwtr   r   r   create_access_token    s    r*   )tokenc                 C   s   t j| ttgd}|S )N)Z
algorithms)r   decoder
   r   )r+   payloadr   r   r   verify_token*   s    r.   )N)r   r   typingr   Zjoser   r   Zpasslib.contextr   sqlalchemy.ormr   r   modelsosdotenvr	   getenvr
   r   intr   r   r   r   strr   dictr*   r.   r   r   r   r   <module>   s"   
