def set_cookie(self, response, name, data, path=None, expires=None, secret_key=None, max_age=None, secure=False, httponly=True, force=True): """store data under the named cookie as a securecookie in the response""" if secret_key is None: secret_key = self.config.get('secret_key', None) cookie = SecureCookie(data, secret_key=secret_key) path = path or self.config.get('session_cookie_path') or \ self.config.get('application_root') or '/' cookie.save_cookie(response, key=name, expires=expires, max_age=max_age, path=path, domain=self.config.session_cookie_domain, secure=secure, httponly=httponly, force=force)
def set_cookie(self, response, name, data, path = None, expires = None, secret_key = None, max_age = None, secure = False, httponly = True, force = True): """store data under the named cookie as a securecookie in the response""" if secret_key is None: secret_key = self.config.get('secret_key', None) cookie = SecureCookie(data, secret_key=secret_key) path = path or self.config.get('session_cookie_path') or \ self.config.get('application_root') or '/' cookie.save_cookie(response, key=name, expires=expires, max_age=max_age, path=path, domain=self.config.session_cookie_domain, secure=secure, httponly=httponly, force=force)
def save_session(self, session, response): # 这个函数将在构造好了response后调用,此函数逻辑是如何session有内容,就将其 # 保存在数据库,然后构建一个新的Securecookie,在传入sid的值,然后保存在response # 中 if session is not None: if session.should_save: self.session_store.save(session) secure_cookie = SecureCookie({}, secret_key=self.secret_key) secure_cookie['sid'] = session.sid # 这里必须要有一次额外的赋值,否则secure_cookie的should_save为False secure_cookie.save_cookie(response, self.session_cookie_name)
def save_session_to_cookie(response): secret = current_app.config.get("session_secret") if secret: session = local.session if session: if not isinstance(session, SecureCookie): session = SecureCookie(session, secret) expires = None lifetime = current_app.config.get("session_lifetime") if lifetime: expires = datetime.datetime.utcnow() + datetime.timedelta(seconds=lifetime) session_name = current_app.config.get("session_cookie_name") or "session" session.save_cookie(response, session_name, expires=expires)
def login(): username = request.values.get('username', '') password = request.values.get('password', '') ret_data = {} is_login = db.check_user(username, password) if is_login: ret_data['status'] = 'ok' else: ret_data['status'] = 'error' ret_data['message'] = 'Username or password is wrong!' resp = Response(json.dumps(ret_data)) if is_login: # New and save `uid` to cookie cookie = SecureCookie(secret_key=app.secret_key) cookie['uid'] = username cookie['is_guest'] = False cookie.save_cookie(resp, key='auth', max_age=USER_COOKIE_AGE) return resp
def init(): ''' For guest user ''' domain = request.args.get('domain', CONFIG['domain']) title = request.args.get('title', CONFIG['title']) theme = request.args.get('theme', CONFIG['theme']) local = request.args.get('local', CONFIG['local']) is_login = '' user = '******' if g.is_login: is_login = '******' record = db.load_visitor(g.uid) record['nick'] = '%s(%s)' % (record['nick'], record['location']) user = { 'id': g.uid, 'nick': record['nick'], 'show': 'available', 'status': '' } user = json.dumps(user) path = 'http://%s/' % CONFIG['domain'] js = u'''var _IMC = { production_name: 'service', version: '%(version)s', domain: '%(domain)s', path: '%(path)s', is_login: '******', user: %(user)s, setting:{ play_sound: true, minimize_layout: true, buddy_sticky: true }, disable_chatlink: '', title: '%(title)s', theme: '%(theme)s', local: '%(local)s', jsonp: '1', min: window.location.href.indexOf("webim_debug") != -1 ? "" : ".min" }; _IMC.script = window.webim ? '' : ('<link href="' + _IMC.path + 'static/webim.' + _IMC.production_name + _IMC.min + '.css?' + _IMC.version + '" media="all" type="text/css" rel="stylesheet"/><link href="' + _IMC.path + 'static/themes/' + _IMC.theme + '/jquery.ui.theme.css?' + _IMC.version + '" media="all" type="text/css" rel="stylesheet"/><script src="' + _IMC.path + 'static/webim.' + _IMC.production_name + _IMC.min + '.js?' + _IMC.version + '" type="text/javascript"></script><script src="' + _IMC.path + 'static/i18n/webim-' + _IMC.local + '.js?' + _IMC.version + '" type="text/javascript"></script>'); _IMC.script += '<script src="' + _IMC.path + 'static/webim.js?' + _IMC.version + '" type="text/javascript"></script>'; document.write( _IMC.script );''' % { 'version': CONFIG['version'], 'domain': domain, 'path': path, 'is_login': is_login, 'user': user, 'title': title, 'theme': theme, 'local': local, } resp = Response(js, content_type='text/javascript') # Save uid to cookie if not g.is_login: print 'Save uid to cookie' cookie = SecureCookie(secret_key=app.secret_key) cookie['uid'] = g.uid cookie['is_guest'] = True cookie.save_cookie(resp, key='auth', max_age=VISITOR_COOKIE_AGE) return resp
def init(): ''' For guest user ''' domain = request.args.get('domain', CONFIG['domain']) title = request.args.get('title', CONFIG['title']) theme = request.args.get('theme', CONFIG['theme']) local = request.args.get('local', CONFIG['local']) is_login = '' user = '******' if g.is_login: is_login = '******' record = db.load_visitor(g.uid) record['nick'] = '%s(%s)' % (record['nick'], record['location']) user = { 'id' : g.uid, 'nick': record['nick'], 'show': 'available', 'status' : '' } user = json.dumps(user) path = 'http://%s/' % CONFIG['domain'] js = u'''var _IMC = { production_name: 'service', version: '%(version)s', domain: '%(domain)s', path: '%(path)s', is_login: '******', user: %(user)s, setting:{ play_sound: true, minimize_layout: true, buddy_sticky: true }, disable_chatlink: '', title: '%(title)s', theme: '%(theme)s', local: '%(local)s', jsonp: '1', min: window.location.href.indexOf("webim_debug") != -1 ? "" : ".min" }; _IMC.script = window.webim ? '' : ('<link href="' + _IMC.path + 'static/webim.' + _IMC.production_name + _IMC.min + '.css?' + _IMC.version + '" media="all" type="text/css" rel="stylesheet"/><link href="' + _IMC.path + 'static/themes/' + _IMC.theme + '/jquery.ui.theme.css?' + _IMC.version + '" media="all" type="text/css" rel="stylesheet"/><script src="' + _IMC.path + 'static/webim.' + _IMC.production_name + _IMC.min + '.js?' + _IMC.version + '" type="text/javascript"></script><script src="' + _IMC.path + 'static/i18n/webim-' + _IMC.local + '.js?' + _IMC.version + '" type="text/javascript"></script>'); _IMC.script += '<script src="' + _IMC.path + 'static/webim.js?' + _IMC.version + '" type="text/javascript"></script>'; document.write( _IMC.script );''' % { 'version' : CONFIG['version'], 'domain' : domain, 'path' : path, 'is_login' : is_login, 'user' : user, 'title' : title, 'theme' : theme, 'local' : local, } resp = Response(js, content_type='text/javascript') # Save uid to cookie if not g.is_login: print 'Save uid to cookie' cookie = SecureCookie(secret_key=app.secret_key) cookie['uid'] = g.uid cookie['is_guest'] = True cookie.save_cookie(resp, key='auth', max_age=VISITOR_COOKIE_AGE) return resp