
    ٘gRW                        d dl Z d dlZd dlmZ d dlmZ 	 d dlZd dlm	Z
 d dlmZ d dlmZ d dlmZmZmZ e j$                  d    dk(  ZeseZneZd	 Z G d
 dee      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de
      Z	 G d de	      Z G d de	      Z G d de	      Z  G d de	      Z! G d  d!e	      Z" G d" d#e	      Z#y# e$ r d dlZY w xY w)$    N)datetime)uuid4)SessionInterface)SessionMixin)CallbackDict)SignerBadSignature
want_bytes   c                 F    | j                   dz  dz  dz  | j                  z   S )N<      )daysseconds)tds    a/var/www/html/janusapi.askjitendra.com/env/lib/python3.12/site-packages/flask_session/sessions.pytotal_secondsr      s#    77R<"r!BJJ..    c                       e Zd ZdZddZy)ServerSideSessionz)Baseclass for server-side based sessions.Nc                 f    d }t        j                  | ||       || _        |r|| _        d| _        y )Nc                     d| _         y )NT)modifiedselfs    r   	on_updatez-ServerSideSession.__init__.<locals>.on_update   s	     DMr   F)r   __init__sid	permanentr   )r   initialr   r   r   s        r   r   zServerSideSession.__init__   s1    	!dGY7&DNr   )NNN)__name__
__module____qualname____doc__r    r   r   r   r      s
    3r   r   c                       e Zd Zy)RedisSessionNr!   r"   r#   r%   r   r   r'   r'   (       r   r'   c                       e Zd Zy)MemcachedSessionNr(   r%   r   r   r+   r+   ,   r)   r   r+   c                       e Zd Zy)FileSystemSessionNr(   r%   r   r   r-   r-   0   r)   r   r-   c                       e Zd Zy)MongoDBSessionNr(   r%   r   r   r/   r/   4   r)   r   r/   c                       e Zd Zy)SqlAlchemySessionNr(   r%   r   r   r1   r1   8   r)   r   r1   c                       e Zd Zd Zd Zy)r   c                 (    t        t                     S N)strr   r   s    r   _generate_sidzSessionInterface._generate_sid>   s    57|r   c                 L    |j                   sy t        |j                   dd      S )Nzflask-sessionhmac)saltkey_derivation)
secret_keyr   )r   apps     r   _get_signerzSessionInterface._get_signerA   s$    ~~cnn?%+- 	-r   N)r!   r"   r#   r6   r=   r%   r   r   r   r   <   s    -r   r   c                       e Zd ZdZd Zy)NullSessionInterfacezAUsed to open a :class:`flask.sessions.NullSession` instance.
    c                      y r4   r%   )r   r<   requests      r   open_sessionz!NullSessionInterface.open_sessionL   s    r   N)r!   r"   r#   r$   rB   r%   r   r   r?   r?   H   s    r   r?   c                   ,    e Zd ZdZeZeZddZd Z	d Z
y)RedisSessionInterfacea|  Uses the Redis key-value store as a session backend.

    .. versionadded:: 0.2
        The `use_signer` parameter was added.

    :param redis: A ``redis.Redis`` instance.
    :param key_prefix: A prefix that is added to all Redis store keys.
    :param use_signer: Whether to sign the session id cookie or not.
    :param permanent: Whether to use permanent session or not.
    c                 |    |ddl m}  |       }|| _         || _        || _        || _        t        | d      | _        y )Nr   )Redisget_cookie_samesite)redisrF   
key_prefix
use_signerr   hasattrhas_same_site_capability)r   rH   rI   rJ   r   rF   s         r   r   zRedisSessionInterface.__init___   s=    =#GE
$$"(/6K(L%r   c                 0   |j                   j                  |j                  d         }|s-| j                         }| j	                  || j
                        S | j                  r6| j                  |      }|y 	 |j                  |      }|j                         }t        s"t        |t              s|j                  dd      }| j                  j                  | j                  |z         }|/	 | j                   j#                  |      }| j	                  ||      S | j	                  || j
                        S # t        $ r0 | j                         }| j	                  || j
                        cY S w xY w#  | j	                  || j
                        cY S xY w)NSESSION_COOKIE_NAMEr   r   utf-8strictr   )cookiesgetconfigr6   session_classr   rJ   r=   unsigndecoder	   PY2
isinstance	text_typerH   rI   
serializerloads)r   r<   rA   r   signersid_as_bytesvaldatas           r   rB   z"RedisSessionInterface.open_sessioni   sr   oo!!#**-B"CD$$&C%%#%HH??%%c*F~M%}}S1"))+
 :c95**Wh/CjjnnT__s23?M,,S1))$C)88 !!cT^^!DD   M((*))cT^^)LLMM))cT^^)LLs   9!D8 --E4 86E10E14Fc           	      v   | j                  |      }| j                  |      }|s`|j                  rS| j                  j	                  | j
                  |j                  z          |j                  |j                  d   ||       y i }| j                  |      }| j                  |      }| j                  r| j                  |      |d<   | j                  ||      }	| j                  j                  t!        |            }
| j                  j#                  | j
                  |j                  z   |
t%        |j&                               | j(                  r4| j+                  |      j-                  t/        |j                              }n|j                  } |j0                  |j                  d   |f|	||||d| y )NrN   domainpathsamesite)namevaluetimeexpireshttponlyrd   re   secure)get_cookie_domainget_cookie_pathr   rH   deleterI   r   delete_cookierU   get_cookie_httponlyget_cookie_securerL   rG   get_expiration_timer\   dumpsdictsetexr   permanent_session_lifetimerJ   r=   signr
   
set_cookie)r   r<   sessionresponserd   re   conditional_cookie_kwargsrl   rm   rk   r`   
session_ids               r   save_sessionz"RedisSessionInterface.save_session   s   '',##C(

!!$//GKK"?@&&szz2G'H.44 ' A %'!++C0'',((484L4LS4Q%j1**38oo##DM2

doo;3+C,J,JK 	 	M??))#.33Jw{{4KLJ JCJJ'<=z 	9$+h#)V	9 8	9r   NFT)r!   r"   r#   r$   pickler\   r'   rV   r   rB   r   r%   r   r   rD   rD   P   s$    	 J MME6#9r   rD   c                   8    e Zd ZdZeZeZddZd Z	d Z
d Zd Zy)	MemcachedSessionInterfacea  A Session interface that uses memcached as backend.

    .. versionadded:: 0.2
        The `use_signer` parameter was added.

    :param client: A ``memcache.Client`` instance.
    :param key_prefix: A prefix that is added to all Memcached store keys.
    :param use_signer: Whether to sign the session id cookie or not.
    :param permanent: Whether to use permanent session or not.
    c                     || j                         }|t        d      || _        || _        || _        || _        t        | d      | _        y )Nzno memcache module foundrG   )_get_preferred_memcache_clientRuntimeErrorclientrI   rJ   r   rK   rL   )r   r   rI   rJ   r   s        r   r   z"MemcachedSessionInterface.__init__   sR    >88:F~"#=>>$$"(/6K(L%r   c                     dg}	 dd l }|j                  |      S # t        $ r Y nw xY w	 dd l}|j                  |      S # t        $ r Y y w xY w)Nz127.0.0.1:11211r   )pylibmcClientImportErrormemcache)r   serversr   r   s       r   r   z8MemcachedSessionInterface._get_preferred_memcache_client   sd    $%	+ >>'**  		
	, ??7++  		s    	&&? 	A
Ac                 P    |dkD  r |t        t        j                               z  }|S )z
        Memcached deals with long (> 30 days) timeouts in a special
        way. Call this function to obtain a safe value for your timeout.
        i ' )intri   )r   timeouts     r   _get_memcache_timeoutz/MemcachedSessionInterface._get_memcache_timeout   s(    
 W s499;''Gr   c                 T   |j                   j                  |j                  d         }|s-| j                         }| j	                  || j
                        S | j                  r6| j                  |      }|y 	 |j                  |      }|j                         }| j                  |z   }t        r!t        |t              r|j                  d      }| j                   j                  |      }|@	 t        st#        |      }| j$                  j'                  |      }| j	                  ||      S | j	                  || j
                        S # t        $ r0 | j                         }| j	                  || j
                        cY S w xY w#  | j	                  || j
                        cY S xY w)NrN   rO   rP   rR   )rS   rT   rU   r6   rV   r   rJ   r=   rW   rX   r	   rI   rY   rZ   unicodeencoder   r
   r\   r]   )	r   r<   rA   r   r^   r_   full_session_keyr`   ra   s	            r   rB   z&MemcachedSessionInterface.open_session   s   oo!!#**-B"CD$$&C%%#%HH??%%c*F~M%}}S1"))+
  ??S0:.8/66w?kkoo./?M$S/C,,S1))$C)88 !!cT^^!DD!   M((*))cT^^)LLMM))cT^^)LLs   9!E
 .>F 
6FFF'c           	         | j                  |      }| j                  |      }| j                  |j                  z   }t        r!t        |t              r|j                  d      }|sI|j                  r<| j                  j                  |       |j                  |j                  d   ||       y i }| j                  |      }| j                  |      }	| j                  r| j!                  |      |d<   | j#                  ||      }
t        s&| j$                  j'                  t)        |      d      }n$| j$                  j'                  t)        |            }| j                  j+                  ||| j-                  t/        |j0                                     | j2                  r4| j5                  |      j7                  t9        |j                              }n|j                  } |j:                  |j                  d   |f|
||||	d| y )NrP   rN   rc   rf   r   rj   )rn   ro   rI   r   rY   rZ   r   r   r   r   rp   rq   rU   rr   rs   rL   rG   rt   r\   ru   rv   setr   r   rx   rJ   r=   ry   r
   rz   )r   r<   r{   r|   rd   re   r   r}   rl   rm   rk   r`   r~   s                r   r   z&MemcachedSessionInterface.save_session  s   '',##C(??W[[8:.8/66w?""#34&&szz2G'H.44 ' A$&!++C0'',((484L4LS4Q%j1**38//''Wq9C//''W6C(#t/I/I%c&D&DE0G 	H??))#.33Jw{{4KLJ JCJJ'<=z 	9$+h#)V	9 8	9r   Nr   )r!   r"   r#   r$   r   r\   r+   rV   r   r   r   rB   r   r%   r   r   r   r      s.    	 J$M	M, E< 9r   r   c                   *    e Zd ZdZeZ	 ddZd Zd Zy)FileSystemSessionInterfacean  Uses the :class:`cachelib.file.FileSystemCache` as a session backend.

    .. versionadded:: 0.2
        The `use_signer` parameter was added.

    :param cache_dir: the directory where session files are stored.
    :param threshold: the maximum number of items the session stores before it
                      starts deleting some.
    :param mode: the file mode wanted for the session files, default 0600
    :param key_prefix: A prefix that is added to FileSystemCache store keys.
    :param use_signer: Whether to sign the session id cookie or not.
    :param permanent: Whether to use permanent session or not.
    c                 |    ddl m}  ||||      | _        || _        || _        || _        t        | d      | _        y )Nr   )FileSystemCache)	thresholdmoderG   )cachelib.filer   cacherI   rJ   r   rK   rL   )r   	cache_dirr   r   rI   rJ   r   r   s           r   r   z#FileSystemSessionInterface.__init__5  s:    1$Y)$O
$$"(/6K(L%r   c                 `   |j                   j                  |j                  d         }|s-| j                         }| j	                  || j
                        S | j                  r6| j                  |      }|y 	 |j                  |      }|j                         }| j                  j                  | j                  |z         }|| j	                  ||      S | j	                  || j
                        S # t        $ r0 | j                         }| j	                  || j
                        cY S w xY w)NrN   rO   rR   )rS   rT   rU   r6   rV   r   rJ   r=   rW   rX   r	   r   rI   )r   r<   rA   r   r^   r_   ra   s          r   rB   z'FileSystemSessionInterface.open_session>  s   oo!!#**-B"CD$$&C%%#%HH??%%c*F~M%}}S1"))+
 zz~~doo34%%d%44!!cT^^!DD   M((*))cT^^)LLMs   9!C4 46D-,D-c           	      B   | j                  |      }| j                  |      }|s`|j                  rS| j                  j	                  | j
                  |j                  z          |j                  |j                  d   ||       y i }| j                  |      }| j                  |      }| j                  r| j                  |      |d<   | j                  ||      }	t        |      }
| j                  j                  | j
                  |j                  z   |
t!        |j"                               | j$                  r4| j'                  |      j)                  t+        |j                              }n|j                  } |j,                  |j                  d   |f|	||||d| y )NrN   rc   rf   rj   )rn   ro   r   r   rp   rI   r   rq   rU   rr   rs   rL   rG   rt   rv   r   r   rx   rJ   r=   ry   r
   rz   )r   r<   r{   r|   rd   re   r}   rl   rm   rk   ra   r~   s               r   r   z'FileSystemSessionInterface.save_sessionS  sr   '',##C(

!!$//GKK"?@&&szz2G'H.44 ' A$&!++C0'',((484L4LS4Q%j1**38G}

t4d$S%C%CD	F??))#.33Jw{{4KLJ JCJJ'<=z 	9$+h#)V	9 8	9r   Nr   )	r!   r"   r#   r$   r-   rV   r   rB   r   r%   r   r   r   r   $  s$     &M .2ME*9r   r   c                   0    e Zd ZdZeZeZ	 	 ddZd Z	d Z
y)MongoDBSessionInterfacea  A Session interface that uses mongodb as backend.

    .. versionadded:: 0.2
        The `use_signer` parameter was added.

    :param client: A ``pymongo.MongoClient`` instance.
    :param db: The database you want to use.
    :param collection: The collection you want to use.
    :param key_prefix: A prefix that is added to all MongoDB store keys.
    :param use_signer: Whether to sign the session id cookie or not.
    :param permanent: Whether to use permanent session or not.
    c                     |ddl m}  |       }|| _        ||   |   | _        || _        || _        || _        t        | d      | _        y )Nr   )MongoClientrG   )	pymongor   r   storerI   rJ   r   rK   rL   )r   r   db
collectionrI   rJ   r   r   s           r   r   z MongoDBSessionInterface.__init__  sM    >+ ]FBZ
+
$$"(/6K(L%r   c                    |j                   j                  |j                  d         }|s-| j                         }| j	                  || j
                        S | j                  r6| j                  |      }|y 	 |j                  |      }|j                         }| j                  |z   }| j                  j                  d|i      }|rE|j                  d      t        j                         k  r| j                  j!                  d|i       d }|=	 |d   }| j"                  j%                  t'        |            }	| j	                  |	|      S | j	                  || j
                        S # t        $ r0 | j                         }| j	                  || j
                        cY S w xY w#  | j	                  || j
                        cY S xY w)NrN   rO   id
expirationr`   rR   )rS   rT   rU   r6   rV   r   rJ   r=   rW   rX   r	   rI   r   find_oner   utcnowremover\   r]   r
   )
r   r<   rA   r   r^   r_   store_iddocumentr`   ra   s
             r   rB   z$MongoDBSessionInterface.open_session  s   oo!!#**-B"CD$$&C%%#%HH??%%c*F~M%}}S1"))+
 ??S(::&&h'78\2hoo6GGJJtX./HMuo,,Z_=))$C)88 !!cT^^!DD#   M((*))cT^^)LLMM))cT^^)LLs   9!E) ;F% )6F"!F"%Gc           	      4   | j                  |      }| j                  |      }| j                  |j                  z   }|sK|j                  r>| j
                  j                  d|i       |j                  |j                  d   ||       y i }| j                  |      }| j                  |      }	| j                  r| j                  |      |d<   | j                  ||      }
| j                  j                  t!        |            }| j
                  j#                  d|i|||
dd       | j$                  r4| j'                  |      j)                  t+        |j                              }n|j                  } |j,                  |j                  d   |f|
||||	d| y )Nr   rN   rc   rf   )r   r`   r   Trj   )rn   ro   rI   r   r   r   r   rq   rU   rr   rs   rL   rG   rt   r\   ru   rv   updaterJ   r=   ry   r
   rz   )r   r<   r{   r|   rd   re   r   r}   rl   rm   rk   r`   r~   s                r   r   z$MongoDBSessionInterface.save_session  s   '',##C(??W[[0

!!4"23&&szz2G'H.44 ' A$&!++C0'',((484L4LS4Q%j1**38oo##DM2

4*!)"%)0237	9 ??))#.33Jw{{4KLJ JCJJ'<=z 	9$+h#)V	9 8	9r   Nr   )r!   r"   r#   r$   r   r\   r/   rV   r   rB   r   r%   r   r   r   r   p  s*     J"MFK
ME>9r   r   c                   0    e Zd ZdZeZeZ	 	 ddZd Z	d Z
y)SqlAlchemySessionInterfacea  Uses the Flask-SQLAlchemy from a flask app as a session backend.

    .. versionadded:: 0.2

    :param app: A Flask app instance.
    :param db: A Flask-SQLAlchemy instance.
    :param table: The table name you want to use.
    :param key_prefix: A prefix that is added to all store keys.
    :param use_signer: Whether to sign the session id cookie or not.
    :param permanent: Whether to use permanent session or not.
    c                      |ddl m}  ||      }| _        | _        | _        | _        t         d       _         G  fdd j                  j                        }| _	        y )Nr   )
SQLAlchemyrG   c                      e Zd ZW ZW  j                  j                  W  j                  j                  d      ZW  j                  j                  W  j                  j                  d      d      Z	W  j                  j                  W  j                  j                        ZW  j                  j                  W  j                  j                        Zd Zd Zy)4SqlAlchemySessionInterface.__init__.<locals>.SessionT)primary_key   )uniquec                 .    || _         || _        || _        y r4   )r~   ra   expiry)r   r~   ra   r   s       r   r   z=SqlAlchemySessionInterface.__init__.<locals>.Session.__init__  s    ", 	$r   c                      d| j                   z  S )Nz<Session data %s>)ra   r   s    r   __repr__z=SqlAlchemySessionInterface.__init__.<locals>.Session.__repr__  s    *TYY66r   N)r!   r"   r#   __tablename__r   ColumnIntegerr   Stringr~   LargeBinaryra   DateTimer   r   r   )r   tables   r   Sessionr     s    !MTBBs(;DIJ77>>$''"5"56DWW^^DGG$4$45F%
7r   r   )
flask_sqlalchemyr   r   rI   rJ   r   rK   rL   Modelsql_session_model)	r   r<   r   r   rI   rJ   r   r   r   s	   `  `     r   r   z#SqlAlchemySessionInterface.__init__  s^    :3CB$$"(/6K(L%	7dggmm 	7" ")r   c                    |j                   j                  |j                  d         }|s-| j                         }| j	                  || j
                        S | j                  r6| j                  |      }|y 	 |j                  |      }|j                         }| j                  |z   }| j                  j                  j                  |      j                         }|rl|j                   t#        j$                         k  rK| j&                  j(                  j+                  |       | j&                  j(                  j-                          d }|rD	 |j.                  }| j0                  j3                  t5        |            }	| j	                  |	|      S | j	                  || j
                        S # t        $ r0 | j                         }| j	                  || j
                        cY S w xY w#  | j	                  || j
                        cY S xY w)NrN   rO   r~   rR   )rS   rT   rU   r6   rV   r   rJ   r=   rW   rX   r	   rI   r   query	filter_byfirstr   r   r   r   r{   rp   commitra   r\   r]   r
   )
r   r<   rA   r   r^   r_   r   saved_sessionr`   ra   s
             r   rB   z'SqlAlchemySessionInterface.open_session  s   oo!!#**-B"CD$$&C%%#%HH??%%c*F~M%}}S1"))+
 ??S(..44>> ? !!& 	]11X__5FFGGOO""=1GGOO""$ MM#((,,Z_=))$C)88 !!cT^^!DD'   M((*))cT^^)LLM"M))cT^^)LLs   9!F. AG* .6G'&G'*Hc           	         | j                  |      }| j                  |      }| j                  |j                  z   }| j                  j
                  j                  |      j                         }|sy|j                  rl|rI| j                  j                  j                  |       | j                  j                  j                          |j                  |j                  d   ||       y i }| j                  |      }	| j!                  |      }
| j"                  r| j%                  |      |d<   | j'                  ||      }| j(                  j+                  t-        |            }|r3||_        ||_        | j                  j                  j                          n\| j	                  |||      }| j                  j                  j3                  |       | j                  j                  j                          | j4                  r4| j7                  |      j9                  t;        |j                              }n|j                  } |j<                  |j                  d   |f||	|||
d| y )Nr   rN   rc   rf   rj   )rn   ro   rI   r   r   r   r   r   r   r   r{   rp   r   rq   rU   rr   rs   rL   rG   rt   r\   ru   rv   ra   r   addrJ   r=   ry   r
   rz   )r   r<   r{   r|   rd   re   r   r   r}   rl   rm   rk   r`   new_sessionr~   s                  r   r   z'SqlAlchemySessionInterface.save_session  s   '',##C(??W[[0..44>> ? !!& 	 GGOO**=9GGOO**,&&szz2G'H.44 ' A$&!++C0'',((484L4LS4Q%j1**38oo##DM2!$M#*M GGOO""$003HKGGOO,GGOO""$??))#.33Jw{{4KLJ JCJJ'<=z 	9$+h#)V	9 8	9r   Nr   )r!   r"   r#   r$   r   r\   r1   rV   r   rB   r   r%   r   r   r   r     s*    
 J%M>C)<EB%9r   r   )$sysri   r   uuidr   cPickler   r   flask.sessionsr   FlaskSessionInterfacer   werkzeug.datastructuresr   itsdangerousr   r	   r
   version_inforY   r5   r[   r   r   r   r'   r+   r-   r/   r1   r?   rD   r   r   r   r   r%   r   r   <module>r      s!   
    E ' 0 9 9 
qQ
II/
l 
	$ 		( 		) 		& 		) 		-, 	-+ W9, W9tw9 0 w9tI9!1 I9XY9. Y9xt9!1 t9K  s   C# #	C/.C/