def save_session(self, app, session, response): domain = self.get_cookie_domain(app) path = self.get_cookie_path(app) if not session: if session.modified: self.redis.delete(self.session_prefix + session.session_id) response.delete_cookie(app.session_cookie_name, domain=domain, path=path) return httponly = self.get_cookie_httponly(app) secure = self.get_cookie_secure(app) expire = self.get_expiration_time(app, session) serialize_session = self.serialization_method.dumps(dict(session)) pipe = self.redis.pipeline() pipe.set(self.session_prefix + session.session_id, serialize_session) pipe.expire(self.session_prefix + session.session_id, total_seconds(self.expire_time)) pipe.execute() if self.use_sign: session_id = Signer(app.secret_key, salt='flask-redis-session', key_derivation='hmac').sign(session.session_id.encode('utf-8')) session_id = session_id.decode('utf-8') else: session_id = session.session_id print('session_id:', session_id) response.set_cookie(key=app.session_cookie_name, value=session_id, max_age=self.expire_time, expires=expire, path=path, domain=domain, secure=secure, httponly=httponly)
def to_python(self, value): try: data = Signer(application.app.secret_key, salt=b'device-secure').unsign(value) except BadData: raise NotFound device_id, certificate_hash = data.decode("US-ASCII").split('.', 2) retval = DeviceBase.find(id=str(device_id)) if retval is None: raise NotFound if not compare_digest(certificate_hash, retval.certificate.get_hexhash()): raise NotFound return retval
def save_session(self, app, session, response): domain = self.get_cookie_domain(app) path = self.get_cookie_path(app) if not session: if session.modified: self.redis.delete(self.session_prefix + session.session_id) response.delete_cookie(app.session_cookie_name, domain=domain, path=path) return httponly = self.get_cookie_httponly(app) secure = self.get_cookie_secure(app) expire = self.get_expiration_time(app, session) serialize_session = self.serialization_method.dumps(dict(session)) pipe = self.redis.pipeline() pipe.set(self.session_prefix + session.session_id, serialize_session) pipe.expire(self.session_prefix + session.session_id, total_seconds(self.expire_time)) pipe.execute() if self.use_sign: session_id = Signer(app.secret_key, salt='flask-redis-session', key_derivation='hmac').sign( session.session_id.encode('utf-8')) session_id = session_id.decode('utf-8') else: session_id = session.session_id print('session_id:', session_id) response.set_cookie(key=app.session_cookie_name, value=session_id, max_age=self.expire_time, expires=expire, path=path, domain=domain, secure=secure, httponly=httponly)