Skip to content

Commit 5f8526c

Browse files
fernet client management
1 parent 8342c9a commit 5f8526c

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

Dockerfile

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
1-
# Use Python 3.11 slim image as base
21
FROM python:3.11-slim
32

4-
# Set working directory
53
WORKDIR /app
64

7-
# Set environment variables
8-
ENV PYTHONDONTWRITEBYTECODE=1 \
9-
PYTHONUNBUFFERED=1 \
10-
PIP_NO_CACHE_DIR=1 \
11-
PIP_DISABLE_PIP_VERSION_CHECK=1
12-
135
# Install system dependencies
146
RUN apt-get update && apt-get install -y \
15-
gcc \
7+
gcc curl ca-certificates\
168
&& rm -rf /var/lib/apt/lists/*
179

18-
# Copy requirements first for better caching
19-
COPY requirements.txt .
10+
# Download the latest installer
11+
ADD https://astral.sh/uv/install.sh /uv-installer.sh
12+
13+
# Run the installer then remove it
14+
RUN sh /uv-installer.sh && rm /uv-installer.sh
2015

21-
# Install Python dependencies
22-
RUN pip install --no-cache-dir -r requirements.txt
16+
# Ensure the installed binary is on the `PATH`
17+
ENV PATH="/root/.local/bin/:$PATH"
2318

2419
COPY app/ ./app/
2520
COPY main.py .
2621
COPY pyproject.toml .
22+
COPY uv.lock .
23+
24+
RUN uv sync --locked
2725

28-
ENTRYPOINT ["python", "main.py"]
26+
ENTRYPOINT ["uv", "run", "main.py"]

app/services/token_store.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)