Skip to content

Commit aeb8025

Browse files
authored
Hash persisted Firstrade session cache key (#13)
1 parent aefacdd commit aeb8025

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

application/firstrade_client.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import json
1010
import os
1111
from dataclasses import dataclass
12+
from hashlib import sha256
1213
from pathlib import Path
1314
from time import time
1415
from typing import Any, Callable
@@ -267,8 +268,7 @@ def _build_session(self, session_factory: Callable[..., Any], cookie_dir: Path)
267268
)
268269

269270
def _session_cache_path(self, cookie_dir: Path) -> Path:
270-
safe_username = "".join(ch for ch in self.credentials.username if ch.isalnum() or ch in ("-", "_"))
271-
return cookie_dir / f"ft_session{safe_username}.json"
271+
return cookie_dir / f"ft_session_{self._session_cache_identity()}.json"
272272

273273
def _load_session_cache(self, cookie_dir: Path) -> dict[str, Any] | None:
274274
path = self._session_cache_path(cookie_dir)
@@ -382,8 +382,13 @@ def _session_state_store(self) -> GcsStateStore | None:
382382
)
383383

384384
def _session_state_key(self) -> str:
385-
safe_username = "".join(ch for ch in self.credentials.username if ch.isalnum() or ch in ("-", "_"))
386-
return f"sessions/{safe_username or 'unknown'}/latest.json"
385+
return f"sessions/{self._session_cache_identity()}/latest.json"
386+
387+
def _session_cache_identity(self) -> str:
388+
username = str(self.credentials.username or "").strip().lower()
389+
if not username:
390+
return "unknown"
391+
return sha256(username.encode("utf-8")).hexdigest()[:16]
387392

388393
def require_connected(self) -> tuple[Any, Any]:
389394
if self.session is None or self.account_data is None:

0 commit comments

Comments
 (0)