mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-09 04:06:42 +00:00
The carrier key switched from the tokens file's path and hash to the sys_token_store_users fingerprint once the token store loaded, so the stage-wide park re-rendered the applications SPOT (about 11 s) even when the tokens were identical. Both sources now sign as a digest over exactly the tokens the users hydration reads; the store helpers move to users/store.py to keep the package under the file length cap. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
69 lines
2.0 KiB
Python
69 lines
2.0 KiB
Python
"""The token store the users are hydrated from: the play fact and its digest."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
from collections.abc import Mapping
|
|
from typing import Any
|
|
|
|
STORE_FACT = "sys_token_store_users"
|
|
|
|
_DIGESTS: dict[str, str] = {}
|
|
|
|
|
|
def snapshot(variables: Mapping[str, Any] | None) -> Mapping[str, Any] | None:
|
|
"""Return the token store the play read from the stack host.
|
|
|
|
Args:
|
|
variables: the play's variable scope.
|
|
|
|
Returns:
|
|
The ``sys_token_store_users`` fact, or None outside a play that loaded
|
|
``sys-token-store``.
|
|
"""
|
|
value = (variables or {}).get(STORE_FACT)
|
|
return value if isinstance(value, Mapping) else None
|
|
|
|
|
|
def _tagged(value: Any) -> list:
|
|
return [True, str(value)] if isinstance(value, str) else [False, repr(value)]
|
|
|
|
|
|
def tokens_digest(store_users: Mapping[str, Any]) -> str:
|
|
"""Digest exactly the part of a store that `_hydrate_users_tokens` reads.
|
|
|
|
Args:
|
|
store_users: the store's ``users`` mapping.
|
|
|
|
Returns:
|
|
The SHA-256 of every user entry with a tokens mapping, in store order,
|
|
and its non-empty stripped tokens, memoised per store content.
|
|
"""
|
|
memo_key = hashlib.md5(
|
|
repr(list(store_users.items())).encode("utf-8"), usedforsecurity=False
|
|
).hexdigest()
|
|
digest = _DIGESTS.get(memo_key)
|
|
if digest is not None:
|
|
return digest
|
|
entries: list = []
|
|
for user_key, user_data in store_users.items():
|
|
if not isinstance(user_data, Mapping):
|
|
continue
|
|
tokens = user_data.get("tokens", {})
|
|
if not isinstance(tokens, Mapping):
|
|
continue
|
|
kept = []
|
|
for app_id, token in tokens.items():
|
|
text = "" if token is None else str(token).strip()
|
|
if text:
|
|
kept.append([_tagged(app_id), text])
|
|
entries.append([_tagged(user_key), kept])
|
|
digest = hashlib.sha256(json.dumps(entries).encode("utf-8")).hexdigest()
|
|
_DIGESTS[memo_key] = digest
|
|
return digest
|
|
|
|
|
|
def _reset() -> None:
|
|
_DIGESTS.clear()
|