3
ãdeŸW  ã               @   sˆ  d Z ddlZddlZddlmZ ddlmZ yddlZW n ek
rT   ddlZY nX 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
d„ ZG dd„ deeƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZ
G dd„ de
ƒZG dd„ de
ƒZ G dd„ de
ƒZ!G d d!„ d!e
ƒZ"G d"d#„ d#e
ƒZ#G d$d%„ d%e
ƒZ$dS )&zÄ
    flask_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)ÚCallbackDict)ÚSignerÚBadSignatureÚ
want_bytesé   c             C   s   | j d d d | j S )Né<   é   )ÚdaysÚseconds)Útd© r   úM/var/www/agendate/envp3/lib/python3.6/site-packages/flask_session/sessions.pyÚtotal_seconds!   s    r   c               @   s   e Zd ZdZddd„ZdS )ÚServerSideSessionz)Baseclass for server-side based sessions.Nc             C   s0   dd„ }t j| ||ƒ || _|r&|| _d| _d S )Nc             S   s
   d| _ d S )NT)Úmodified)Úselfr   r   r   Ú	on_update)   s    z-ServerSideSession.__init__.<locals>.on_updateF)r   Ú__init__ÚsidÚ	permanentr   )r   Úinitialr   r   r   r   r   r   r   (   s    zServerSideSession.__init__)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   %   s   r   c               @   s   e Zd ZdS )ÚRedisSessionN)r   r   r   r   r   r   r   r   2   s   r   c               @   s   e Zd ZdS )ÚMemcachedSessionN)r   r   r   r   r   r   r   r    6   s   r    c               @   s   e Zd ZdS )ÚFileSystemSessionN)r   r   r   r   r   r   r   r!   :   s   r!   c               @   s   e Zd ZdS )ÚMongoDBSessionN)r   r   r   r   r   r   r   r"   >   s   r"   c               @   s   e Zd ZdS )ÚSqlAlchemySessionN)r   r   r   r   r   r   r   r#   B   s   r#   c               @   s   e Zd Zdd„ Zdd„ ZdS )r   c             C   s
   t tƒ ƒS )N)Ústrr   )r   r   r   r   Ú_generate_sidH   s    zSessionInterface._generate_sidc             C   s   |j s
d S t|j ddd�S )Nzflask-sessionÚhmac)ZsaltZkey_derivation)Z
secret_keyr   )r   Úappr   r   r   Ú_get_signerK   s    zSessionInterface._get_signerN)r   r   r   r%   r(   r   r   r   r   r   F   s   r   c               @   s   e Zd ZdZdd„ ZdS )ÚNullSessionInterfacezAUsed to open a :class:`flask.sessions.NullSession` instance.
    c             C   s   d S )Nr   )r   r'   Úrequestr   r   r   Úopen_sessionV   s    z!NullSessionInterface.open_sessionN)r   r   r   r   r+   r   r   r   r   r)   R   s   r)   c               @   s2   e Zd ZdZeZeZddd„Zdd„ Z	dd	„ Z
d
S )Ú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.
    FTc             C   sB   |d krddl m} |ƒ }|| _ || _|| _|| _t| dƒ| _d S )Nr   )ÚRedisÚget_cookie_samesite)Úredisr-   Ú
key_prefixÚ
use_signerr   ÚhasattrÚhas_same_site_capability)r   r/   r0   r1   r   r-   r   r   r   r   i   s    zRedisSessionInterface.__init__c             C   s  |j j|jƒ}|s*| jƒ }| j|| jd�S | jr†| j|ƒ}|d krFd S y|j|ƒ}|j	ƒ }W n( t
k
r„   | jƒ }| j|| jd�S X t r¤t|tƒ r¤|j	ddƒ}| jj| j| ƒ}|d k	� ròy| jj|ƒ}| j||d�S    | j|| jd�S | j|| jd�S )N)r   r   zutf-8Ústrict)r   )ÚcookiesÚgetÚsession_cookie_namer%   Úsession_classr   r1   r(   ÚunsignÚdecoder   ÚPY2Ú
isinstanceÚ	text_typer/   r0   Ú
serializerÚloads)r   r'   r*   r   ÚsignerÚsid_as_bytesÚvalÚdatar   r   r   r+   s   s0    


z"RedisSessionInterface.open_sessionc             C   sú   | j |ƒ}| j|ƒ}|sH|jrD| jj| j|j ƒ |j|j||d� d S i }| j	|ƒ}| j
|ƒ}| jrt| j|ƒ|d< | j||ƒ}	| jjt|ƒƒ}
| jj| j|j |
t|jƒd� | jrÎ| j|ƒjt|jƒƒ}n|j}|j|j|f|	||||dœ|—Ž d S )N)ÚdomainÚpathÚsamesite)ÚnameÚvalueÚtime)ÚexpiresÚhttponlyrD   rE   Úsecure)Úget_cookie_domainÚget_cookie_pathr   r/   Údeleter0   r   Údelete_cookier7   Úget_cookie_httponlyÚget_cookie_securer3   r.   Úget_expiration_timer>   ÚdumpsÚdictÚsetexr   Úpermanent_session_lifetimer1   r(   Úsignr	   Ú
set_cookie)r   r'   ÚsessionÚresponserD   rE   Úconditional_cookie_kwargsrK   rL   rJ   rB   Ú
session_idr   r   r   Úsave_sessionŽ   s0    





z"RedisSessionInterface.save_sessionN)FT)r   r   r   r   Úpickler>   r   r8   r   r+   r^   r   r   r   r   r,   Z   s   


r,   c               @   sB   e Zd ZdZeZeZddd„Zdd„ Z	dd	„ Z
d
d„ Zdd„ ZdS )Ú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.
    FTc             C   sH   |d kr | j ƒ }|d kr tdƒ‚|| _|| _|| _|| _t| dƒ| _d S )Nzno memcache module foundr.   )Ú_get_preferred_memcache_clientÚRuntimeErrorÚclientr0   r1   r   r2   r3   )r   rc   r0   r1   r   r   r   r   r   Ã   s    z"MemcachedSessionInterface.__init__c             C   sb   dg}ydd l }W n tk
r&   Y nX |j|ƒS ydd l}W n tk
rR   Y nX |j|ƒS d S )Nz127.0.0.1:11211r   )ÚpylibmcÚImportErrorÚClientÚmemcache)r   Zserversrd   rg   r   r   r   ra   Î   s    
z8MemcachedSessionInterface._get_preferred_memcache_clientc             C   s   |dkr|t tjƒ ƒ7 }|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   Útimeoutr   r   r   Ú_get_memcache_timeoutÞ   s    z/MemcachedSessionInterface._get_memcache_timeoutc       	      C   s  |j j|jƒ}|s*| jƒ }| j|| jd�S | jr†| j|ƒ}|d krFd S y|j|ƒ}|j	ƒ }W n( t
k
r„   | jƒ }| j|| jd�S X | j| }tr¨t|tƒr¨|jdƒ}| jj|ƒ}|d k	� rüy&tsÌt|ƒ}| jj|ƒ}| j||d�S    | j|| jd�S | j|| jd�S )N)r   r   zutf-8)r   )r5   r6   r7   r%   r8   r   r1   r(   r9   r:   r   r0   r;   r<   ÚunicodeÚencoderc   r	   r>   r?   )	r   r'   r*   r   r@   rA   Úfull_session_keyrB   rC   r   r   r   r+   í   s6    




z&MemcachedSessionInterface.open_sessionc             C   s,  | j |ƒ}| j|ƒ}| j|j }tr8t|tƒr8|jdƒ}|sd|jr`| j	j
|ƒ |j|j||d� d S i }| j|ƒ}| j|ƒ}	| jr�| j|ƒ|d< | j||ƒ}
ts´| jjt|ƒdƒ}n| jjt|ƒƒ}| j	j||| jt|jƒƒƒ | j�r | j|ƒjt|jƒƒ}n|j}|j|j|f|
||||	dœ|—Ž d S )Nzutf-8)rD   rE   rF   r   )rJ   rK   rD   rE   rL   )rM   rN   r0   r   r;   r<   rk   rl   r   rc   rO   rP   r7   rQ   rR   r3   r.   rS   r>   rT   rU   Úsetrj   r   rW   r1   r(   rX   r	   rY   )r   r'   rZ   r[   rD   rE   rm   r\   rK   rL   rJ   rB   r]   r   r   r   r^     s:    






z&MemcachedSessionInterface.save_sessionN)FT)r   r   r   r   r_   r>   r    r8   r   ra   rj   r+   r^   r   r   r   r   r`   ´   s   

r`   c               @   s.   e Zd ZdZeZddd„Zdd„ Zdd	„ Zd
S )Ú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.
    FTc             C   s>   ddl m} ||||d�| _|| _|| _|| _t| dƒ| _d S )Nr   )ÚFileSystemCache)Ú	thresholdÚmoder.   )Zcachelib.filerp   Úcacher0   r1   r   r2   r3   )r   Ú	cache_dirrq   rr   r0   r1   r   rp   r   r   r   r   ?  s    z#FileSystemSessionInterface.__init__c             C   s¾   |j j|jƒ}|s*| jƒ }| j|| jd�S | jr†| j|ƒ}|d krFd S y|j|ƒ}|j	ƒ }W n( t
k
r„   | jƒ }| j|| jd�S X | jj| j| ƒ}|d k	r®| j||d�S | j|| jd�S )N)r   r   )r   )r5   r6   r7   r%   r8   r   r1   r(   r9   r:   r   rs   r0   )r   r'   r*   r   r@   rA   rC   r   r   r   r+   H  s$    

z'FileSystemSessionInterface.open_sessionc             C   sð   | j |ƒ}| j|ƒ}|sH|jrD| jj| j|j ƒ |j|j||d� d S i }| j	|ƒ}| j
|ƒ}| jrt| j|ƒ|d< | j||ƒ}	t|ƒ}
| jj| j|j |
t|jƒƒ | jrÄ| j|ƒjt|jƒƒ}n|j}|j|j|f|	||||dœ|—Ž d S )N)rD   rE   rF   )rJ   rK   rD   rE   rL   )rM   rN   r   rs   rO   r0   r   rP   r7   rQ   rR   r3   r.   rS   rU   rn   r   rW   r1   r(   rX   r	   rY   )r   r'   rZ   r[   rD   rE   r\   rK   rL   rJ   rC   r]   r   r   r   r^   ]  s0    





z'FileSystemSessionInterface.save_sessionN)FT)	r   r   r   r   r!   r8   r   r+   r^   r   r   r   r   ro   .  s
   
ro   c               @   s2   e Zd ZdZeZeZddd„Zdd„ Z	dd	„ Z
d
S )Ú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.
    FTc             C   sP   |d krddl m} |ƒ }|| _|| | | _|| _|| _|| _t| dƒ| _d S )Nr   )ÚMongoClientr.   )	Zpymongorv   rc   Ústorer0   r1   r   r2   r3   )r   rc   ÚdbZ
collectionr0   r1   r   rv   r   r   r   r   ‹  s    z MongoDBSessionInterface.__init__c       
      C   s"  |j j|jƒ}|s*| jƒ }| j|| jd�S | jr†| j|ƒ}|d krFd S y|j|ƒ}|j	ƒ }W n( t
k
r„   | jƒ }| j|| jd�S X | j| }| jjd|iƒ}|rÊ|jdƒtjƒ krÊ| jjd|iƒ d }|d k	�ry&|d }| jjt|ƒƒ}	| j|	|d�S    | j|| jd�S | j|| jd�S )N)r   r   ÚidÚ
expirationrB   )r   )r5   r6   r7   r%   r8   r   r1   r(   r9   r:   r   r0   rw   Zfind_oner   ÚutcnowÚremover>   r?   r	   )
r   r'   r*   r   r@   rA   Ústore_idÚdocumentrB   rC   r   r   r   r+   —  s6    



z$MongoDBSessionInterface.open_sessionc             C   sþ   | j |ƒ}| j|ƒ}| j|j }|sP|jrL| jjd|iƒ |j|j||d� d S i }| j	|ƒ}| j
|ƒ}	| jr|| j|ƒ|d< | j||ƒ}
| jjt|ƒƒ}| jjd|i|||
dœdƒ | jrÒ| j|ƒjt|jƒƒ}n|j}|j|j|f|
||||	dœ|—Ž d S )Nry   )rD   rE   rF   )ry   rB   rz   T)rJ   rK   rD   rE   rL   )rM   rN   r0   r   r   rw   r|   rP   r7   rQ   rR   r3   r.   rS   r>   rT   rU   Úupdater1   r(   rX   r	   rY   )r   r'   rZ   r[   rD   rE   r}   r\   rK   rL   rJ   rB   r]   r   r   r   r^   ¶  s6    





z$MongoDBSessionInterface.save_sessionN)FT)r   r   r   r   r_   r>   r"   r8   r   r+   r^   r   r   r   r   ru   z  s    
ru   c               @   s2   e Zd ZdZeZeZddd„Zdd„ Z	dd	„ Z
d
S )Ú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.
    FTc       	         sd   |d krddl m} ||ƒ}|ˆ _|ˆ _|ˆ _|ˆ _tˆ dƒˆ _G ‡ ‡fdd„dˆ jjƒ}|ˆ _	d S )Nr   )Ú
SQLAlchemyr.   c                   sl   e Zd Z”Z” jj” jjdd�Z” jj” jjdƒdd�Z	” jj” jj
ƒZ” jj” jjƒZdd„ Zdd„ Zd	S )
z4SqlAlchemySessionInterface.__init__.<locals>.SessionT)Zprimary_keyéÿ   )Úuniquec             S   s   || _ || _|| _d S )N)r]   rC   Úexpiry)r   r]   rC   r„   r   r   r   r   ù  s    z=SqlAlchemySessionInterface.__init__.<locals>.Session.__init__c             S   s
   d| j  S )Nz<Session data %s>)rC   )r   r   r   r   Ú__repr__þ  s    z=SqlAlchemySessionInterface.__init__.<locals>.Session.__repr__N)r   r   r   Z__tablename__rx   ZColumnZIntegerry   ÚStringr]   ZLargeBinaryrC   ÚDateTimer„   r   r…   r   )r   Útabler   r   ÚSessionñ  s   r‰   )
Zflask_sqlalchemyr�   rx   r0   r1   r   r2   r3   ZModelÚsql_session_model)	r   r'   rx   rˆ   r0   r1   r   r�   r‰   r   )r   rˆ   r   r   æ  s    z#SqlAlchemySessionInterface.__init__c       
      C   s&  |j j|jƒ}|s*| jƒ }| j|| jd�S | jr†| j|ƒ}|d krFd S y|j|ƒ}|j	ƒ }W n( t
k
r„   | jƒ }| j|| jd�S X | j| }| jjj|d�jƒ }|rÔ|jtjƒ krÔ| jjj|ƒ | jjjƒ  d }|�ry$|j}| jjt|ƒƒ}	| j|	|d�S    | j|| jd�S | j|| jd�S )N)r   r   )r]   )r   )r5   r6   r7   r%   r8   r   r1   r(   r9   r:   r   r0   rŠ   ÚqueryÚ	filter_byÚfirstr„   r   r{   rx   rZ   rO   ÚcommitrC   r>   r?   r	   )
r   r'   r*   r   r@   rA   r}   Úsaved_sessionrB   rC   r   r   r   r+     s:    


z'SqlAlchemySessionInterface.open_sessionc             C   sL  | j |ƒ}| j|ƒ}| j|j }| jjj|d�jƒ }|sr|jrn|r\| j	j
j|ƒ | j	j
jƒ  |j|j||d� d S i }| j|ƒ}	| j|ƒ}
| jrž| j|ƒ|d< | j||ƒ}| jjt|ƒƒ}|rØ||_||_| j	j
jƒ  n(| j|||ƒ}| j	j
j|ƒ | j	j
jƒ  | j�r | j|ƒjt|jƒƒ}n|j}|j|j|f||	|||
dœ|—Ž d S )N)r]   )rD   rE   rF   )rJ   rK   rD   rE   rL   )rM   rN   r0   r   rŠ   r‹   rŒ   r�   r   rx   rZ   rO   rŽ   rP   r7   rQ   rR   r3   r.   rS   r>   rT   rU   rC   r„   Úaddr1   r(   rX   r	   rY   )r   r'   rZ   r[   rD   rE   r}   r�   r\   rK   rL   rJ   rB   Znew_sessionr]   r   r   r   r^   %  sD    





z'SqlAlchemySessionInterface.save_sessionN)FT)r   r   r   r   r_   r>   r#   r8   r   r+   r^   r   r   r   r   r€   Ö  s    
!r€   )%r   ÚsysrI   r   Úuuidr   ÚcPickler_   re   Zflask.sessionsr   ZFlaskSessionInterfacer   Zwerkzeug.datastructuresr   Zitsdangerousr   r   r	   Úversion_infor;   r$   r=   rk   r   r   r   r    r!   r"   r#   r)   r,   r`   ro   ru   r€   r   r   r   r   Ú<module>
   s<   ZzL\