fix: stabilize WeKnora embed session cookie
This commit is contained in:
1 parent
54fd0e7316
commit
b527bc94bb
3 files changed
+45
-10
No files matched your search
@@ -14,10 +14,11 @@ from typing import Any
|
||||
|
||||
from fastapi import HTTPException
|
||||
|
||||
from deerflow.config.runtime_paths import runtime_home
|
||||
|
||||
WEKNORA_EMBED_COOKIE = "deerflow_weknora_embed"
|
||||
WEKNORA_EMBED_TTL_SECONDS = 15 * 60
|
||||
WEKNORA_EMBED_API_PREFIX = "/api/llmwiki/weknora-embed"
|
||||
WEKNORA_PROXY_SECRET = os.getenv("DEERFLOW_LLMWIKI_PROXY_SECRET") or secrets.token_urlsafe(48)
|
||||
WEKNORA_ROOT_ASSET_PATHS: frozenset[str] = frozenset(
|
||||
{
|
||||
"/favicon.ico",
|
||||
@@ -29,6 +30,31 @@ WEKNORA_ROOT_ASSET_PATHS: frozenset[str] = frozenset(
|
||||
)
|
||||
|
||||
|
||||
def _load_weknora_proxy_secret() -> str:
|
||||
if secret := os.getenv("DEERFLOW_LLMWIKI_PROXY_SECRET"):
|
||||
return secret
|
||||
secret_path = runtime_home() / "weknora_embed_secret"
|
||||
try:
|
||||
if secret_path.is_file():
|
||||
secret = secret_path.read_text(encoding="utf-8").strip()
|
||||
if secret:
|
||||
return secret
|
||||
secret_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
secret = secrets.token_urlsafe(48)
|
||||
try:
|
||||
fd = os.open(str(secret_path), os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
||||
except FileExistsError:
|
||||
return secret_path.read_text(encoding="utf-8").strip()
|
||||
with os.fdopen(fd, "w", encoding="utf-8") as handle:
|
||||
handle.write(secret)
|
||||
return secret
|
||||
except OSError:
|
||||
return secrets.token_urlsafe(48)
|
||||
|
||||
|
||||
WEKNORA_PROXY_SECRET = _load_weknora_proxy_secret()
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WeKnoraEmbedContext:
|
||||
mapping_id: str
|
||||
|
||||
Reference in new issue
Block a user