
    P1i6                         d Z ddlZddlmZ ddlmZmZ ddlmZ  ej                  e
      Z G d de      Z G d	 d
e      Z G d de      Zy)z
Custom JWT authentication backend that automatically tracks user activity.

This extends Simple JWT's authentication to fire signals whenever JWT tokens
are validated, ensuring systematic activity tracking for API requests.
    N)JWTAuthentication)InvalidToken
TokenError   )fire_jwt_authenticated_signalc                   ,     e Zd ZdZ fdZ fdZ xZS )!ActivityTrackingJWTAuthenticationz
    Custom JWT authentication that automatically tracks user activity.
    
    This extends the default Simple JWT authentication to fire a signal
    whenever a user is successfully authenticated via JWT token validation.
    c                     t         |   |      }||\  }}	 t        ||       |S |S # t        $ r#}t        j                  d|        Y d}~|S d}~ww xY w)zX
        Authenticate the request and fire activity tracking signal on success.
        Nz(Error firing JWT authentication signal: )superauthenticater   	Exceptionloggererror)selfrequestresultuservalidated_tokene	__class__s         5/home/cursorai/projects/iching/main/authentication.pyr   z.ActivityTrackingJWTAuthentication.authenticate   sq    
 %g. $*!D/M-dG<
 v	  MGsKLL	Ms   ) 	AAAc                     	 t         |   |      S # t        $ r5}t        j	                  d|        t        |j                  d         d}~ww xY w)z
        Validate the given raw token and return a validated token instance.
        
        Override to add additional logging if needed.
        zJWT token validation failed: r   N)r   get_validated_tokenr   r   debugr   args)r   	raw_tokenr   r   s      r   r   z5ActivityTrackingJWTAuthentication.get_validated_token,   sO    	*7.y99 	*LL8<=qvvay))	*s    	A0AA)__name__
__module____qualname____doc__r   r   __classcell__r   s   @r   r	   r	      s    (
* 
*    r	   c                   "     e Zd ZdZ fdZ xZS )JWTCookieAuthenticationz
    JWT authentication that reads tokens from cookies instead of headers.
    
    This is useful for web applications that want to use JWT tokens stored
    in HTTP-only cookies for additional security.
    c                     |j                   j                  d      }|t        |   |      S t	        |t
              r|j                  d      S |S )zU
        Extract the JWT token from cookies instead of Authorization header.
        access_tokenutf-8)COOKIESgetr   get_raw_token
isinstancestrencode)r   r   r   r   s      r   r+   z%JWTCookieAuthentication.get_raw_tokenA   sN    
 OO''7	7(11,6y#,Fy(UIUr#   r   r   r   r    r+   r!   r"   s   @r   r%   r%   9   s    V Vr#   r%   c                   "     e Zd ZdZ fdZ xZS )HybridJWTAuthenticationz
    Hybrid JWT authentication that tries both header and cookie methods.
    
    This provides maximum flexibility by supporting both:
    - Authorization header (for API clients)
    - HTTP-only cookies (for web applications)
    c                     t         |   |      }||S |j                  j                  d      }|#t	        |t
              r|j                  d      S |S y)zY
        Try to extract JWT token from Authorization header first, then cookies.
        Nr'   r(   )r   r+   r)   r*   r,   r-   r.   )r   r   header_tokencookie_tokenr   s       r   r+   z%HybridJWTAuthentication.get_raw_tokenX   s_    
 w,W5# **>:#3=lC3P<&&w/bVbbr#   r/   r"   s   @r   r1   r1   O   s     r#   r1   )r    logging'rest_framework_simplejwt.authenticationr   #rest_framework_simplejwt.exceptionsr   r   signalsr   	getLoggerr   r   r	   r%   r1    r#   r   <module>r;      sT     E H 2			8	$&*(9 &*RV? V,? r#   