- Updated the .env.example file to reflect a development environment setup with enhanced secret key requirements and local host settings. - Modified the Docker Compose configuration to enhance security with read-only settings and no-new-privileges options. - Updated requirements.txt to pin package versions for better dependency management. - Enhanced the FastAPI application to include dynamic OpenAPI and documentation URLs based on the environment. - Implemented session versioning in JWT tokens to improve security and user session management. - Added new validation for user roles and password strength in schemas. - Improved email sending logic to handle recipient lists more robustly and added logging for SMTP operations. - Updated dashboard and profile templates to reflect new features and improve user experience.
29 lines
724 B
Python
29 lines
724 B
Python
import base64
|
|
import hashlib
|
|
|
|
from cryptography.fernet import Fernet, InvalidToken
|
|
|
|
from app.core.config import settings
|
|
|
|
|
|
def _fernet() -> Fernet:
|
|
digest = hashlib.sha256(settings.secret_key.encode("utf-8")).digest()
|
|
key = base64.urlsafe_b64encode(digest)
|
|
return Fernet(key)
|
|
|
|
|
|
def encrypt_secret(plain: str) -> str:
|
|
if not plain:
|
|
return ""
|
|
return _fernet().encrypt(plain.encode("utf-8")).decode("utf-8")
|
|
|
|
|
|
def decrypt_secret(cipher: str) -> str:
|
|
if not cipher:
|
|
return ""
|
|
try:
|
|
return _fernet().decrypt(cipher.encode("utf-8")).decode("utf-8")
|
|
except (InvalidToken, ValueError):
|
|
# Bestehende Klartext-Einträge aus älteren Installationen.
|
|
return cipher
|