@@ -21,7 +21,6 @@ class TokenStore:
2121
2222 def __init__ (self ) -> None :
2323 self ._client : redis .Redis | None = None
24- self ._cipher : Fernet | None = None
2524 # Cache decrypted payloads for 1 day (86400s) to reduce Redis hits
2625 # Max size 5000 allows many active users without eviction
2726 self ._payload_cache : TTLCache = TTLCache (maxsize = 5000 , ttl = 86400 )
@@ -42,25 +41,24 @@ def _ensure_secure_salt(self) -> None:
4241 )
4342
4443 def _get_cipher (self ) -> Fernet :
45- """Get or create Fernet cipher instance based on TOKEN_SALT."""
4644 salt = b"x7FDf9kypzQ1LmR32b8hWv49sKq2Pd8T"
47- if self ._cipher is None :
48- kdf = PBKDF2HMAC (
49- algorithm = hashes .SHA256 (),
50- length = 32 ,
51- salt = salt ,
52- iterations = 200_000 ,
53- )
45+ kdf = PBKDF2HMAC (
46+ algorithm = hashes .SHA256 (),
47+ length = 32 ,
48+ salt = salt ,
49+ iterations = 200_000 ,
50+ )
5451
55- key = base64 .urlsafe_b64encode (kdf .derive (settings .TOKEN_SALT .encode ("utf-8" )))
56- self ._cipher = Fernet (key )
57- return self ._cipher
52+ key = base64 .urlsafe_b64encode (kdf .derive (settings .TOKEN_SALT .encode ("utf-8" )))
53+ return Fernet (key )
5854
5955 def encrypt_token (self , token : str ) -> str :
60- return self ._cipher .encrypt (token .encode ("utf-8" )).decode ("utf-8" )
56+ cipher = self ._get_cipher ()
57+ return cipher .encrypt (token .encode ("utf-8" )).decode ("utf-8" )
6158
6259 def decrypt_token (self , enc : str ) -> str :
63- return self ._cipher .decrypt (enc .encode ("utf-8" )).decode ("utf-8" )
60+ cipher = self ._get_cipher ()
61+ return cipher .decrypt (enc .encode ("utf-8" )).decode ("utf-8" )
6462
6563 async def _get_client (self ) -> redis .Redis :
6664 if self ._client is None :
0 commit comments