<s> from __future__ import unicode_literals <EOL> import base64 <EOL> from datetime import datetime , timedelta <EOL> try : <EOL> from django . utils . six . moves import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> import string <EOL> from django . conf import settings <EOL> from django . core . exceptions import SuspiciousOperation <EOL> from django . utils . crypto import constant_time_compare <EOL> from django . utils . crypto import get_random_string <EOL> from django . utils . crypto import salted_hmac <EOL> from django . utils import timezone <EOL> from django . utils . encoding import force_bytes <EOL> VALID_KEY_CHARS = string . ascii_lowercase + string . digits <EOL> class CreateError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class SessionBase ( object ) : <EOL> """<STR_LIT>""" <EOL> TEST_COOKIE_NAME = '<STR_LIT>' <EOL> TEST_COOKIE_VALUE = '<STR_LIT>' <EOL> def __init__ ( self , session_key = None ) : <EOL> self . _session_key = session_key <EOL> self . accessed = False <EOL> self . modified = False <EOL> def __contains__ ( self , key ) : <EOL> return key in self . _session <EOL> def __getitem__ ( self , key ) : <EOL> return self . _session [ key ] <EOL> def __setitem__ ( self , key , value ) : <EOL> self . _session [ key ] = value <EOL> self . modified = True <EOL> def __delitem__ ( self , key ) : <EOL> del self . _session [ key ] <EOL> self . modified = True <EOL> def get ( self , key , default = None ) : <EOL> return self . _session . get ( key , default ) <EOL> def pop ( self , key , * args ) : <EOL> self . modified = self . modified or key in self . _session <EOL> return self . _session . pop ( key , * args ) <EOL> def setdefault ( self , key , value ) : <EOL> if key in self . _session : <EOL> return self . _session [ key ] <EOL> else : <EOL> self . modified = True <EOL> self . _session [ key ] = value <EOL> return value <EOL> def set_test_cookie ( self ) : <EOL> self [ self . TEST_COOKIE_NAME ] = self . TEST_COOKIE_VALUE <EOL> def test_cookie_worked ( self ) : <EOL> return self . get ( self . TEST_COOKIE_NAME ) == self . TEST_COOKIE_VALUE <EOL> def delete_test_cookie ( self ) : <EOL> del self [ self . TEST_COOKIE_NAME ] <EOL> def _hash ( self , value ) : <EOL> key_salt = "<STR_LIT>" + self . __class__ . __name__ <EOL> return salted_hmac ( key_salt , value ) . hexdigest ( ) <EOL> def encode ( self , session_dict ) : <EOL> "<STR_LIT>" <EOL> pickled = pickle . dumps ( session_dict , pickle . HIGHEST_PROTOCOL ) <EOL> hash = self . _hash ( pickled ) <EOL> return base64 . b64encode ( hash . encode ( ) + b"<STR_LIT::>" + pickled ) . decode ( '<STR_LIT:ascii>' ) <EOL> def decode ( self , session_data ) : <EOL> encoded_data = base64 . b64decode ( force_bytes ( session_data ) ) <EOL> try : <EOL> hash , pickled = encoded_data . split ( b'<STR_LIT::>' , <NUM_LIT:1> ) <EOL> expected_hash = self . _hash ( pickled ) <EOL> if not constant_time_compare ( hash . decode ( ) , expected_hash ) : <EOL> raise SuspiciousOperation ( "<STR_LIT>" ) <EOL> else : <EOL> return pickle . loads ( pickled ) <EOL> except Exception : <EOL> return { } <EOL> def update ( self , dict_ ) : <EOL> self . _session . update ( dict_ ) <EOL> self . modified = True <EOL> def has_key ( self , key ) : <EOL> return key in self . _session <EOL> def keys ( self ) : <EOL> return self . _session . keys ( ) <EOL> def values ( self ) : <EOL> return self . _session . values ( ) <EOL> def items ( self ) : <EOL> return self . _session . items ( ) <EOL> def iterkeys ( self ) : <EOL> return self . _session . iterkeys ( ) <EOL> def itervalues ( self ) : <EOL> return self . _session . itervalues ( ) <EOL> def iteritems ( self ) : <EOL> return self . _session . iteritems ( ) <EOL> def clear ( self ) : <EOL> self . _session_cache = { } <EOL> self . accessed = True <EOL> self . modified = True <EOL> def _get_new_session_key ( self ) : <EOL> "<STR_LIT>" <EOL> while True : <EOL> session_key = get_random_string ( <NUM_LIT:32> , VALID_KEY_CHARS ) <EOL> if not self . exists ( session_key ) : <EOL> break <EOL> return session_key <EOL> def _get_or_create_session_key ( self ) : <EOL> if self . _session_key is None : <EOL> self . _session_key = self . _get_new_session_key ( ) <EOL> return self . _session_key <EOL> def _get_session_key ( self ) : <EOL> return self . _session_key <EOL> session_key = property ( _get_session_key ) <EOL> def _get_session ( self , no_load = False ) : <EOL> """<STR_LIT>""" <EOL> self . accessed = True <EOL> try : <EOL> return self . _session_cache <EOL> except AttributeError : <EOL> if self . session_key is None or no_load : <EOL> self . _session_cache = { } <EOL> else : <EOL> self . _session_cache = self . load ( ) <EOL> return self . _session_cache <EOL> _session = property ( _get_session ) <EOL> def get_expiry_age ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> modification = kwargs [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> modification = timezone . now ( ) <EOL> try : <EOL> expiry = kwargs [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> expiry = self . get ( '<STR_LIT>' ) <EOL> if not expiry : <EOL> return settings . SESSION_COOKIE_AGE <EOL> if not isinstance ( expiry , datetime ) : <EOL> return expiry <EOL> delta = expiry - modification <EOL> return delta . days * <NUM_LIT> + delta . seconds <EOL> def get_expiry_date ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> modification = kwargs [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> modification = timezone . now ( ) <EOL> try : <EOL> expiry = kwargs [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> expiry = self . get ( '<STR_LIT>' ) <EOL> if isinstance ( expiry , datetime ) : <EOL> return expiry <EOL> if not expiry : <EOL> expiry = settings . SESSION_COOKIE_AGE <EOL> return modification + timedelta ( seconds = expiry ) <EOL> def set_expiry ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> try : <EOL> del self [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> return <EOL> if isinstance ( value , timedelta ) : <EOL> value = timezone . now ( ) + value <EOL> self [ '<STR_LIT>' ] = value <EOL> def get_expire_at_browser_close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . get ( '<STR_LIT>' ) is None : <EOL> return settings . SESSION_EXPIRE_AT_BROWSER_CLOSE <EOL> return self . get ( '<STR_LIT>' ) == <NUM_LIT:0> <EOL> def flush ( self ) : <EOL> """<STR_LIT>""" <EOL> self . clear ( ) <EOL> self . delete ( ) <EOL> self . create ( ) <EOL> def cycle_key ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . _session_cache <EOL> key = self . session_key <EOL> self . create ( ) <EOL> self . _session_cache = data <EOL> self . delete ( key ) <EOL> def exists ( self , session_key ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def create ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def save ( self , must_create = False ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def delete ( self , session_key = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def load ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ classmethod <EOL> def clear_expired ( cls ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError </s>
