
    {i                        d Z ddlmZ ddlmZmZ ddlmZ ddlmZm	Z	m
Z
mZmZmZ ddlmZmZ ddlmZ  G d d	e      Zy
)zF
Authentication session model for storing active tokens and sessions.
    )annotations)datetimetimezone)Optional)StringDateTimeBooleanText
ForeignKeyfunc)Mappedmapped_column)Basec                     e Zd ZU dZdZ edd      Zded<    e ed      dd	      Z	ded
<    ee
dddd      Zded<    ee
dddd      Zded<    ee
dd      Zded<    e ed      dd      Zded<    ee
dd      Zded<    e ed      d      Zded<    e ed      d      Zded<    eeddd       Zd!ed"<    eeddd       Zd!ed#<    e ed       ej0                         d$      Zded%<    e ed       ej0                          ej0                         &      Zded'<    e ed      d      Zded(<    e ed       ej0                         d$      Zded)<   d0d*Zd1d+Zd2d,Zd2d-Z d1d.Z!y/)3AuthSessionz
    Model for storing user authentication sessions and tokens.
    
    This table tracks active user sessions, refresh tokens, and provides
    the ability to revoke tokens for logout functionality.
    auth_sessionsT)primary_keyindexzMapped[int]idzusers.idF)nullabler   user_idzAccess token for the session)uniquer   r   doczMapped[str]access_tokenzRefresh token for the sessionzMapped[Optional[str]]refresh_tokenzDevice/browser information)r   r   device_info-   zIP address of the session
ip_addresszUser agent string
user_agent)r   )r   zMapped[datetime]access_token_expires_atzMapped[Optional[datetime]]refresh_token_expires_at)defaultr   r   zMapped[bool]	is_active
is_revoked)server_defaultr   
created_at)r%   onupdate
updated_at
revoked_atlast_activity_atc                V    d| j                    d| j                   d| j                   dS )Nz<AuthSession(id=z
, user_id=z, is_active=z)>)r   r   r#   selfs    _/var/www/html/hwPaymentPortal-be-dev/hw-payment-portal-api/src/apps/auth/models/auth_session.py__repr__zAuthSession.__repr__7   s+    !$''*T\\N,t~~N^^`aa    c                p    d| _         d| _        t        j                  t        j
                        | _        y)zRevoke this session (logout).FTN)r#   r$   r   nowr   utcr)   r,   s    r.   revokezAuthSession.revoke:   s$    ",,x||4r0   c                b    | j                   t        j                  t        j                        k  S )z%Check if the access token is expired.)r    r   r2   r   r3   r,   s    r.   
is_expiredzAuthSession.is_expired@   s     ++hll8<<.HHHr0   c                |    | j                   sy| j                   t        j                  t        j                        k  S )z&Check if the refresh token is expired.T)r!   r   r2   r   r3   r,   s    r.   is_refresh_token_expiredz$AuthSession.is_refresh_token_expiredD   s,    ,,,,x||HLL/IIIr0   c                T    t        j                  t        j                        | _        y)zUpdate last activity timestamp.N)r   r2   r   r3   r*   r,   s    r.   update_activityzAuthSession.update_activityJ   s     (X\\ :r0   N)returnstr)r;   None)r;   bool)"__name__
__module____qualname____doc____tablename__r   r   __annotations__r   r   r
   r   r   r   r   r   r   r   r    r!   r	   r#   r$   r   r2   r&   r(   r)   r*   r/   r4   r6   r8   r:    r0   r.   r   r      s    $M#DABA )J)?%W[\G[\
 !.d4tV[a  !AL+  A+8dRVae  lK  ,LM(  L *7tdPl)mK&m(5fRj4Up(qJ%q(5dTOb(cJ%c 1>hPT>U`e0f-f;H[_I`ko;p8p ,GTEY]^I|^,Wee[_`J` $141HYaY]YaYaYcns#tJ t-:8T;Rckcgckckcm  yAx|  yA  yA  yC  .DJ*  D-:8T;R]a-bJ*b3@SWAXiqimiqiqis  C  4D0  Db5IJ;r0   r   N)rB   
__future__r   r   r   typingr   
sqlalchemyr   r   r	   r
   r   r   sqlalchemy.ormr   r   src.apps.base.models.baser   r   rE   r0   r.   <module>rK      s-    # '  H H 0 *=;$ =;r0   