def main(token): env = bootstrap(default_config_file) settings = env['request'].registry.settings secret = settings.get('session.secret') manager = SessionManager(cfg=settings, ttl=3600, secret=secret) session = manager.get_session(token=token) print('Session: {}'.format(session)) print('Data:\n{}'.format(pprint.pformat(dict(session)))) return True
def main(token): env = bootstrap(default_config_file) settings = env['request'].registry.settings secret = settings.get('session.secret') manager = SessionManager(cfg = settings, ttl = 3600, secret = secret) session = manager.get_session(token = token) print('Session: {}'.format(session)) print('Data:\n{}'.format(pprint.pformat(dict(session)))) return True
class SessionFactory(SessionInterface): """ Session factory, implementing flask.session.SessionInterface, to provide eduID redis-based sessions to the APIs. :param config: the configuration for the session :type config: dict """ def __init__(self, config): self.config = config secret = config['SECRET_KEY'] ttl = 2 * int(config['PERMANENT_SESSION_LIFETIME']) self.manager = SessionManager(config, ttl=ttl, secret=secret) def open_session(self, app, request): """ See flask.session.SessionInterface """ try: cookie_name = app.config['SESSION_COOKIE_NAME'] except KeyError: return None token = request.cookies.get(cookie_name, None) if token is None: # New session base_session = self.manager.get_session(data={}) session = Session(app, base_session, new=True) else: # Existing session try: base_session = self.manager.get_session(token=token) session = Session(app, base_session, new=False) except KeyError: raise NoSessionDataFoundException('No session data found') return session def save_session(self, app, session, response): """ See flask.session.SessionInterface """ session.persist() session.set_cookie(response)
def __init__(self, settings): ''' SessionFactory constructor. :param settings: the pyramid settings :type settings: dict ''' cookie_max_age = int(settings.get('session.cookie_max_age')) # make sure that the data in redis outlives the session cookie session_ttl = 2 * cookie_max_age secret = settings.get('session.secret') self.manager = SessionManager(settings, ttl=session_ttl, secret=secret)
def __init__(self, name, logger, ttl, config): super(ExpiringCacheCommonSession, self).__init__(name, logger, ttl, lock=None) redis_cfg = { 'REDIS_PORT': config.redis_port, 'REDIS_DB': config.redis_db, } if config.redis_sentinel_hosts: redis_cfg.update({ 'REDIS_SENTINEL_HOSTS': config.redis_sentinel_hosts, 'REDIS_SENTINEL_SERVICE_NAME': config.redis_sentinel_service_name, }) else: redis_cfg['REDIS_HOST'] = config.redis_host self._redis_cfg = redis_cfg self._manager = SessionManager(redis_cfg, ttl=ttl, secret=config.session_app_key)
def __init__(self, config): self.config = config secret = config['SECRET_KEY'] ttl = 2 * int(config['PERMANENT_SESSION_LIFETIME']) self.manager = SessionManager(config, ttl=ttl, secret=secret)
class ExpiringCacheCommonSession(ExpiringCache): def __init__(self, name, logger, ttl, config): super(ExpiringCacheCommonSession, self).__init__(name, logger, ttl, lock=None) redis_cfg = { 'REDIS_PORT': config.redis_port, 'REDIS_DB': config.redis_db, } if config.redis_sentinel_hosts: redis_cfg.update({ 'REDIS_SENTINEL_HOSTS': config.redis_sentinel_hosts, 'REDIS_SENTINEL_SERVICE_NAME': config.redis_sentinel_service_name, }) else: redis_cfg['REDIS_HOST'] = config.redis_host self._redis_cfg = redis_cfg self._manager = SessionManager(redis_cfg, ttl=ttl, secret=config.session_app_key) def __repr__(self): return '<{!s}: {!s}>'.format(self.__class__.__name__, unicode(self)) def __unicode__(self): if 'REDIS_SENTINEL_HOSTS' in self._redis_cfg: return u'redis sentinel={!r}'.format(','.join( self._redis_cfg['REDIS_SENTINEL_HOSTS'])) else: return u'redis host={!r}'.format(self._redis_cfg['REDIS_HOST']) def add(self, key, info): """ Add entry to the cache. :param key: Lookup key for entry :param info: Value to be stored for 'key' :type key: str | unciode :type info: dict :return: New session :rtype: Session """ if isinstance(info, SSOLoginData): data = info.to_dict() data[ 'req_info'] = None # can't serialize this - will be re-created from SAMLRequest else: data = info _session_id = bytes(key.decode('hex')) session = self._manager.get_session(session_id=_session_id, data=data) session.commit() return session def get(self, key): """ Fetch data from cache based on `key'. :param key: hash key to use for lookup :type key: str | unicode :returns: The previously added session :rtype: Session | None """ _session_id = bytes(key.decode('hex')) try: session = self._manager.get_session(session_id=_session_id) return dict(session) except KeyError: pass def delete(self, key): """ Delete an item from the cache. :param key: hash key to delete :type key: str | unicode :return: True on success """ _session_id = bytes(key.decode('hex')) session = self._manager.get_session(session_id=_session_id) if not session: return False session.clear() return True