askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
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 |
| 2 | capability token for an invoice, invalidating whatever token was live |
| 3 | before (plan/customer-payment-portal-spec.md). |
| 4 | |
| 5 | This is the ONLY place a client can cause invoices.portal_token to be |
| 6 | set. portal_token is schema read_only (never client-submitted directly, |
| 7 | same posture invoice_totals.py's docstring documents for stamped totals); |
| 8 | this object writes it with object_records.update_collection_record's |
| 9 | preserve_read_only=True, the "trusted, explicit, opt-in... never wired to |
| 10 | an HTTP request payload" escape hatch object_records.py itself reserves |
| 11 | for exactly this: a caller that owns the field's derivation, not a |
| 12 | pass-through of client input. The client controls WHETHER a new token is |
| 13 | minted (by calling this action at all); it never controls WHAT the token |
| 14 | is. |
| 15 | |
| 16 | Doubles, today, as the only way a portal_token is minted for an invoice |
| 17 | the owner has not yet had dunned (system_invoice_aging mints one lazily |
| 18 | the first time a dunning email needs a working link, but that only fires |
| 19 | once 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 |
| 21 | lives in objects this task's edit boundary does not include (site_invoices |
| 22 | / any hook on the invoices collection) -- flagged for whoever owns that |
| 23 | surface next, not solved here by reaching outside the boundary. |
| 24 | |
| 25 | Regeneration is the correct response to a leaked link (a customer forwards |
| 26 | the email, a token ends up in a log, etc): the OLD token stops matching |
| 27 | anything the instant this writes, because site_invoice_portal's lookup is |
| 28 | a live scan against the current stored value, not a cached credential. |
| 29 | """ |
| 30 | from __future__ import annotations |
| 31 | |
| 32 | import os |
| 33 | import secrets |
| 34 | |
| 35 | import object_records |
| 36 | |
| 37 | ACTOR = "action_regenerate_portal_link" |
| 38 | DATA_DIR_ENV = "DBBASIC_DATA_DIR" |
| 39 | |
| 40 | |
| 41 | def _base_dir() -> str: |
| 42 | return os.environ.get(DATA_DIR_ENV, "data") |
| 43 | |
| 44 | |
| 45 | def 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}"} |