î
ªÍ X0  ã               @   s"  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l m Z m Z d  d l m 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 d  d	 l m Z m Z d  d
 l m Z e j e j Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z d S)é    )Úunicode_literalsN)ÚdatetimeÚ	timedelta)Úsettings)ÚSuspiciousSession)ÚSuspiciousOperation)Útimezone)Úconstant_time_compareÚget_random_stringÚsalted_hmac)Úforce_bytesÚ
force_text)Úimport_stringc               @   s   e  Z d  Z d Z d S)ÚCreateErrorz‡
    Used internally as a consistent exception type to catch from save (see the
    docstring for SessionBase.save() for details).
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úS/home/ubuntu/projects/ifolica/build/django/django/contrib/sessions/backends/base.pyr      s   r   c               @   s   e  Z d  Z d Z d S)ÚUpdateErrorzF
    Occurs if Django tries to update a session that was deleted.
    N)r   r   r   r   r   r   r   r   r      s   r   c               @   sb  e  Z d  Z d Z d Z d Z e ƒ  Z d d d „ Z d d „  Z	 d	 d
 „  Z
 d d „  Z d d „  Z d d d „ Z e d d „ Z d d „  Z d d „  Z d d „  Z d d „  Z d d „  Z d d „  Z d d  „  Z d! d" „  Z d# d$ „  Z d% d& „  Z d' d( „  Z d) d* „  Z d+ d, „  Z d- d. „  Z d/ d0 „  Z d1 d2 „  Z d3 d4 „  Z d5 d6 „  Z  d7 d8 „  Z! d9 d: „  Z" d; d< „  Z# d= d> „  Z$ e% e# ƒ Z& e% e# e$ ƒ Z' d? d@ dA „ Z( e% e( ƒ Z) dB dC „  Z* dD dE „  Z+ dF dG „  Z, dH dI „  Z- dJ dK „  Z. dL dM „  Z/ dN dO „  Z0 dP dQ „  Z1 d? dR dS „ Z2 d dT dU „ Z3 dV dW „  Z4 e5 dX dY „  ƒ Z6 d S)ZÚSessionBasez-
    Base class for all Session classes.
    Z
testcookieZworkedNc             C   s1   | |  _  d |  _ d |  _ t t j ƒ |  _ d  S)NF)Ú_session_keyÚaccessedÚmodifiedr   r   ZSESSION_SERIALIZERÚ
serializer)ÚselfÚsession_keyr   r   r   Ú__init__/   s    			zSessionBase.__init__c             C   s   | |  j  k S)N)Ú_session)r   Úkeyr   r   r   Ú__contains__5   s    zSessionBase.__contains__c             C   s   |  j  | S)N)r   )r   r    r   r   r   Ú__getitem__8   s    zSessionBase.__getitem__c             C   s   | |  j  | <d |  _ d  S)NT)r   r   )r   r    Úvaluer   r   r   Ú__setitem__;   s    zSessionBase.__setitem__c             C   s   |  j  | =d |  _ d  S)NT)r   r   )r   r    r   r   r   Ú__delitem__?   s    
zSessionBase.__delitem__c             C   s   |  j  j | | ƒ S)N)r   Úget)r   r    Údefaultr   r   r   r&   C   s    zSessionBase.getc             C   sL   |  j  p | |  j k |  _  | |  j k r0 f  n | f } |  j j | | Œ S)N)r   r   Ú_SessionBase__not_givenÚpop)r   r    r'   Úargsr   r   r   r)   F   s    zSessionBase.popc             C   s8   | |  j  k r |  j  | Sd |  _ | |  j  | <| Sd  S)NT)r   r   )r   r    r#   r   r   r   Ú
setdefaultK   s
    	zSessionBase.setdefaultc             C   s   |  j  |  |  j <d  S)N)ÚTEST_COOKIE_VALUEÚTEST_COOKIE_NAME)r   r   r   r   Úset_test_cookieS   s    zSessionBase.set_test_cookiec             C   s   |  j  |  j ƒ |  j k S)N)r&   r-   r,   )r   r   r   r   Útest_cookie_workedV   s    zSessionBase.test_cookie_workedc             C   s   |  |  j  =d  S)N)r-   )r   r   r   r   Údelete_test_cookieY   s    zSessionBase.delete_test_cookiec             C   s#   d |  j  j } t | | ƒ j ƒ  S)Nzdjango.contrib.sessions)Ú	__class__r   r   Ú	hexdigest)r   r#   Zkey_saltr   r   r   Ú_hash\   s    zSessionBase._hashc             C   sH   |  j  ƒ  j | ƒ } |  j | ƒ } t j | j ƒ  d | ƒ j d ƒ S)zHReturns the given session dictionary serialized and encoded as a string.ó   :Úascii)r   Údumpsr3   Úbase64Ú	b64encodeÚencodeÚdecode)r   Zsession_dictÚ
serializedÚhashr   r   r   r9   `   s    zSessionBase.encodec             C   så   t  j t | ƒ ƒ } yb | j d d ƒ \ } } |  j | ƒ } t | j ƒ  | ƒ sc t d ƒ ‚ n |  j ƒ  j	 | ƒ SWng t
 k
 rà } zG t | t ƒ rÊ t j d | j j ƒ } | j t | ƒ ƒ n  i  SWYd  d  } ~ Xn Xd  S)Nr4   é   zSession data corruptedzdjango.security.%s)r7   Ú	b64decoder   Úsplitr3   r	   r:   r   r   ÚloadsÚ	ExceptionÚ
isinstancer   ÚloggingÚ	getLoggerr1   r   Úwarningr   )r   Zsession_dataZencoded_datar<   r;   Zexpected_hashÚeÚloggerr   r   r   r:   f   s    zSessionBase.decodec             C   s   |  j  j | ƒ d |  _ d  S)NT)r   Úupdater   )r   Zdict_r   r   r   rH   x   s    zSessionBase.updatec             C   s   | |  j  k S)N)r   )r   r    r   r   r   Úhas_key|   s    zSessionBase.has_keyc             C   s   |  j  j ƒ  S)N)r   Úkeys)r   r   r   r   rJ      s    zSessionBase.keysc             C   s   |  j  j ƒ  S)N)r   Úvalues)r   r   r   r   rK   ‚   s    zSessionBase.valuesc             C   s   |  j  j ƒ  S)N)r   Úitems)r   r   r   r   rL   …   s    zSessionBase.itemsc             C   s   |  j  j ƒ  S)N)r   Úiterkeys)r   r   r   r   rM   ˆ   s    zSessionBase.iterkeysc             C   s   |  j  j ƒ  S)N)r   Ú
itervalues)r   r   r   r   rN   ‹   s    zSessionBase.itervaluesc             C   s   |  j  j ƒ  S)N)r   Ú	iteritems)r   r   r   r   rO   Ž   s    zSessionBase.iteritemsc             C   s   i  |  _  d |  _ d |  _ d  S)NT)Ú_session_cacher   r   )r   r   r   r   Úclear‘   s    		zSessionBase.clearc             C   s9   y t  |  j ƒ o |  j SWn t k
 r4 d SYn Xd S)zBReturns True when there is no session_key and the session is emptyTN)Úboolr   rP   ÚAttributeError)r   r   r   r   Úis_empty™   s    zSessionBase.is_emptyc             C   s-   x& t  d t ƒ } |  j | ƒ s Pq q W| S)z*Returns session key that isn't being used.é    )r
   ÚVALID_KEY_CHARSÚexists)r   r   r   r   r   Ú_get_new_session_key    s
    z SessionBase._get_new_session_keyc             C   s(   |  j  d  k r! |  j ƒ  |  _  n  |  j  S)N)r   rX   )r   r   r   r   Ú_get_or_create_session_key¨   s    z&SessionBase._get_or_create_session_keyc             C   s   | o t  | ƒ d k S)z”
        Key must be truthy and at least 8 characters long. 8 characters is an
        arbitrary lower bound for some minimal key security.
        é   )Úlen)r   r    r   r   r   Ú_validate_session_key­   s    z!SessionBase._validate_session_keyc             C   s   |  j  S)N)Ú_SessionBase__session_key)r   r   r   r   Ú_get_session_key´   s    zSessionBase._get_session_keyc             C   s(   |  j  | ƒ r | |  _ n	 d |  _ d S)zV
        Validate session key on assignment. Invalid values will set to None.
        N)r\   r]   )r   r#   r   r   r   Ú_set_session_key·   s    zSessionBase._set_session_keyFc             C   s`   d |  _  y |  j SWnB t k
 rX |  j d k s9 | rE i  |  _ n |  j ƒ  |  _ Yn X|  j S)z 
        Lazily loads session from storage (unless "no_load" is True, when only
        an empty dict is stored) and stores it in the current instance.
        TN)r   rP   rS   r   Úload)r   Zno_loadr   r   r   Ú_get_sessionÃ   s    	zSessionBase._get_sessionc             K   s   y | d } Wn t  k
 r. t j ƒ  } Yn Xy | d } Wn! t  k
 r` |  j d ƒ } Yn X| sn t j St | t ƒ s | S| | } | j d | j	 S)zÕGet the number of seconds until the session expires.

        Optionally, this function accepts `modification` and `expiry` keyword
        arguments specifying the modification and expiry of the session.
        ÚmodificationÚexpiryÚ_session_expiryi€Q )
ÚKeyErrorr   Únowr&   r   ÚSESSION_COOKIE_AGErB   r   ÚdaysÚseconds)r   Úkwargsrb   rc   Údeltar   r   r   Úget_expiry_ageÔ   s    
zSessionBase.get_expiry_agec             K   s—   y | d } Wn t  k
 r. t j ƒ  } Yn Xy | d } Wn! t  k
 r` |  j d ƒ } Yn Xt | t ƒ rt | S| s† t j } n  | t d | ƒ S)zÔGet session the expiry date (as a datetime object).

        Optionally, this function accepts `modification` and `expiry` keyword
        arguments specifying the modification and expiry of the session.
        rb   rc   rd   ri   )	re   r   rf   r&   rB   r   r   rg   r   )r   rj   rb   rc   r   r   r   Úget_expiry_dateí   s    zSessionBase.get_expiry_datec             C   s`   | d k r0 y |  d =Wn t  k
 r+ Yn Xd St | t ƒ rR t j ƒ  | } n  | |  d <d S)a+  
        Sets a custom expiration for the session. ``value`` can be an integer,
        a Python ``datetime`` or ``timedelta`` object or ``None``.

        If ``value`` is an integer, the session will expire after that many
        seconds of inactivity. If set to ``0`` then the session will expire on
        browser close.

        If ``value`` is a ``datetime`` or ``timedelta`` object, the session
        will expire at that specific future time.

        If ``value`` is ``None``, the session uses the global session expiry
        policy.
        Nrd   )re   rB   r   r   rf   )r   r#   r   r   r   Ú
set_expiry  s    zSessionBase.set_expiryc             C   s/   |  j  d ƒ d k r t j S|  j  d ƒ d k S)a  
        Returns ``True`` if the session is set to expire when the browser
        closes, and ``False`` if there's an expiry date. Use
        ``get_expiry_date()`` or ``get_expiry_age()`` to find the actual expiry
        date/age, if there is one.
        rd   Nr   )r&   r   ZSESSION_EXPIRE_AT_BROWSER_CLOSE)r   r   r   r   Úget_expire_at_browser_close  s    z'SessionBase.get_expire_at_browser_closec             C   s!   |  j  ƒ  |  j ƒ  d |  _ d S)ze
        Removes the current session data from the database and regenerates the
        key.
        N)rQ   Údeleter   )r   r   r   r   Úflush(  s    

zSessionBase.flushc             C   s?   |  j  } |  j } |  j ƒ  | |  _  | r; |  j | ƒ n  d S)zV
        Creates a new session key, while retaining the current session data.
        N)rP   r   Úcreaterp   )r   Údatar    r   r   r   Ú	cycle_key1  s    		
	zSessionBase.cycle_keyc             C   s   t  d ƒ ‚ d S)zG
        Returns True if the given session_key already exists.
        z9subclasses of SessionBase must provide an exists() methodN)ÚNotImplementedError)r   r   r   r   r   rW   >  s    zSessionBase.existsc             C   s   t  d ƒ ‚ d S)zÆ
        Creates a new session instance. Guaranteed to create a new object with
        a unique key and will have saved the result once (with empty data)
        before the method returns.
        z8subclasses of SessionBase must provide a create() methodN)ru   )r   r   r   r   rr   D  s    zSessionBase.createc             C   s   t  d ƒ ‚ d S)a  
        Saves the session data. If 'must_create' is True, a new session object
        is created (otherwise a CreateError exception is raised). Otherwise,
        save() only updates an existing object and does not create one
        (an UpdateError is raised).
        z6subclasses of SessionBase must provide a save() methodN)ru   )r   Zmust_creater   r   r   ÚsaveL  s    zSessionBase.savec             C   s   t  d ƒ ‚ d S)z}
        Deletes the session data under this key. If the key is None, the
        current session key value is used.
        z8subclasses of SessionBase must provide a delete() methodN)ru   )r   r   r   r   r   rp   U  s    zSessionBase.deletec             C   s   t  d ƒ ‚ d S)zB
        Loads the session data and returns a dictionary.
        z6subclasses of SessionBase must provide a load() methodN)ru   )r   r   r   r   r`   \  s    zSessionBase.loadc             C   s   t  d ƒ ‚ d S)a  
        Remove expired sessions from the session store.

        If this operation isn't possible on a given backend, it should raise
        NotImplementedError. If it isn't necessary, because the backend has
        a built-in expiration mechanism, it should be a no-op.
        z.This backend does not support clear_expired().N)ru   )Úclsr   r   r   Úclear_expiredb  s    	zSessionBase.clear_expired)7r   r   r   r   r-   r,   Úobjectr(   r   r!   r"   r$   r%   r&   r)   r+   r.   r/   r0   r3   r9   r:   rH   rI   rJ   rK   rL   rM   rN   rO   rQ   rT   rX   rY   r\   r^   r_   Úpropertyr   r   ra   r   rl   rm   rn   ro   rq   rt   rW   rr   rv   rp   r`   Úclassmethodrx   r   r   r   r   r   &   sb   				r   ) Ú
__future__r   r7   rC   Ústringr   r   Zdjango.confr   Z"django.contrib.sessions.exceptionsr   Zdjango.core.exceptionsr   Zdjango.utilsr   Zdjango.utils.cryptor	   r
   r   Zdjango.utils.encodingr   r   Zdjango.utils.module_loadingr   Úascii_lowercaseÚdigitsrV   rA   r   r   ry   r   r   r   r   r   Ú<module>   s   