Files
monica_site/site/social/crypto.py
T
westfarn 1f7d78de64
Deploy Beta / unit-tests (push) Successful in 9s
Deploy Beta / docker (push) Successful in 17s
Deploy Beta / deploy-beta (push) Successful in 2m31s
Add Django site, Docker packaging, and beta/prod Gitea deploys.
Unignore site/ (was blocked by mkdocs /site rule), add compose/Docker/uv tooling, and split deploys so push to main goes to beta while prod stays manual.
2026-08-08 07:32:55 -05:00

28 lines
803 B
Python

"""Token encryption helpers for SocialAccount."""
from cryptography.fernet import Fernet, InvalidToken
from django.conf import settings
def _fernet() -> Fernet:
key = settings.SOCIAL_TOKEN_ENCRYPTION_KEY
if not key:
# Dev-only fallback — generate is not stable across restarts; set the env var.
key = Fernet.generate_key().decode()
if isinstance(key, str):
key = key.encode()
return Fernet(key)
def encrypt_tokens(plaintext: str) -> str:
return _fernet().encrypt(plaintext.encode()).decode()
def decrypt_tokens(ciphertext: str) -> str:
if not ciphertext:
return ""
try:
return _fernet().decrypt(ciphertext.encode()).decode()
except InvalidToken as exc:
raise ValueError("Unable to decrypt social tokens") from exc