Skip to content

Commit 0bf1514

Browse files
feat: Introduce token redaction utility and apply it to log messages for enhanced security.
1 parent 5181a9d commit 0bf1514

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

app/services/token_store.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import redis.asyncio as redis
77
from cachetools import TTLCache
8-
from cryptography.fernet import Fernet
8+
from cryptography.fernet import Fernet, InvalidToken
99
from cryptography.hazmat.primitives import hashes
1010
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
1111
from loguru import logger
@@ -43,11 +43,12 @@ def _ensure_secure_salt(self) -> None:
4343

4444
def _get_cipher(self) -> Fernet:
4545
"""Get or create Fernet cipher instance based on TOKEN_SALT."""
46+
salt = b"x7FDf9kypzQ1LmR32b8hWv49sKq2Pd8T"
4647
if self._cipher is None:
4748
kdf = PBKDF2HMAC(
4849
algorithm=hashes.SHA256(),
4950
length=32,
50-
salt=b"", # empty salt
51+
salt=salt,
5152
iterations=200_000,
5253
)
5354

@@ -120,7 +121,7 @@ async def get_user_data(self, token: str) -> dict[str, Any] | None:
120121
data["authKey"] = self.decrypt_token(data["authKey"])
121122
self._payload_cache[token] = data
122123
return data
123-
except json.JSONDecodeError:
124+
except (json.JSONDecodeError, InvalidToken):
124125
return None
125126

126127
async def delete_token(self, token: str = None, key: str = None) -> None:

0 commit comments

Comments
 (0)