U
    [Mh                     @   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schemesZ
deprecatedc                 C   s   t | |S N)pwd_contextZverify)Zplain_passwordhashed_password r   	./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)crudZ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   )ZminutesZexp)	algorithm)	copyr   Zutcnowr   updater   encoder
   r   )r   r   Z	to_encodeZ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$   Zpayloadr   r   r   verify_token*   s    r&   )N)r   r   typingr   Zjoser   r   Zpasslib.contextr   Zsqlalchemy.ormr   r   ZmodelsosZdotenvr	   getenvr
   r   intr   r   r   r   strr   dictr#   r&   r   r   r   r   <module>   s"   
