33 lines
986 B
Python
33 lines
986 B
Python
# -*- coding: utf-8 -*-
|
|
|
|
from werkzeug.contrib.securecookie import SecureCookie
|
|
|
|
class WSGISession:
|
|
def __init__(self, cookies, secret_key):
|
|
self.secret_key = secret_key
|
|
data = cookies.get('session')
|
|
if data:
|
|
self.sc = SecureCookie.unserialize(data, secret_key)
|
|
else:
|
|
self.reset()
|
|
if 'admin' not in self.sc:
|
|
self.reset()
|
|
|
|
def __getitem__(self, key):
|
|
return self.sc.__getitem__(key)
|
|
def __setitem__(self, key, value):
|
|
return self.sc.__setitem__(key, value)
|
|
def __delitem__(self, key):
|
|
return self.sc.__delitem__(key)
|
|
def __contains__(self, key):
|
|
return self.sc.__contains__(key)
|
|
|
|
def reset(self):
|
|
self.sc = SecureCookie(secret_key=self.secret_key)
|
|
self.sc['admin'] = False
|
|
|
|
def save(self, response):
|
|
if self.sc.should_save:
|
|
data = self.sc.serialize()
|
|
response.set_cookie('session', data, httponly=True)
|