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

9 downloads

Install on DigitalOcean 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.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 49 minutes ago History
regenerate_portal_link.py · 3.5 KB · 83 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 # access: owner-or-admin check on the invoice, on the next lines
59 invoice = object_records.get_collection_record("invoices", invoice_id, base_dir=base)
60 except Exception:
61 return {"status": 404, "error": f"Invoice not found: {invoice_id}"}
62
63 # Owner-or-admin, same shape as app-finance's reverse_journal and
64 # app-banking's resolve_bank_line: the object enforces this itself
65 # (execute is granted broadly to "registered"; ownership is not a
66 # row_filter concern for an action, it is this check).
67 if not is_admin and invoice.get("owner_id") != user_id:
68 return {
69 "status": 403,
70 "error": "Only the invoice's owner (or an admin) may regenerate its portal link.",
71 }
72
73 token = secrets.token_urlsafe(32)
74 # access: owner-or-admin check on the invoice, on the next lines
75 object_records.update_collection_record(
76 "invoices",
77 invoice_id,
78 {"portal_token": token},
79 base_dir=base,
80 actor=ACTOR,
81 preserve_read_only=True,
82 )
83 return {"status": 200, "ok": True, "invoice_id": invoice_id, "path": f"/pay/{token}"}