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 | Session expiration timestamp (ISO format). |
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. Uses system temp directory with encryption for security.
Security features: 1. Uses system temp directory (auto-cleaned on reboot) 2. File permissions restricted to current user (0o600) 3. Session data encrypted with machine-specific key 4. Automatic expiration and cleanup 5. Process-specific session binding
Attributes:
| Name | Type | Description |
|---|---|---|
session_dir | Directory for session files (in system temp). | |
session_file | Path to the encrypted session file. | |
ttl | Default time-to-live for sessions. |
Example
# Initialize manager
manager = SessionManager(
app_name="audex",
default_ttl=timedelta(hours=8),
)
await manager.init()
# Login
await manager.login(
doctor_id="doctor-abc123",
doctor_name="张医生",
eid="dr_zhang",
)
# 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.
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, uses default. | 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.
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. |
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