Session Manager¶
session ¶
Classes¶
SessionData ¶
Bases: NamedTuple
Session data structure.
Attributes:
| Name | Type | Description |
|---|---|---|
doctor_id | str | Doctor's unique identifier. |
doctor_name | str | None | Doctor's display name. |
eid | str | Doctor's eid. |
created_at | str | Session creation timestamp (ISO format). |
expires_at | str | None | Session expiration timestamp (ISO format), None means never expires. |
Functions¶
to_dict ¶
Convert to dictionary for JSON serialization.
Source code in audex/lib/session.py
from_dict classmethod ¶
from_dict(data: dict[str, str | None]) -> SessionData
Create from dictionary.
Source code in audex/lib/session.py
SessionManager ¶
Bases: LoggingMixin, AsyncContextMixin
Secure local session manager with automatic state management.
This manager maintains session state automatically - no need to manually manage tokens. Session persists across application restarts until it expires (or never expires if ttl is None).
Security features: 1. Uses application data directory (persistent storage) 2. File permissions restricted to current user (0o600) 3. Session data encrypted with Fernet (AES-128-CBC + HMAC-SHA256) 4. Automatic expiration and cleanup (optional) 5. Machine-specific encryption key
Attributes:
| Name | Type | Description |
|---|---|---|
session_dir | Directory for session files (in app data directory). | |
session_file | Path to the encrypted session file. | |
ttl | Default time-to-live for sessions (None = never expires). |
Example
# Initialize manager
manager = SessionManager(
app_name="audex",
default_ttl=timedelta(
hours=8
), # or None for no expiration
)
await manager.init()
# Login with expiration
await manager.login(
doctor_id="doctor-abc123",
doctor_name="张医生",
eid="dr_zhang",
)
# Login without expiration (永久登录)
await manager.login(
doctor_id="doctor-abc123",
doctor_name="张医生",
eid="dr_zhang",
ttl=None, # 永不过期
)
# Check if logged in (no token needed!)
if await manager.is_logged_in():
session = await manager.get_session()
print(f"Logged in as: {session.doctor_name}")
# Works across app restarts (if not expired)
# ... restart app ...
if await manager.is_logged_in():
print("Auto-logged in!")
# Logout
await manager.logout()
# Cleanup
await manager.close()
Source code in audex/lib/session.py
Functions¶
init async ¶
Initialize the session manager.
Creates session directory with restricted permissions and starts automatic cleanup task (if ttl is set).
Source code in audex/lib/session.py
close async ¶
Close the session manager.
Stops the cleanup task. Session file is preserved for next startup.
Source code in audex/lib/session.py
login async ¶
login(doctor_id: str, eid: str, *, doctor_name: str | None = None, ttl: timedelta | None = None) -> SessionData
Create a login session.
If a session already exists, it will be replaced with the new one. Session persists across application restarts until expiration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doctor_id | str | Doctor's unique identifier. | required |
doctor_name | str | None | Doctor's display name. | None |
eid | str | Doctor's eid. | required |
ttl | timedelta | None | Time-to-live for the session. If None, session never expires. If not provided, uses default ttl from init. | None |
Returns:
| Type | Description |
|---|---|
SessionData | Created SessionData. |
Example
Source code in audex/lib/session.py
logout async ¶
Logout by deleting the session.
Returns:
| Type | Description |
|---|---|
bool | True if session was deleted, False if no session exists. |
Source code in audex/lib/session.py
is_logged_in async ¶
Check if there's an active (non-expired) session.
Returns:
| Type | Description |
|---|---|
bool | True if there's an active session, False otherwise. |
Source code in audex/lib/session.py
get_session async ¶
get_session() -> SessionData | None
Get current session if exists and not expired.
Returns:
| Type | Description |
|---|---|
SessionData | None | SessionData if session exists and is valid, None otherwise. |
Example
Source code in audex/lib/session.py
get_doctor_id async ¶
Get current logged-in doctor's ID.
Returns:
| Type | Description |
|---|---|
str | None | Doctor ID if logged in, None otherwise. |
Example
Source code in audex/lib/session.py
extend_session async ¶
Extend current session expiration time.
Note: This only works if the session has an expiration time. For sessions with no expiration (ttl=None), this operation has no effect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
extra_ttl | timedelta | Additional time to add to expiration. | required |
Returns:
| Type | Description |
|---|---|
bool | True if session was extended, False if no session exists or session never expires. |
Example
Source code in audex/lib/session.py
SessionError ¶
Bases: AudexError
Base exception for session errors.
Source code in audex/exceptions.py
SessionExpiredError ¶
SessionNotFoundError ¶
options: show_root_heading: true show_source: true heading_level: 2 members_order: source show_signature_annotations: true separate_signature: true