<s> """<STR_LIT>""" <EOL> import re <EOL> import webapp2 <EOL> from webapp2_extras import securecookie <EOL> from webapp2_extras import security <EOL> default_config = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT:path>' : '<STR_LIT:/>' , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : False , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } <EOL> _default_value = object ( ) <EOL> class _UpdateDictMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> on_update = None <EOL> def calls_update ( name ) : <EOL> def oncall ( self , * args , ** kw ) : <EOL> rv = getattr ( super ( _UpdateDictMixin , self ) , name ) ( * args , ** kw ) <EOL> if self . on_update is not None : <EOL> self . on_update ( ) <EOL> return rv <EOL> oncall . __name__ = name <EOL> return oncall <EOL> __setitem__ = calls_update ( '<STR_LIT>' ) <EOL> __delitem__ = calls_update ( '<STR_LIT>' ) <EOL> clear = calls_update ( '<STR_LIT>' ) <EOL> pop = calls_update ( '<STR_LIT>' ) <EOL> popitem = calls_update ( '<STR_LIT>' ) <EOL> setdefault = calls_update ( '<STR_LIT>' ) <EOL> update = calls_update ( '<STR_LIT>' ) <EOL> del calls_update <EOL> class SessionDict ( _UpdateDictMixin , dict ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , container , data = None , new = False ) : <EOL> self . container = container <EOL> self . new = new <EOL> self . modified = False <EOL> dict . update ( self , data or ( ) ) <EOL> def pop ( self , key , * args ) : <EOL> if key in self : <EOL> return super ( SessionDict , self ) . pop ( key , * args ) <EOL> if args : <EOL> return args [ <NUM_LIT:0> ] <EOL> raise KeyError ( key ) <EOL> def on_update ( self ) : <EOL> self . modified = True <EOL> def get_flashes ( self , key = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return self . pop ( key , [ ] ) <EOL> def add_flash ( self , value , level = None , key = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . setdefault ( key , [ ] ) . append ( ( value , level ) ) <EOL> class BaseSessionFactory ( object ) : <EOL> """<STR_LIT>""" <EOL> name = None <EOL> session_store = None <EOL> session_args = None <EOL> session = None <EOL> def __init__ ( self , name , session_store ) : <EOL> self . name = name <EOL> self . session_store = session_store <EOL> self . session_args = session_store . config [ '<STR_LIT>' ] . copy ( ) <EOL> self . session = None <EOL> def get_session ( self , max_age = _default_value ) : <EOL> raise NotImplementedError ( ) <EOL> def save_session ( self , response ) : <EOL> raise NotImplementedError ( ) <EOL> class SecureCookieSessionFactory ( BaseSessionFactory ) : <EOL> """<STR_LIT>""" <EOL> def get_session ( self , max_age = _default_value ) : <EOL> if self . session is None : <EOL> data = self . session_store . get_secure_cookie ( self . name , <EOL> max_age = max_age ) <EOL> new = data is None <EOL> self . session = SessionDict ( self , data = data , new = new ) <EOL> return self . session <EOL> def save_session ( self , response ) : <EOL> if self . session is None or not self . session . modified : <EOL> return <EOL> self . session_store . save_secure_cookie ( <EOL> response , self . name , dict ( self . session ) , ** self . session_args ) <EOL> class CustomBackendSessionFactory ( BaseSessionFactory ) : <EOL> """<STR_LIT>""" <EOL> sid = None <EOL> _sid_re = re . compile ( r'<STR_LIT>' ) <EOL> def get_session ( self , max_age = _default_value ) : <EOL> if self . session is None : <EOL> data = self . session_store . get_secure_cookie ( self . name , <EOL> max_age = max_age ) <EOL> sid = data . get ( '<STR_LIT>' ) if data else None <EOL> self . session = self . _get_by_sid ( sid ) <EOL> return self . session <EOL> def _get_by_sid ( self , sid ) : <EOL> raise NotImplementedError ( ) <EOL> def _is_valid_sid ( self , sid ) : <EOL> """<STR_LIT>""" <EOL> return sid and self . _sid_re . match ( sid ) is not None <EOL> def _get_new_sid ( self ) : <EOL> return security . generate_random_string ( entropy = <NUM_LIT> ) <EOL> class SessionStore ( object ) : <EOL> """<STR_LIT>""" <EOL> config_key = __name__ <EOL> def __init__ ( self , request , config = None ) : <EOL> """<STR_LIT>""" <EOL> self . request = request <EOL> self . config = request . app . config . load_config ( self . config_key , <EOL> default_values = default_config , user_values = config , <EOL> required_keys = ( '<STR_LIT>' , ) ) <EOL> self . sessions = { } <EOL> @ webapp2 . cached_property <EOL> def serializer ( self ) : <EOL> return securecookie . SecureCookieSerializer ( self . config [ '<STR_LIT>' ] ) <EOL> def get_backend ( self , name ) : <EOL> """<STR_LIT>""" <EOL> backends = self . config [ '<STR_LIT>' ] <EOL> backend = backends [ name ] <EOL> if isinstance ( backend , basestring ) : <EOL> backend = backends [ name ] = webapp2 . import_string ( backend ) <EOL> return backend <EOL> def _get_session_container ( self , name , factory ) : <EOL> if name not in self . sessions : <EOL> self . sessions [ name ] = factory ( name , self ) <EOL> return self . sessions [ name ] <EOL> def get_session ( self , name = None , max_age = _default_value , factory = None , <EOL> backend = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> factory = factory or self . get_backend ( backend ) <EOL> name = name or self . config [ '<STR_LIT>' ] <EOL> if max_age is _default_value : <EOL> max_age = self . config [ '<STR_LIT>' ] <EOL> container = self . _get_session_container ( name , factory ) <EOL> return container . get_session ( max_age = max_age ) <EOL> def get_secure_cookie ( self , name , max_age = _default_value ) : <EOL> """<STR_LIT>""" <EOL> if max_age is _default_value : <EOL> max_age = self . config [ '<STR_LIT>' ] <EOL> value = self . request . cookies . get ( name ) <EOL> if value : <EOL> return self . serializer . deserialize ( name , value , max_age = max_age ) <EOL> def set_secure_cookie ( self , name , value , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( value , dict ) , '<STR_LIT>' <EOL> container = self . _get_session_container ( name , <EOL> SecureCookieSessionFactory ) <EOL> container . get_session ( ) . update ( value ) <EOL> container . session_args . update ( kwargs ) <EOL> def save_sessions ( self , response ) : <EOL> """<STR_LIT>""" <EOL> for session in self . sessions . values ( ) : <EOL> session . save_session ( response ) <EOL> def save_secure_cookie ( self , response , name , value , ** kwargs ) : <EOL> value = self . serializer . serialize ( name , value ) <EOL> response . set_cookie ( name , value , ** kwargs ) <EOL> _registry_key = '<STR_LIT>' <EOL> def get_store ( factory = SessionStore , key = _registry_key , request = None ) : <EOL> """<STR_LIT>""" <EOL> request = request or webapp2 . get_request ( ) <EOL> store = request . registry . get ( key ) <EOL> if not store : <EOL> store = request . registry [ key ] = factory ( request ) <EOL> return store <EOL> def set_store ( store , key = _registry_key , request = None ) : <EOL> """<STR_LIT>""" <EOL> request = request or webapp2 . get_request ( ) <EOL> request . registry [ key ] = store <EOL> default_config [ '<STR_LIT>' ] [ '<STR_LIT>' ] = SecureCookieSessionFactory </s>
