19 lines
649 B
Python
19 lines
649 B
Python
from cryptography.fernet import Fernet
|
|
from app.core.config import settings
|
|
|
|
def get_cipher():
|
|
"""Get Fernet cipher for encryption/decryption"""
|
|
if not settings.GOOGLE_TOKEN_ENCRYPTION_KEY:
|
|
raise ValueError("GOOGLE_TOKEN_ENCRYPTION_KEY not set")
|
|
return Fernet(settings.GOOGLE_TOKEN_ENCRYPTION_KEY.encode())
|
|
|
|
def encrypt_token(token: str) -> str:
|
|
"""Encrypt a token string"""
|
|
cipher = get_cipher()
|
|
return cipher.encrypt(token.encode()).decode()
|
|
|
|
def decrypt_token(encrypted_token: str) -> str:
|
|
"""Decrypt an encrypted token"""
|
|
cipher = get_cipher()
|
|
return cipher.decrypt(encrypted_token.encode()).decode()
|