o
    	,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m	Z	 d dl
mZ d dlmZ d dlmZmZ edd	Zed
d	ZG dd deZG dd dZeefdedefddZeefdedefddZdefddZdefddZdS )    )DependsHTTPException)OAuth2PasswordBearer)JWTErrorjwt)datetime	timedelta)settings)	BaseModel)LiteralOptionalz/FASTAPI/user/login)tokenUrlz/FASTAPI/student/loginc                   @   sN   e Zd ZU dZee ed< dZee ed< ed ed< ed ed< e	ed< dS )	JWTPayloadSchemaNuser_id
student_id)studentadminuser	user_type)r   r   
superadmin	user_roleexp)
__name__
__module____qualname__r   r   int__annotations__r   r   r    r   r   8/var/www/html/RetailAPI/app/dependency/authantication.pyr      s   
 r   c                   @   s8   e Zd ZededefddZededefddZdS )	
JWTManagerdatareturnc                 C   s:   t  ttjd }|| _| jdd}tj|tj	tj
dS )N)minutesT)exclude_none)	algorithm)r   utcnowr   r	   ACCESS_TOKEN_EXPIRE_MINUTESr   dictr   encode
SECRET_KEY	ALGORITHM)r    expirepayloadr   r   r   create_access_token   s   zJWTManager.create_access_tokentokenc              
   C   s   z3t j| tjtjgd}h d}||stdddt t	|d kr,tdddt
di |W S  ty@   tdd	d tyV } ztd
dt| dd }~ww )N)
algorithms>   r   r   r   i  zInvalid token structurestatus_codedetailr     zToken has expiredzInvalid or tampered tokeni  zInternal error: r   )r   decoder	   r)   r*   issubsetr   r   r%   utcfromtimestampr   r   	Exceptionstr)r.   r,   required_keyser   r   r   verify_token   s   
zJWTManager.verify_tokenN)r   r   r   staticmethodr   r8   r-   r;   r   r   r   r   r      s
    r   r.   r!   c                 C   
   t | S Nr   r;   r.   r   r   r   get_current_user3      
rA   c                 C   r=   r>   r?   r@   r   r   r   get_current_student8   rB   rC   allowed_rolesc                        t tfdtf fdd}|S )Ncurrent_userc                       | j  vrtddd| S Nr3   z#Access forbidden: insufficient roler0   r   r   )rF   rD   r   r   role_checker>      
z!allow_roles.<locals>.role_checker)r   rA   r   rD   rK   r   rJ   r   allow_roles=      rN   c                     rE   )Ncurrent_studentc                    rG   rH   rI   )rP   rJ   r   r   rK   H   rL   z)allow_student_roles.<locals>.role_checker)r   rC   r   rM   r   rJ   r   allow_student_rolesG   rO   rQ   N)fastapir   r   fastapi.securityr   joser   r   r   r   
app.configr	   pydanticr
   typingr   r   user_oauth2_schemestudent_oauth2_schemer   r   r8   rA   rC   rN   rQ   r   r   r   r   <module>   s    


