import logging from os import getenv from cryptography.fernet import Fernet from credentials_loader import get_secret,MissingCredentialError logger = logging.getLogger(__name__) try: encryption_key = get_secret(getenv("ENCRYPTION_KEY",""),"/etc/secrets/encryption_key") logger.debug("Loaded encryption key") except MissingCredentialError: logger.fatal("Could not load encryption key") raise fernet = Fernet(encryption_key) def encrypt_token(token: str) -> str: """Encrypt a token string.""" return fernet.encrypt(token.encode()).decode() def decrypt_token(token: str) -> str: """Decrypt a token string.""" return fernet.decrypt(token.encode()).decode()