
    khD5                     J   d Z ddlZddlmZ ddlmZ 	 ddlZddlm	Z	m
Z
 ddlm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y# e$ r ddlZY w xY w)z
    flaskext.session.sessions
    ~~~~~~~~~~~~~~~~~~~~~~~~~

    Server-side Sessions and SessionInterfaces.

    :copyright: (c) 2014 by Shipeng Feng.
    :license: BSD, see LICENSE for more details.
    N)datetime)uuid4)SessionInterfaceSessionMixin)CallbackDictc                       e Zd ZdZddZy)ServerSideSessionz)Baseclass for server-side based sessions.Nc                 b    d }t        j                  | ||       || _        d| _        d| _        y )Nc                     d| _         y )NT)modifiedselfs    S/var/www/teggl/fontify/venv/lib/python3.12/site-packages/flask_sessions/sessions.py	on_updatez-ServerSideSession.__init__.<locals>.on_update   s	     DM    TF)r   __init__sid	permanentr   )r   initialr   r   s       r   r   zServerSideSession.__init__   s.    	!dGY7r   )NN)__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"   +   r   r   r"   c                       e Zd Zy)MongoDBSessionNr   r   r   r   r$   r$   /   r   r   r$   c                       e Zd ZdZd Zy)NullSessionInterfacezAUsed to open a :class:`flask.sessions.NullSession` instance.
    c                      y Nr   )r   apprequests      r   open_sessionz!NullSessionInterface.open_session7   s    r   N)r   r   r   r   r+   r   r   r   r&   r&   3   s    r   r&   c                   0    e Zd ZdZeZeZd Zd Z	d Z
d Zy)RedisSessionInterfacezUses the Redis key-value store as a session backend.

    :param redis: A ``redis.Redis`` instance.
    :param key_prefix: A prefix that is added to all Redis store keys.
    c                 v    ddl m} | |       | _         || _        y |j                  |      | _         || _        y )Nr   )Redis)redisr/   from_url
key_prefix)r   urlr2   r/   s       r   r   zRedisSessionInterface.__init__E   s5    ;DJ % ,DJ$r   c                 (    t        t                     S r(   strr   r   s    r   _generate_sidz#RedisSessionInterface._generate_sidM       57|r   c                    |j                   j                  |j                        }|s"| j                         }| j	                  |      S | j
                  j                  | j                  |z         }|/	 | j                  j                  |      }| j	                  ||      S | j	                  |      S #  | j	                  |      cY S xY wNr   )	cookiesgetsession_cookie_namer7   session_classr0   r2   
serializerloads)r   r)   r*   r   valdatas         r   r+   z"RedisSessionInterface.open_sessionP   s    oo!!#"9"9:$$&C%%#%..jjnnT__s23?3,,S1))$C)88 !!c!**3))c)22s   5-B5 5Cc           	         | j                  |      }| j                  |      }|s]|j                  rP| j                  j	                  | j
                  |j                  z          |j                  |j                  ||       y | j                  |      }| j                  |      }| j                  ||      }| j                  j                  t        |            }	| j                  j                  | j
                  |j                  z   |	t!        |j"                  j%                                      |j                  r-|j'                  |j                  |j                  |||||       y y NdomainpathexpireshttponlyrG   rH   secure)get_cookie_domainget_cookie_pathr   r0   deleter2   r   delete_cookier>   get_cookie_httponlyget_cookie_secureget_expiration_timer@   dumpsdictsetexintpermanent_session_lifetimetotal_seconds
set_cookie)
r   r)   sessionresponserG   rH   rK   rL   rJ   rB   s
             r   save_sessionz"RedisSessionInterface.save_session^   s4   '',##C(

!!$//GKK"?@&&s'>'>.44 ' A ++C0'',**38oo##DM2

7;;6S;;IIKL	N 7 7$+h#)V   E r   N)r   r   r   r   pickler@   r   r?   r   r7   r+   r]   r   r   r   r-   r-   ;   s(     J M%+Er   r-   c                   <    e Zd ZdZeZeZd Zd Z	d Z
d Zd Zd Zy)	MemcachedSessionInterfacezA Session interface that uses memcached as backend.

    :param client: A ``memcache.Client`` instance.
    :param key_prefix: A prefix that is added to all Memcached store keys.
    c                 ^    || j                         }|t        d      || _        || _        y )Nzno memcache module found)_get_preferred_memcache_clientRuntimeErrorclientr2   )r   rd   r2   s      r   r   z"MemcachedSessionInterface.__init__   s4    >88:F~"#=>>$r   c                 (    t        t                     S r(   r5   r   s    r   r7   z'MemcachedSessionInterface._generate_sid   r8   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   serversrg   rj   s       r   rb   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 ' )rW   time)r   timeouts     r   _get_memcache_timeoutz/MemcachedSessionInterface._get_memcache_timeout   s(    
 W s499;''Gr   c                    |j                   j                  |j                        }|s"| j                         }| j	                  |      S | j
                  |z   }t        |t              r|j                  d      }| j                  j                  |      }|/	 | j                  j                  |      }| j	                  ||      S | j	                  |      S #  | j	                  |      cY S xY w)Nr;   utf-8)r<   r=   r>   r7   r?   r2   
isinstanceunicodeencoderd   r@   rA   )r   r)   r*   r   full_session_keyrB   rC   s          r   r+   z&MemcachedSessionInterface.open_session   s    oo!!#"9"9:$$&C%%#%..??S0&0/66w?kkoo./?3,,S1))$C)88 !!c!**3))c)22s   -C C.c           
         | j                  |      }| j                  |      }| j                  |j                  z   }t	        |t
              r|j                  d      }|sF|j                  r9| j                  j                  |       |j                  |j                  ||       y | j                  |      }| j                  |      }| j                  ||      }	| j                  j!                  t#        |            }
| j                  j%                  ||
| j'                  t)        |j*                  j-                                            |j                  r-|j/                  |j                  |j                  |	||||       y y )Nrq   rF   rI   )rM   rN   r2   r   rr   rs   rt   r   rd   rO   rP   r>   rQ   rR   rS   r@   rT   rU   setro   rW   rX   rY   rZ   )r   r)   r[   r\   rG   rH   ru   rK   rL   rJ   rB   s              r   r]   z&MemcachedSessionInterface.save_session   sW   '',##C(??W[[8&0/66w?""#34&&s'>'>.44 ' A ++C0'',**38oo##DM2(#t/I/IC::HHJK0M 	N 7 7$+h#)V   E r   N)r   r   r   r   r^   r@   r    r?   r   r7   rb   ro   r+   r]   r   r   r   r`   r`   ~   s2     J$M%, +" Er   r`   c                   ,    e Zd ZdZeZd Zd Zd Zd Z	y)FileSystemSessionInterfacea  Uses the :class:`werkzeug.contrib.cache.FileSystemCache` as a session
    backend.

    :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.
    c                 >    ddl m}  ||||      | _        || _        y )Nr   )FileSystemCache)	thresholdmode)werkzeug.contrib.cacher{   cacher2   )r   	cache_dirr|   r}   r2   r{   s         r   r   z#FileSystemSessionInterface.__init__   s    :$Y)$O
$r   c                 (    t        t                     S r(   r5   r   s    r   r7   z(FileSystemSessionInterface._generate_sid   r8   r   c                 2   |j                   j                  |j                        }|s"| j                         }| j	                  |      S | j
                  j                  | j                  |z         }|| j	                  ||      S | j	                  |      S r:   )r<   r=   r>   r7   r?   r   r2   )r   r)   r*   r   rC   s        r   r+   z'FileSystemSessionInterface.open_session   s    oo!!#"9"9:$$&C%%#%..zz~~doo34%%d%44!!c!**r   c           	         | j                  |      }| j                  |      }|s]|j                  rP| j                  j	                  | j
                  |j                  z          |j                  |j                  ||       y | j                  |      }| j                  |      }| j                  ||      }t        |      }	| j                  j                  | j
                  |j                  z   |	t        |j                  j!                                      |j                  r-|j#                  |j                  |j                  |||||       y y rE   )rM   rN   r   r   rO   r2   r   rP   r>   rQ   rR   rS   rU   rw   rW   rX   rY   rZ   )
r   r)   r[   r\   rG   rH   rK   rL   rJ   rC   s
             r   r]   z'FileSystemSessionInterface.save_session  s#   '',##C(

!!$//GKK"?@&&s'>'>.44 ' A ++C0'',**38G}

t4dS;;IIKL	N 7 7$+h#)V   E r   N)
r   r   r   r   r"   r?   r   r7   r+   r]   r   r   r   ry   ry      s#     &M%
+Er   ry   c                   0    e Zd ZdZeZeZd Zd Z	d Z
d Zy)MongoDBSessionInterfacea  A Session interface that uses mongodb as backend.

    :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.
    c                 X    |ddl m}  |       }|| _        ||   |   | _        || _        y )Nr   )MongoClient)pymongor   rd   storer2   )r   rd   db
collectionr2   r   s         r   r   z MongoDBSessionInterface.__init__1  s/    >+ ]FBZ
+
$r   c                 (    t        t                     S r(   r5   r   s    r   r7   z%MongoDBSessionInterface._generate_sid9  r8   r   c                    |j                   j                  |j                        }|s"| j                         }| j	                  |      S | j
                  j                  d| j                  |z   i      }|r(|j                  d      t        j                         kD  r|nd }|=	 |d   }| j                  j                  t        |            }| j	                  ||      S | j	                  |      S #  | j	                  |      cY S xY w)Nr;   id
expirationrB   )r<   r=   r>   r7   r?   r   find_oner2   r   utcnowr@   rA   r6   )r   r)   r*   r   documentrB   rC   s          r   r+   z$MongoDBSessionInterface.open_session<  s    oo!!#"9"9:$$&C%%#%..::&&doo.C'DE'\\,/(//2CC IM 	3uo,,SX6))$C)88 !!c!**3))c)22s   #;C1 1Dc           	      v   | j                  |      }| j                  |      }| j                  |j                  z   }|sH|j                  r;| j
                  j                  d|i       |j                  |j                  ||       y | j                  |      }| j                  |      }| j                  ||      }	| j                  j                  t        |            }
| j
                  j                  d|i||
|	dd       |j                  r-|j!                  |j                  |j                  |	||||       y y )Nr   rF   )r   rB   r   TrI   )rM   rN   r2   r   r   r   rO   rP   r>   rQ   rR   rS   r@   rT   rU   updaterZ   )r   r)   r[   r\   rG   rH   store_idrK   rL   rJ   rB   s              r   r]   z$MongoDBSessionInterface.save_sessionM  s/   '',##C(??W[[0

!!4"23&&s'>'>.44 ' A ++C0'',**38oo##DM2

4*!)"%)0237	9  7 7$+h#)V   E r   N)r   r   r   r   r^   r@   r$   r?   r   r7   r+   r]   r   r   r   r   r   %  s(     J"M%+" Er   r   )r   rm   r   uuidr   cPickler^   ri   flask.sessionsr   r   werkzeug.datastructuresr   r	   r   r    r"   r$   r&   r-   r`   ry   r   r   r   r   <module>r      s       : 0	l 		$ 		( 		) 		& 	+ @E, @EFeE 0 eEP<E!1 <E~HE. HEk  s   B 	B"!B"