portal: answer for a lapsed membership instead of leaving the dashboard blank
ci/woodpecker/push/woodpecker Pipeline was canceled
ci/woodpecker/push/woodpecker Pipeline was canceled
New read-only /broker/membership-status, authenticated by the broker secret with a verifier that admits a member row in ANY state (usage and keys still require active). Returns active/state, the latest allowance_log decision with its evidence, the OC minimum contribution (MIN_CONTRIBUTION, copy-only — OC stays the authority), credits, allowance and the contribute URL. MIN_CONTRIBUTION is deliberately NOT wired into deactivation: a number we half-know should not cost anyone access.
This commit is contained in:
1 parent
6041c0cdb2
commit
27b0ea564a
2 files changed
+88
No files matched your search
+61
@@ -339,6 +339,10 @@ OC_PERSONAL_TOKEN = os.environ.get("OC_PERSONAL_TOKEN", "")
|
||||
# regardless of their $10/15/20 contribution. Governance decision (Loomio).
|
||||
MEMBER_BUDGET = float(os.environ.get("MEMBER_BUDGET", "15.0"))
|
||||
|
||||
# Open Collective's flexible-tier floor (observed 2026-11-17). Used for COPY and
|
||||
# evidence only — never as a deactivation trigger: OC remains the authority on dues.
|
||||
MIN_CONTRIBUTION = float(os.environ.get("MIN_CONTRIBUTION", "5.0") or 5.0)
|
||||
|
||||
# --- Guest accounts (operator decisions, 2026-11-08) -------------------------- #
|
||||
# A member can give a friend access to the co-op with $2 of credits, no Open
|
||||
# Collective membership required. The guest is a ONE-TIME non-expiring credit
|
||||
@@ -6575,6 +6579,63 @@ async def broker_cancel_gift_alias(request: Request):
|
||||
_GUEST_INVITE_LOCK = asyncio.Lock()
|
||||
|
||||
|
||||
def _verify_broker_identity_any_state(request: Request) -> str:
|
||||
"""Broker auth that admits a member row in ANY state, for STATUS reads only.
|
||||
|
||||
`_verify_broker_secret` refuses a lapsed member, which is right for usage,
|
||||
keys and everything else — but it left a lapsed member's dashboard with nothing
|
||||
to render. This verifier is scoped to one read-only route that says why you are
|
||||
not active and what restores access; it never grants access to usage or keys.
|
||||
"""
|
||||
if not BROKER_SECRET:
|
||||
raise HTTPException(503, "Broker secret not configured")
|
||||
provided = request.headers.get("x-broker-secret", "")
|
||||
if not secrets.compare_digest(provided, BROKER_SECRET):
|
||||
raise HTTPException(401, "Invalid broker secret")
|
||||
identity = (request.headers.get("x-member-email") or request.headers.get("x-member-user") or "").strip()
|
||||
if not identity:
|
||||
raise HTTPException(401, "Missing member identity")
|
||||
email = resolve_member_email(identity)
|
||||
conn = get_db()
|
||||
row = conn.execute("SELECT email FROM members WHERE email = ?", (email,)).fetchone()
|
||||
conn.close()
|
||||
if not row:
|
||||
raise HTTPException(403, "No membership on file")
|
||||
return email
|
||||
|
||||
|
||||
@app.get("/broker/membership-status")
|
||||
@limiter.limit("30/minute")
|
||||
async def broker_membership_status(request: Request):
|
||||
"""Why is my dashboard empty? State, reason, minimum and what restores access.
|
||||
|
||||
Answers in ANY membership state, including lapsed and awaiting-contribution,
|
||||
so the dashboard can explain rather than render a blank page.
|
||||
"""
|
||||
email = _verify_broker_identity_any_state(request)
|
||||
conn = get_db()
|
||||
row = conn.execute(
|
||||
"SELECT active, slug, source, credit_balance, balance FROM members WHERE email = ?",
|
||||
(email,)).fetchone()
|
||||
decision = conn.execute(
|
||||
"SELECT cycle, decision, evidence, decided_at FROM allowance_log "
|
||||
"WHERE email = ? ORDER BY decided_at DESC LIMIT 1", (email,)).fetchone()
|
||||
conn.close()
|
||||
active, slug, source, credits, balance = row
|
||||
return {
|
||||
"active": bool(active),
|
||||
"state": "active" if active else "inactive",
|
||||
"source": source,
|
||||
"minimum_contribution": MIN_CONTRIBUTION,
|
||||
"contribute_url": credit_pack_url(),
|
||||
"allowance": float(balance if balance is not None else 0.0),
|
||||
"credits": float(credits or 0.0),
|
||||
"last_decision": ({"cycle": decision[0], "decision": decision[1],
|
||||
"evidence": decision[2], "decided_at": decision[3]}
|
||||
if decision else None),
|
||||
}
|
||||
|
||||
|
||||
@app.get("/broker/guests")
|
||||
@limiter.limit("30/minute")
|
||||
async def broker_guest_list(request: Request):
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import asyncio, os, sys, tempfile
|
||||
os.environ["DB_PATH"] = os.path.join(tempfile.mkdtemp(), "status.db")
|
||||
os.environ.setdefault("BROKER_SECRET", "test-secret")
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
import app.main as m
|
||||
m.DB_PATH = os.environ["DB_PATH"]
|
||||
m.BROKER_SECRET = "test-secret"
|
||||
|
||||
conn = m.get_db()
|
||||
m._ensure_allowance_tables(conn)
|
||||
conn.execute("INSERT OR REPLACE INTO members (email, slug, active, credit_balance, balance, source) VALUES (?,?,?,?,?,?)",
|
||||
("lapsed@example.org", "lapsed", 0, 3.0, 0.0, "oc"))
|
||||
conn.execute("INSERT INTO allowance_log (email, cycle, decision, evidence, anchor_day) VALUES (?,?,?,?,?)",
|
||||
("lapsed@example.org", "2026-11-05", "awaiting_contribution", "no live recurring contribution in this cycle", 5))
|
||||
conn.commit(); conn.close()
|
||||
|
||||
import httpx
|
||||
async def run():
|
||||
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=m.app), base_url="http://p", timeout=60) as c:
|
||||
r = await c.get("/broker/membership-status", headers={"x-broker-secret": "test-secret", "x-member-email": "lapsed@example.org"})
|
||||
return r.status_code, r.json()
|
||||
st, d = asyncio.run(run())
|
||||
print(" route:", st, "| active:", d.get("active"), "| state:", d.get("state"), "| min:", d.get("minimum_contribution"), "| decision:", (d.get("last_decision") or {}).get("decision"))
|
||||
assert st == 200 and d["active"] is False and d["minimum_contribution"] == 5.0
|
||||
assert (d.get("last_decision") or {}).get("decision") == "awaiting_contribution"
|
||||
assert d.get("contribute_url")
|
||||
print("MEMBERSHIP STATUS ROUTE OK: answers for a lapsed member, states minimum and last decision")
|
||||
Reference in new issue
Block a user