o
    ǌh                     @   s   d Z ddlZ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 eeZe
dgd	d
ZdededefddZdedefddZ	ddedee defddZdedee fddZdefddZdS )zI
Authentication utilities for password hashing and JWT token management.
    N)datetime	timedelta)Optional)JWTErrorjwt)CryptContext)settingsbcryptauto)schemes
deprecatedplain_passwordhashed_passwordreturnc              
   C   sT   zt | |}td |W S  ty) } ztd|  W Y d}~dS d}~ww )z!Verify password against its hash.u$   🔐 Password verification completedu!   ❌ Password verification error: NF)pwd_contextverifyloggerdebug	Exceptionerror)r   r   resulte r   auth_utils.pyverify_password   s   
r   passwordc              
   C   sF   zt | }td |W S  ty" } z	td|   d}~ww )zGenerate password hash.u'   🔐 Password hash created successfullyu   ❌ Password hashing error: N)r   hashr   r   r   r   )r   hashedr   r   r   r   get_password_hash   s   

r   dataexpires_deltac              
   C   s   z9|   }|rt | }n
t ttjd }|d|i tj|tj	tj
d}| dd}td|  |W S  tyN } z	td|   d}~ww )	zCreate JWT access token.minutesexp)	algorithmsubunknownu$   🎫 JWT token created for user ID: u   ❌ JWT token creation error: N)copyr   utcnowr   r   ACCESS_TOKEN_EXPIRE_MINUTESupdater   encode
SECRET_KEY	ALGORITHMgetr   r   r   r   )r   r    	to_encodeexpireencoded_jwtuser_idr   r   r   r   create_access_token*   s*   r3   tokenc              
   C   s   zt j| tjtjgd}|dd}td|  |W S  ty7 } zt	d|  W Y d}~dS d}~w t
yQ } ztd|  W Y d}~dS d}~ww )zVerify and decode JWT token.)
algorithmsr%   r&   u!   ✅ JWT token valid for user ID: u   ❌ Invalid JWT token: Nu-   ❌ Unexpected JWT token verification error: )r   decoder   r,   r-   r.   r   r   r   warningr   r   )r4   payloadr2   r   r   r   r   verify_tokenI   s$   r9   c                  C   s(   t  ttjd } td|   | S )zGet token expiration time.r!   u   ⏰ Token expiration time: )r   r(   r   r   r)   r   r   )
expirationr   r   r   get_token_expiration]   s
   r;   )N)__doc__loggingr   r   typingr   joser   r   passlib.contextr   configr   	getLogger__name__r   r   strboolr   r   dictr3   r9   r;   r   r   r   r   <module>   s*    

