Federation data and actor keys (phase 1, step 1)
- federation_settings (per user; off, followers-only, not discoverable by default) and federation_keys (RSA-2048; NULL user = instance actor), on the federation migration branch - Private keys encrypted at rest with Fernet, keyed by HKDF-SHA256 from FEDERATION_KEY_SECRET (now required, 32+ chars, never printed) - Keys are created when a user first enables sharing and never regenerated; sharing.shared_user() treats unknown and non-sharing users identically - One flask-smorest Api per app (get_api(app)), so apps with and without federation never share an OpenAPI spec - Test fixtures for a second, federation-enabled app on the same DB Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
This commit is contained in:
1 parent
00ad8e9e6d
commit
98c65547f3
12 files changed
+513
-5
No files matched your search
@@ -0,0 +1,69 @@
|
||||
"""Per-user sharing settings: whether a user federates, and how."""
|
||||
|
||||
from scrobbler.extensions import db
|
||||
from scrobbler.federation import keys
|
||||
from scrobbler.federation.models import VISIBILITIES, FederationSettings
|
||||
from scrobbler.services import accounts
|
||||
|
||||
EDITABLE = (
|
||||
"enabled",
|
||||
"visibility",
|
||||
"manually_approves_followers",
|
||||
"discoverable",
|
||||
"indexable",
|
||||
"display_name",
|
||||
"bio",
|
||||
)
|
||||
|
||||
|
||||
def settings_for(user_id: int) -> FederationSettings:
|
||||
"""The user's settings; unsaved defaults if they've never changed them."""
|
||||
settings = db.session.get(FederationSettings, user_id)
|
||||
if settings is None:
|
||||
settings = FederationSettings(
|
||||
user_id=user_id,
|
||||
enabled=False,
|
||||
visibility="followers",
|
||||
manually_approves_followers=False,
|
||||
discoverable=False,
|
||||
indexable=False,
|
||||
)
|
||||
return settings
|
||||
|
||||
|
||||
def update(user_id: int, changes: dict) -> tuple[FederationSettings, bool | None]:
|
||||
"""Apply changes. Returns the settings and True/False if sharing was switched
|
||||
on/off by this update (None if unchanged)."""
|
||||
unknown = set(changes) - set(EDITABLE)
|
||||
if unknown:
|
||||
raise ValueError(f"not editable: {sorted(unknown)}")
|
||||
if changes.get("visibility", "followers") not in VISIBILITIES:
|
||||
raise ValueError("visibility must be one of " + ", ".join(VISIBILITIES))
|
||||
settings = settings_for(user_id)
|
||||
was_enabled = bool(settings.enabled)
|
||||
for field, value in changes.items():
|
||||
setattr(settings, field, value)
|
||||
db.session.add(settings)
|
||||
db.session.commit()
|
||||
if settings.enabled and not was_enabled:
|
||||
keys.key_for(user_id) # create the key pair before anyone can fetch the actor
|
||||
toggled = None if settings.enabled == was_enabled else bool(settings.enabled)
|
||||
return settings, toggled
|
||||
|
||||
|
||||
def shared_user(username: str):
|
||||
"""(user, settings) for a user who has switched sharing on, else None. Callers must
|
||||
treat 'unknown user' and 'not sharing' identically, so accounts can't be probed."""
|
||||
user = accounts.find_user(username)
|
||||
if user is None:
|
||||
return None
|
||||
settings = db.session.get(FederationSettings, user.id)
|
||||
if settings is None or not settings.enabled:
|
||||
return None
|
||||
return user, settings
|
||||
|
||||
|
||||
def sharing_count() -> int:
|
||||
return db.session.scalar(
|
||||
db.select(db.func.count()).select_from(FederationSettings).filter_by(enabled=True)
|
||||
)
|
||||
Reference in new issue
Block a user