o
    hN                     @   s   d Z ddlmZ ddlmZmZmZ ddlmZm	Z	m
Z
mZmZmZ ddlZeeZe ZdZdZdd	 Zd
d Zdd Zdd Zdd ZdS )z4
JWT Authentication utilities for Flask application
    wraps)jsonifyrequestcurrent_app)
JWTManagerjwt_requiredcreate_access_tokenget_jwt_identityverify_jwt_in_requestget_jwtN
sisapiuserzM1n4#tr3e@12345c                 C   sT   t |  t jdd }t jdd }t jdd }t jdd }t jd	d
 }dS )zInitialize JWT with Flask appc                 S      t ddddfS )NzToken has expiredz*The token has expired. Please login again.errormessage  r   
jwt_headerjwt_payload r   /var/www/html/auth/jwt_auth.pyexpired_token_callback      z(init_jwt.<locals>.expired_token_callbackc                 S   r   )NzInvalid tokenz)The token is invalid. Please login again.r   r   r   r   r   r   r   invalid_token_callback   r   z(init_jwt.<locals>.invalid_token_callbackc                 S   r   )NzAuthorization requiredz)Request does not contain an access token.r   r   r   r   r   r   r   missing_token_callback%   r   z(init_jwt.<locals>.missing_token_callbackc                 S   r   )NzFresh token requiredz+The token is not fresh. Please login again.r   r   r   r   r   r   r   token_not_fresh_callback,   r   z*init_jwt.<locals>.token_not_fresh_callbackc                 S   r   )NzToken has been revokedzThe token has been revoked.r   r   r   r   r   r   r   revoked_token_callback3   r   z(init_jwt.<locals>.revoked_token_callbackN)jwtinit_appexpired_token_loaderinvalid_token_loaderunauthorized_loaderneeds_fresh_token_loaderrevoked_token_loader)appr   r   r   r   r   r   r   r   init_jwt   s   




r(   c                 C   s   | t kr|tkrd| iS dS )zAuthenticate user credentialsusernameN)USERNAMEPASSWORD)r)   passwordr   r   r   authenticate_user:   s   r-   c                 C   s   | t kr	t| dS dS )z'Create JWT token for authenticated user)identityN)r*   r	   )r)   r   r   r   create_user_token@   s   
r/   c                    s   t   fdd}|S )z2Decorator to require JWT authentication for routesc               
      s^   zt    | i |W S  ty. } ztd|  tddddfW  Y d }~S d }~ww )NzAuthentication error: zAuthentication failedz'Invalid or missing authentication tokenr   r   )r   	Exceptionloggerr   r   )argskwargsefr   r   decorated_functionH   s   z(require_auth.<locals>.decorated_functionr   )r6   r7   r   r5   r   require_authF   s   
r8   c                   C   s(   z	t   dt iW S  ty   Y dS w )z*Get current authenticated user informationr)   N)r   r
   r0   r   r   r   r   get_current_userU   s   r9   )__doc__	functoolsr   flaskr   r   r   flask_jwt_extendedr   r   r	   r
   r   r   logging	getLogger__name__r1   r    r*   r+   r(   r-   r/   r8   r9   r   r   r   r   <module>   s     
(