MCPcopy
hub / github.com/hugapi/hug / SessionMiddleware

Class SessionMiddleware

hug/middleware.py:28–108  ·  view source on GitHub ↗

Simple session middleware. Injects a session dictionary into the context of a request, sets a session cookie, and stores/restores data via a coupled store object. A session store object must implement the following methods: * get(session_id) - return session data * exists(sessi

Source from the content-addressed store, hash-verified

26
27
28class SessionMiddleware(object):
29 """Simple session middleware.
30
31 Injects a session dictionary into the context of a request, sets a session cookie,
32 and stores/restores data via a coupled store object.
33
34 A session store object must implement the following methods:
35 * get(session_id) - return session data
36 * exists(session_id) - return boolean if session ID exists or not
37 * set(session_id, session_data) - save session data for given session ID
38
39 The name of the context key can be set via the 'context_name' argument.
40 The cookie arguments are the same as for falcons set_cookie() function, just prefixed with 'cookie_'.
41 """
42
43 __slots__ = (
44 "store",
45 "context_name",
46 "cookie_name",
47 "cookie_expires",
48 "cookie_max_age",
49 "cookie_domain",
50 "cookie_path",
51 "cookie_secure",
52 "cookie_http_only",
53 )
54
55 def __init__(
56 self,
57 store,
58 context_name="session",
59 cookie_name="sid",
60 cookie_expires=None,
61 cookie_max_age=None,
62 cookie_domain=None,
63 cookie_path=None,
64 cookie_secure=True,
65 cookie_http_only=True,
66 ):
67 self.store = store
68 self.context_name = context_name
69 self.cookie_name = cookie_name
70 self.cookie_expires = cookie_expires
71 self.cookie_max_age = cookie_max_age
72 self.cookie_domain = cookie_domain
73 self.cookie_path = cookie_path
74 self.cookie_secure = cookie_secure
75 self.cookie_http_only = cookie_http_only
76
77 def generate_sid(self):
78 """Generate a UUID4 string."""
79 return str(uuid.uuid4())
80
81 def process_request(self, request, response):
82 """Get session ID from cookie, load corresponding session data from coupled store and inject session data into
83 the request context.
84 """
85 sid = request.cookies.get(self.cookie_name, None)

Callers 1

test_session_middlewareFunction · 0.90

Calls

no outgoing calls

Tested by 1

test_session_middlewareFunction · 0.72