Update Capability Catalog and Club Membership Documentation
Deploy Development / deploy (push) Successful in 49s
Test Suite / pytest-backend (push) Failing after 0s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 15s
Test Suite / playwright-tests (push) Has been cancelled
Test Suite / k6 /health Baseline (push) Has been cancelled
Deploy Development / deploy (push) Successful in 49s
Test Suite / pytest-backend (push) Failing after 0s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 15s
Test Suite / playwright-tests (push) Has been cancelled
Test Suite / k6 /health Baseline (push) Has been cancelled
- Revised the status in the Capability Catalog to reflect partial implementation (M3). - Added a new reference to `MEMBERSHIP_RBAC_DECISIONS_2026-06.md` in both the Capability Catalog and Club Membership documentation. - Enhanced the Club Membership documentation with details on product decisions and onboarding phases. - Implemented middleware in the backend to restrict access for unverified users and those pending club membership. - Updated versioning in `version.py` to reflect changes in account lifecycle management.
This commit is contained in:
1 parent
30dc30c7aa
commit
a2f60d3f46
12 files changed
+765
-15
No files matched your search
@@ -0,0 +1,165 @@
|
||||
"""
|
||||
API-Gates für Onboarding (Phase A — MEMBERSHIP_RBAC_DECISIONS_2026-06.md §1.1).
|
||||
|
||||
Blockiert Domänen-APIs für unverified / verified_pending_club vor dem Router.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
from typing import Optional, Tuple
|
||||
|
||||
from account_lifecycle import resolve_account_state
|
||||
from club_tenancy import is_platform_admin, memberships_with_roles
|
||||
|
||||
# Öffentlich ohne Session
|
||||
PUBLIC_API_PREFIXES = (
|
||||
"/api/auth/login",
|
||||
"/api/auth/register",
|
||||
"/api/auth/forgot-password",
|
||||
"/api/auth/reset-password",
|
||||
"/api/auth/verify/",
|
||||
"/api/legal-documents/",
|
||||
"/api/clubs/public-directory",
|
||||
"/api/version",
|
||||
"/api/health/",
|
||||
"/health",
|
||||
)
|
||||
|
||||
# Mit Session, unabhängig vom account_state (Logout, Profil lesen, …)
|
||||
AUTH_INFRA_PREFIXES = (
|
||||
"/api/auth/logout",
|
||||
"/api/auth/me",
|
||||
"/api/auth/status",
|
||||
"/api/auth/pin",
|
||||
"/api/auth/resend-verification",
|
||||
"/api/profiles/me",
|
||||
"/api/me/entitlements",
|
||||
)
|
||||
|
||||
# Zusätzlich für verified_pending_club (Verein bewerben)
|
||||
PENDING_CLUB_PREFIXES = (
|
||||
"/api/me/club-join-requests",
|
||||
)
|
||||
|
||||
_PROFILE_MUTATION_RE = re.compile(r"^/api/profiles/(\d+)$")
|
||||
|
||||
|
||||
def api_onboarding_gate_enabled() -> bool:
|
||||
return os.getenv("ACCOUNT_GATE_API_ENFORCE", "1").strip() == "1"
|
||||
|
||||
|
||||
def normalize_api_path(path: str) -> str:
|
||||
p = (path or "").split("?", 1)[0].strip()
|
||||
if not p.startswith("/"):
|
||||
p = "/" + p
|
||||
if len(p) > 1 and p.endswith("/"):
|
||||
p = p[:-1]
|
||||
return p
|
||||
|
||||
|
||||
def is_public_api_path(path: str) -> bool:
|
||||
p = normalize_api_path(path)
|
||||
return any(p == pref or p.startswith(pref) for pref in PUBLIC_API_PREFIXES)
|
||||
|
||||
|
||||
def _path_allowed_for_state(path: str, method: str, account_state: str, profile_id: int) -> bool:
|
||||
p = normalize_api_path(path)
|
||||
m = (method or "GET").upper()
|
||||
|
||||
for pref in AUTH_INFRA_PREFIXES:
|
||||
if p == pref or p.startswith(pref + "/"):
|
||||
return True
|
||||
|
||||
match = _PROFILE_MUTATION_RE.match(p)
|
||||
if match and m in ("PUT", "PATCH") and int(match.group(1)) == int(profile_id):
|
||||
return True
|
||||
|
||||
if account_state == "unverified":
|
||||
return False
|
||||
|
||||
if account_state == "verified_pending_club":
|
||||
for pref in PENDING_CLUB_PREFIXES:
|
||||
if p == pref or p.startswith(pref + "/"):
|
||||
return True
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def resolve_account_state_for_token(cur, session_row: dict) -> str:
|
||||
profile_id = int(session_row["profile_id"])
|
||||
role = (session_row.get("role") or "").lower()
|
||||
cur.execute(
|
||||
"SELECT COALESCE(email_verified, false) AS email_verified FROM profiles WHERE id = %s",
|
||||
(profile_id,),
|
||||
)
|
||||
prof = cur.fetchone()
|
||||
email_verified = bool(prof.get("email_verified")) if prof else False
|
||||
memberships = memberships_with_roles(cur, profile_id, active_only=True)
|
||||
has_active = len(memberships) > 0
|
||||
return resolve_account_state(
|
||||
email_verified=email_verified,
|
||||
global_role=role,
|
||||
has_active_membership=has_active,
|
||||
)
|
||||
|
||||
|
||||
def check_api_onboarding_gate(
|
||||
*,
|
||||
path: str,
|
||||
method: str,
|
||||
profile_id: int,
|
||||
account_state: str,
|
||||
) -> Tuple[bool, Optional[str]]:
|
||||
"""
|
||||
Returns (allowed, reason).
|
||||
active_member / platform_admin → immer erlaubt (Domain).
|
||||
"""
|
||||
if not api_onboarding_gate_enabled():
|
||||
return True, None
|
||||
|
||||
if account_state in ("active_member", "platform_admin"):
|
||||
return True, None
|
||||
|
||||
if _path_allowed_for_state(path, method, account_state, profile_id):
|
||||
return True, None
|
||||
|
||||
return False, f"account_state_{account_state}"
|
||||
|
||||
|
||||
def evaluate_request_gate(token: Optional[str], path: str, method: str) -> Tuple[bool, Optional[str], Optional[str]]:
|
||||
"""
|
||||
Vollständige Prüfung inkl. Session-Lookup.
|
||||
Returns: allowed, reason, account_state (für Logging)
|
||||
"""
|
||||
if not api_onboarding_gate_enabled():
|
||||
return True, None, None
|
||||
|
||||
p = normalize_api_path(path)
|
||||
if not p.startswith("/api/"):
|
||||
return True, None, None
|
||||
if is_public_api_path(p):
|
||||
return True, None, None
|
||||
if not token:
|
||||
return True, None, None
|
||||
|
||||
from auth import get_session
|
||||
from db import get_db, get_cursor
|
||||
|
||||
session = get_session(token)
|
||||
if not session:
|
||||
return True, None, None
|
||||
|
||||
profile_id = int(session["profile_id"])
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
account_state = resolve_account_state_for_token(cur, session)
|
||||
|
||||
allowed, reason = check_api_onboarding_gate(
|
||||
path=p,
|
||||
method=method,
|
||||
profile_id=profile_id,
|
||||
account_state=account_state,
|
||||
)
|
||||
return allowed, reason, account_state
|
||||
@@ -87,6 +87,34 @@ app.add_middleware(
|
||||
)
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def account_onboarding_api_gate(request: Request, call_next):
|
||||
"""
|
||||
Phase A: Domänen-APIs für unverified / verified_pending_club sperren.
|
||||
Siehe account_onboarding_gate.py und MEMBERSHIP_RBAC_DECISIONS_2026-06.md §1.1
|
||||
"""
|
||||
from account_onboarding_gate import evaluate_request_gate
|
||||
|
||||
token = request.headers.get("x-auth-token") or request.headers.get("X-Auth-Token")
|
||||
allowed, reason, _state = evaluate_request_gate(
|
||||
token,
|
||||
request.url.path,
|
||||
request.method,
|
||||
)
|
||||
if not allowed:
|
||||
return JSONResponse(
|
||||
status_code=403,
|
||||
content={
|
||||
"detail": (
|
||||
"Zugriff erst nach E-Mail-Bestätigung und Vereinsmitgliedschaft möglich. "
|
||||
"Du kannst einen Beitrittsantrag stellen oder dein Konto in den Einstellungen verwalten."
|
||||
),
|
||||
"reason": reason,
|
||||
},
|
||||
)
|
||||
return await call_next(request)
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def add_api_security_headers(request: Request, call_next):
|
||||
"""Konsistente Basis-Header auch für rein JSON-Responses (MIME-Sniffing)."""
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
"""Tests für account_onboarding_gate."""
|
||||
import pytest
|
||||
|
||||
from account_onboarding_gate import (
|
||||
check_api_onboarding_gate,
|
||||
is_public_api_path,
|
||||
normalize_api_path,
|
||||
)
|
||||
|
||||
|
||||
def test_public_directory_is_public():
|
||||
assert is_public_api_path("/api/clubs/public-directory")
|
||||
|
||||
|
||||
def test_exercises_blocked_for_pending():
|
||||
allowed, reason = check_api_onboarding_gate(
|
||||
path="/api/exercises",
|
||||
method="GET",
|
||||
profile_id=1,
|
||||
account_state="verified_pending_club",
|
||||
)
|
||||
assert not allowed
|
||||
assert reason == "account_state_verified_pending_club"
|
||||
|
||||
|
||||
def test_join_request_allowed_for_pending():
|
||||
allowed, _ = check_api_onboarding_gate(
|
||||
path="/api/me/club-join-requests",
|
||||
method="POST",
|
||||
profile_id=1,
|
||||
account_state="verified_pending_club",
|
||||
)
|
||||
assert allowed
|
||||
|
||||
|
||||
def test_active_member_domain_ok():
|
||||
allowed, reason = check_api_onboarding_gate(
|
||||
path="/api/exercises",
|
||||
method="GET",
|
||||
profile_id=1,
|
||||
account_state="active_member",
|
||||
)
|
||||
assert allowed
|
||||
assert reason is None
|
||||
|
||||
|
||||
def test_profile_self_update_allowed_unverified():
|
||||
allowed, _ = check_api_onboarding_gate(
|
||||
path="/api/profiles/42",
|
||||
method="PUT",
|
||||
profile_id=42,
|
||||
account_state="unverified",
|
||||
)
|
||||
assert allowed
|
||||
|
||||
|
||||
def test_normalize_trailing_slash():
|
||||
assert normalize_api_path("/api/exercises/") == "/api/exercises"
|
||||
+1
-1
@@ -10,7 +10,7 @@ MODULE_VERSIONS = {
|
||||
"profiles": "1.8.1", # GET /profiles/me: account_state + club_roles
|
||||
"tenant_context": "1.1.0", # M3: account_state + email_verified im TenantContext
|
||||
"capabilities": "1.0.1", # resolve_capabilities_map für /me/entitlements
|
||||
"account_lifecycle": "1.0.0", # resolve_account_state + assert_min_account_state (ACCOUNT_GATE_ENFORCE)
|
||||
"account_lifecycle": "1.1.0", # Phase A: account_onboarding_gate API-Middleware
|
||||
"clubs": "0.4.1", # Alle geschützten Endpoints Depends(get_tenant_context); profile_id/role aus TenantContext
|
||||
"club_memberships": "1.0.1", # Depends(get_tenant_context)
|
||||
"club_join_requests": "1.0.1", # Depends(get_tenant_context)
|
||||
|
||||
Reference in new issue
Block a user