feat(backend): license entitlements, enforced limits, free tier

Platform v2 step 1.4 (APPS_PLATFORM D-3..D-6, D-21, §5).
- core/modules/entitlements.py: compute() turns the evaluated license into
  per-module grants: the v2 token's `modules` map (own `exp` with the 5-day
  grace, never cut mid-service) + the free tier; no/invalid token, expired
  after grace, inactive, locked or clock tampering -> free tier only; core
  always. limit(module, name) with token value -> manifest licensed_default ->
  free value; None = unlimited. LICENSE_MODE=off (dev/test only) = everything
- module gating says why: module.not_licensed (402, the first unlicensed
  module in the dependency chain) before module.disabled (403)
- services/license.py accepts only v2 tokens with a modules map; the v1
  "legacy unsigned 72h" path is gone
- apply_license() always installs entitlements (startup before workers,
  every heartbeat, workday close); workers follow license changes
- the global license middleware is deleted (no more whole-API 402/423);
  license_state moved to services/license_state.py
- history_days enforced in every report/history endpoint: lower date bounds
  clamped (also when no "from" is given), and business days older than the
  limit refused by id (license.history_limited); data is never deleted
- bootstrap/modules API show license mode, entitlement source and limit values
- LICENSE_MODE=off in docker-compose.dev.yml and the e2e launchers; sites
  default to enforced
- tests: entitlement rules, limits, the three done-when checks, signed-token
  apply_license path; license tests moved to v2 tokens

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-12 18:32:57 +02:00
co-authored by Claude Opus 5.5
parent 231a97c186
commit 0d43e6a111
21 changed files with 628 additions and 172 deletions
+12 -13
View File
@@ -1,4 +1,4 @@
"""Offline-capable licensing rules (services/license.py, KI-006).
"""Offline-capable licensing rules (services/license.py, KI-006; v2 tokens, D-21).
Tokens are signed with a throwaway key here — the production signing key is
never needed; verify_token() takes the public key as a parameter.
@@ -24,8 +24,9 @@ def b64(data: bytes) -> str:
def make_token(signer=KEY, **overrides) -> str:
payload = {"v": 1, "site_id": "site-A", "active": True, "locked": False, "lock_reason": None,
"expires_at": EXPIRES.isoformat(), "issued_at": ISSUED.isoformat(), **overrides}
payload = {"v": 2, "site_id": "site-A", "active": True, "locked": False, "lock_reason": None,
"expires_at": EXPIRES.isoformat(), "issued_at": ISSUED.isoformat(),
"modules": {"ordering": {}}, **overrides}
body = b64(json.dumps(payload, separators=(",", ":"), sort_keys=True).encode())
return f"{body}.{b64(signer.sign(body.encode()))}"
@@ -48,6 +49,14 @@ def test_edited_expiry_rejected(payload):
assert verify_token(f"{forged}.{sig}", "site-A", PUB) is None
def test_v1_token_rejected(): # D-21: v2 sites accept only v2 tokens
assert verify_token(make_token(v=1), "site-A", PUB) is None
def test_v2_token_without_modules_rejected():
assert verify_token(make_token(modules=None), "site-A", PUB) is None
def test_token_for_another_site_rejected():
assert verify_token(make_token(site_id="site-B"), "site-A", PUB) is None
@@ -115,16 +124,6 @@ def test_clock_before_signed_issue_time_blocks(payload):
# ── evaluate: no genuine token ───────────────────────────────────────────────
def test_transition_old_cloud_recent_ok():
r = evaluate(None, ISSUED, None, False, legacy_ok_at=ISSUED - timedelta(hours=10))
assert (r["licensed"], r["license_problem"]) == (True, None)
def test_transition_old_cloud_stale():
r = evaluate(None, ISSUED, None, False, legacy_ok_at=ISSUED - timedelta(hours=80))
assert (r["licensed"], r["license_problem"]) == (False, "unverified")
def test_never_licensed_blocks():
r = evaluate(None, ISSUED, None, False)
assert (r["licensed"], r["license_problem"]) == (False, "unverified")