o
    ȟ1i                     @   s   d dl m Z mZmZ d dlmZ d dlmZmZmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ edgd	d
Ze
ddZdedefddZdededefddZdZdZdefddZdeeeef fddZdefddZdS )     )datetime	timedeltatimezone)	Annotated)DependsHTTPExceptionstatus)OAuth2PasswordBearer)jwt)InvalidTokenError)CryptContextZbcryptauto)Zschemes
deprecatedtoken)tokenUrlpasswordreturnc                 C   s
   t | S N)pwd_contexthash)r    r   $/app/core/security/authentication.pyhash_password   s   
r   plain_passwordhashed_passwordc                 C   s   t | |S r   )r   verify)r   r   r   r   r   verify_password   s   r   z$f4af2e339a059-489779c74ad750604d9b2fi  datac                 C   s<   |   }ttjttd }|d|i tj	|t
ddS )N)minutesexpHS256)	algorithm)copyr   nowr   utcr   ACCESS_TOKEN_EXPIRE_MINUTESupdater
   encode
SECRET_KEY)r   Z	to_encodeZexpirer   r   r   create_access_token   s   r)   c              
      sh   t tjdddid}ztj| tdgd}|d s||W S  ty&   | ty3 } z|d }~ww )NzCould not validate credentialszWWW-AuthenticateBearer)status_codedetailheadersr    )Z
algorithmsid)r   r   HTTP_401_UNAUTHORIZEDr
   decoder(   r   	Exception)r   Zcredentials_exceptionpayloader   r   r   get_user!   s    r4   required_rolec                    s   t tfdtf fdd}|S )Ncurrent_userc                    s&   | d  krt tjd  dd| S )NZ	user_typezInsufficient privileges, z role required)r+   r,   )r   r   HTTP_403_FORBIDDEN)r6   r5   r   r   role_dependency2   s
   
z%require_role.<locals>.role_dependency)r   r4   dict)r5   r9   r   r8   r   require_role1   s   r;   N)r   r   r   typingr   fastapir   r   r   Zfastapi.securityr	   Zjoser
   Zjwt.exceptionsr   Zpasslib.contextr   r   Zoauth2_schemestrr   boolr   r(   r%   r:   r)   r4   r;   r   r   r   r   <module>   s     
