-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessions.py
More file actions
102 lines (81 loc) · 2.69 KB
/
sessions.py
File metadata and controls
102 lines (81 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""Server-side unlock state (keys stay in RAM; cookie holds opaque session id only)."""
from __future__ import annotations
import secrets
import time
from collections.abc import MutableMapping
from dataclasses import dataclass, field
from modulr_keymaster.profiles import ProfileSecrets
SESSION_COOKIE = "keymaster_session"
# Drop in-memory vault copies when idle (lost cookie / closed tab) or max age reached.
SESSION_IDLE_TIMEOUT_SEC = 30 * 60
SESSION_MAX_LIFETIME_SEC = 8 * 3600
@dataclass
class UnlockedVault:
profiles: list[ProfileSecrets]
@dataclass
class SessionRecord:
vault: UnlockedVault
created_mono: float = field(default_factory=time.monotonic)
last_seen_mono: float = field(default_factory=time.monotonic)
def touch(self) -> None:
self.last_seen_mono = time.monotonic()
def is_expired(self, now: float) -> bool:
if now - self.last_seen_mono > SESSION_IDLE_TIMEOUT_SEC:
return True
if now - self.created_mono > SESSION_MAX_LIFETIME_SEC:
return True
return False
def prune_expired_sessions(
sessions: MutableMapping[str, SessionRecord],
*,
now: float | None = None,
) -> None:
"""Drop sessions past idle or max lifetime (orphaned cookies included)."""
t = time.monotonic() if now is None else now
dead = [sid for sid, rec in sessions.items() if rec.is_expired(t)]
for sid in dead:
del sessions[sid]
def resolve_unlocked_vault(
sessions: MutableMapping[str, SessionRecord],
sid: str | None,
) -> UnlockedVault | None:
"""Return vault for sid if present and not expired; refresh last_seen."""
if not sid:
return None
rec = sessions.get(sid)
if rec is None:
return None
now = time.monotonic()
if rec.is_expired(now):
del sessions[sid]
return None
rec.touch()
return rec.vault
def new_session_id(
sessions: MutableMapping[str, SessionRecord],
vault: UnlockedVault,
) -> str:
"""Register a new unlock session; returns opaque sid."""
prune_expired_sessions(sessions)
sid = secrets.token_urlsafe(32)
sessions[sid] = SessionRecord(vault=vault)
return sid
def find_profile(vault: UnlockedVault, profile_id: str) -> ProfileSecrets | None:
for p in vault.profiles:
if p.id == profile_id:
return p
return None
def replace_session_vault(
sessions: MutableMapping[str, SessionRecord],
sid: str | None,
vault: UnlockedVault,
) -> bool:
"""Swap in-memory profiles after a disk write; returns False if sid missing."""
if not sid:
return False
rec = sessions.get(sid)
if rec is None:
return False
rec.vault = vault
rec.touch()
return True