task: set User-Agent on claim (Cloudflare 1010), show error bodies

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
BobbyandClaude Opus 5.5 committed 2026-11-25 15:06:40 -05:00
1 parent 76ee576a02
commit 06c7091bb3
1 file changed
+8 -3
+8 -3
View File
@@ -37,6 +37,8 @@ from pathlib import Path
STATE = Path.home() / ".local/state/hapi-simplefin-probe" STATE = Path.home() / ".local/state/hapi-simplefin-probe"
DAY = 86400 DAY = 86400
# Cloudflare in front of SimpleFIN rejects Python-urllib's default UA (error 1010).
UA = "hapi-simplefin-probe/1"
def die(msg: str) -> None: def die(msg: str) -> None:
@@ -91,12 +93,12 @@ def get_accounts(url: str, **params: str) -> dict:
base = urllib.parse.urlunsplit((u.scheme, netloc, u.path.rstrip("/") + "/accounts", "", "")) base = urllib.parse.urlunsplit((u.scheme, netloc, u.path.rstrip("/") + "/accounts", "", ""))
full = base + ("?" + urllib.parse.urlencode(params) if params else "") full = base + ("?" + urllib.parse.urlencode(params) if params else "")
cred = base64.b64encode(f"{urllib.parse.unquote(u.username)}:{urllib.parse.unquote(u.password)}".encode()).decode() cred = base64.b64encode(f"{urllib.parse.unquote(u.username)}:{urllib.parse.unquote(u.password)}".encode()).decode()
req = urllib.request.Request(full, headers={"Authorization": f"Basic {cred}", "User-Agent": "simplefin-probe"}) req = urllib.request.Request(full, headers={"Authorization": f"Basic {cred}", "User-Agent": UA})
try: try:
with urllib.request.urlopen(req, timeout=120) as r: with urllib.request.urlopen(req, timeout=120) as r:
return json.load(r) return json.load(r)
except urllib.error.HTTPError as e: except urllib.error.HTTPError as e:
die(f"HTTP {e.code} from /accounts (params {sorted(params)})") die(f"HTTP {e.code} from /accounts (params {sorted(params)}): {e.read()[:200].decode(errors='replace')}")
return {} return {}
@@ -168,9 +170,12 @@ def cmd_claim() -> None:
claim = base64.b64decode(token).decode() claim = base64.b64decode(token).decode()
if not claim.startswith("https://"): if not claim.startswith("https://"):
die("Setup Token did not decode to an https URL") die("Setup Token did not decode to an https URL")
req = urllib.request.Request(claim, method="POST", data=b"", headers={"Content-Length": "0"}) req = urllib.request.Request(claim, method="POST", data=b"", headers={"Content-Length": "0", "User-Agent": UA})
try:
with urllib.request.urlopen(req, timeout=60) as r: with urllib.request.urlopen(req, timeout=60) as r:
url = r.read().decode().strip() url = r.read().decode().strip()
except urllib.error.HTTPError as e:
die(f"HTTP {e.code} from claim: {e.read()[:200].decode(errors='replace')}")
print("\nAccess URL obtained (not printed). A Setup Token can be claimed only once,") print("\nAccess URL obtained (not printed). A Setup Token can be claimed only once,")
print("so save the Access URL to Vaultwarden now; `run` will prompt for it.") print("so save the Access URL to Vaultwarden now; `run` will prompt for it.")
if input("Show it once to copy into Vaultwarden? [y/N] ").lower() == "y": if input("Show it once to copy into Vaultwarden? [y/N] ").lower() == "y":