Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
regenerate_portal_link.py · 3.3 KB · 81 lines
1"""action_regenerate_portal_link -- mint a fresh customer-payment-portal
2capability token for an invoice, invalidating whatever token was live
3before (plan/customer-payment-portal-spec.md).
4
5This is the ONLY place a client can cause invoices.portal_token to be
6set. portal_token is schema read_only (never client-submitted directly,
7same posture invoice_totals.py's docstring documents for stamped totals);
8this object writes it with object_records.update_collection_record's
9preserve_read_only=True, the "trusted, explicit, opt-in... never wired to
10an HTTP request payload" escape hatch object_records.py itself reserves
11for exactly this: a caller that owns the field's derivation, not a
12pass-through of client input. The client controls WHETHER a new token is
13minted (by calling this action at all); it never controls WHAT the token
14is.
15
16Doubles, today, as the only way a portal_token is minted for an invoice
17the owner has not yet had dunned (system_invoice_aging mints one lazily
18the first time a dunning email needs a working link, but that only fires
19once an invoice is overdue). Wiring eager token issuance into the invoice
20"send" flow itself (draft -> sent) is a natural next step, but that logic
21lives in objects this task's edit boundary does not include (site_invoices
22/ any hook on the invoices collection) -- flagged for whoever owns that
23surface next, not solved here by reaching outside the boundary.
24
25Regeneration is the correct response to a leaked link (a customer forwards
26the email, a token ends up in a log, etc): the OLD token stops matching
27anything the instant this writes, because site_invoice_portal's lookup is
28a live scan against the current stored value, not a cached credential.
29"""
30from __future__ import annotations
31
32import os
33import secrets
34
35import object_records
36
37ACTOR = "action_regenerate_portal_link"
38DATA_DIR_ENV = "DBBASIC_DATA_DIR"
39
40
41def _base_dir() -> str:
42 return os.environ.get(DATA_DIR_ENV, "data")
43
44
45def POST(request):
46 identity = request.get("_identity") or {}
47 user_id = identity.get("user_id") or ""
48 is_admin = "admin" in (identity.get("roles") or [])
49 if not user_id:
50 return {"status": 403, "error": "Sign in to regenerate a portal link."}
51
52 invoice_id = str(request.get("invoice_id") or "").strip()
53 if not invoice_id:
54 return {"status": 400, "error": "invoice_id is required"}
55
56 base = _base_dir()
57 try:
58 invoice = object_records.get_collection_record("invoices", invoice_id, base_dir=base)
59 except Exception:
60 return {"status": 404, "error": f"Invoice not found: {invoice_id}"}
61
62 # Owner-or-admin, same shape as app-finance's reverse_journal and
63 # app-banking's resolve_bank_line: the object enforces this itself
64 # (execute is granted broadly to "registered"; ownership is not a
65 # row_filter concern for an action, it is this check).
66 if not is_admin and invoice.get("owner_id") != user_id:
67 return {
68 "status": 403,
69 "error": "Only the invoice's owner (or an admin) may regenerate its portal link.",
70 }
71
72 token = secrets.token_urlsafe(32)
73 object_records.update_collection_record(
74 "invoices",
75 invoice_id,
76 {"portal_token": token},
77 base_dir=base,
78 actor=ACTOR,
79 preserve_read_only=True,
80 )
81 return {"status": 200, "ok": True, "invoice_id": invoice_id, "path": f"/pay/{token}"}