mirror of
https://github.com/ZTatman/myst.git
synced 2026-11-21 04:17:59 +00:00
- Fix logout cookie not reaching browser (same pattern as callback) - Add cascade delete on Grant.user_id FK - Bump httpx to >=0.28.1 - Return plain dict from /dashboard instead of ORM session - Add decrypt functions and key validation to crypto.py - Constrain session token_hash to String(64), add expires_at index - Move Session import to TYPE_CHECKING in user.py - Add indexes on email and LinkedIdentity.user_id
52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
"""Fernet (AES) encryption for OAuth tokens at rest."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from cryptography.fernet import Fernet
|
|
from pydantic import SecretStr
|
|
|
|
|
|
def fernet_from_encryption_key(key: SecretStr) -> Fernet | None:
|
|
raw = key.get_secret_value().strip()
|
|
if not raw:
|
|
return None
|
|
try:
|
|
return Fernet(raw.encode())
|
|
except Exception as e:
|
|
msg = (
|
|
f"Invalid OAUTH_TOKEN_ENCRYPTION_KEY: {e}. "
|
|
"Generate a valid key with cryptography.fernet.Fernet.generate_key().decode()"
|
|
)
|
|
raise ValueError(msg) from e
|
|
|
|
|
|
def encrypt_oauth_token(fernet: Fernet, raw_token: str) -> str:
|
|
return fernet.encrypt(raw_token.encode()).decode()
|
|
|
|
|
|
def encrypt_optional(fernet: Fernet | None, raw_token: str | None) -> str | None:
|
|
if raw_token is None:
|
|
return None
|
|
if fernet is None:
|
|
msg = (
|
|
"OAuth token encryption is not configured: set OAUTH_TOKEN_ENCRYPTION_KEY "
|
|
"to the output of cryptography.fernet.Fernet.generate_key().decode()"
|
|
)
|
|
raise ValueError(msg)
|
|
return encrypt_oauth_token(fernet, raw_token)
|
|
|
|
|
|
def decrypt_oauth_token(fernet: Fernet, encrypted_token: str) -> str:
|
|
return fernet.decrypt(encrypted_token.encode()).decode()
|
|
|
|
|
|
def decrypt_optional(fernet: Fernet | None, encrypted_token: str | None) -> str | None:
|
|
if encrypted_token is None:
|
|
return None
|
|
if fernet is None:
|
|
msg = (
|
|
"OAuth token encryption is not configured: set OAUTH_TOKEN_ENCRYPTION_KEY "
|
|
"to the output of cryptography.fernet.Fernet.generate_key().decode()"
|
|
)
|
|
raise ValueError(msg)
|
|
return decrypt_oauth_token(fernet, encrypted_token)
|