aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/src/meshbay_hub
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-hub/src/meshbay_hub')
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/api/users.py44
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/auth.py22
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/c3d4e5f6a7b9_bundle_pepper.py28
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/db/models.py8
4 files changed, 102 insertions, 0 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/api/users.py b/packages/meshbay-hub/src/meshbay_hub/api/users.py
index 660bd76..72ac68f 100644
--- a/packages/meshbay-hub/src/meshbay_hub/api/users.py
+++ b/packages/meshbay-hub/src/meshbay_hub/api/users.py
@@ -27,7 +27,9 @@ from meshbay_hub.auth import (
hash_password_off_loop,
hash_refresh_token,
issue_access_token,
+ open_pepper,
pw_needs_rehash,
+ seal_pepper,
verify_password_off_loop,
)
from meshbay_hub.config import HubConfig
@@ -362,6 +364,39 @@ async def _login_failed(db: AsyncSession, username: str, ip: str,
raise HTTPException(status_code=401, detail="Invalid credentials")
+# ── Bundle pepper ────────────────────────────────────────────────────────────
+#
+# Half of what opens this account's keypair bundles on nodes; the passphrase is
+# the other half. Handed out only where the caller proved the passphrase or a
+# device key — sign-in, device sign-in, a passphrase change — or already holds a
+# session that did (`GET /me/bundle-pepper`). Never on a token refresh, which
+# proves only possession of a refresh token; never to a node token; never in a
+# token or a log line.
+
+def _bundle_pepper(user: User) -> dict:
+ """The pepper for a response, created on first use. The caller commits."""
+ if user.bundle_pepper is None:
+ user.bundle_pepper = seal_pepper(secrets.token_bytes(32), user.id)
+ user.bundle_pepper_version = user.bundle_pepper_version or 1
+ raw = open_pepper(user.bundle_pepper, user.id)
+ return {"bundle_pepper": base64.b64encode(raw).decode(),
+ "bundle_pepper_version": user.bundle_pepper_version}
+
+
+@router.get("/me/bundle-pepper")
+@limiter.limit("10/minute")
+async def get_bundle_pepper(
+ request: Request,
+ current_user: User = Depends(require_user_scope),
+ db: AsyncSession = Depends(get_db),
+):
+ """For a session opened before the pepper existed: asked once, then kept
+ only as part of the key derived from it."""
+ pepper = _bundle_pepper(current_user)
+ await db.commit()
+ return pepper
+
+
@router.post("/login")
@limiter.limit("10/minute")
async def login(
@@ -445,6 +480,7 @@ async def login(
family_id=family_id, expires_at=expires_at,
))
db.add(IPLog(user_id=user.id, event="login", ip_address=ip))
+ pepper = _bundle_pepper(user)
await db.commit()
return {
@@ -452,6 +488,7 @@ async def login(
"refresh_token": raw_rt,
"token_type": "bearer",
"expires_in": _ttl(),
+ **pepper,
}
@@ -626,6 +663,7 @@ async def device_auth(
family_id=str(uuid.uuid4()), expires_at=expires_at))
db.add(IPLog(user_id=user.id, event="device_auth",
ip_address=client_ip(request)))
+ pepper = _bundle_pepper(user)
await db.commit()
return {
@@ -634,6 +672,7 @@ async def device_auth(
"token_type": "bearer",
"expires_in": _ttl(),
"device_id": matched.id,
+ **pepper,
}
@@ -1045,6 +1084,9 @@ async def change_password(
))
db.add(IPLog(user_id=current_user.id, event="password_change",
ip_address=client_ip(request)))
+ # The new passphrase makes a new bundle key, and the client does not keep
+ # the pepper; this call proved the old passphrase, so it carries it.
+ pepper = _bundle_pepper(current_user)
await db.commit()
return {
@@ -1053,6 +1095,7 @@ async def change_password(
"refresh_token": raw_rt,
"token_type": "bearer",
"expires_in": _ttl(),
+ **pepper,
}
@@ -1442,6 +1485,7 @@ async def erase_account(db: AsyncSession, user: User, owned_groups: str = "refus
user.pw_hash = b""
user.pw_salt = b""
user.pk_node_ed25519 = None
+ user.bundle_pepper = None
user.status = "deleted"
user.role = "user"
await db.commit()
diff --git a/packages/meshbay-hub/src/meshbay_hub/auth.py b/packages/meshbay-hub/src/meshbay_hub/auth.py
index 0d0fd95..1d09581 100644
--- a/packages/meshbay-hub/src/meshbay_hub/auth.py
+++ b/packages/meshbay-hub/src/meshbay_hub/auth.py
@@ -324,6 +324,28 @@ def decrypt_email(stored: str) -> str:
return AESGCM(_email_key).decrypt(nonce, ct, None).decode()
+def seal_pepper(raw: bytes, user_id: str) -> str:
+ """Seal a bundle pepper for storage, bound to its account.
+
+ The same at-rest key as the e-mail, with the account id and a purpose as
+ associated data: a sealed value copied into another row, or into the e-mail
+ column, does not open.
+ """
+ if _email_key is None:
+ raise RuntimeError("Hub keypair not loaded")
+ nonce = os.urandom(12)
+ aad = f"meshbay:bundle_pepper:{user_id}".encode()
+ return base64.b64encode(nonce + AESGCM(_email_key).encrypt(nonce, raw, aad)).decode()
+
+
+def open_pepper(stored: str, user_id: str) -> bytes:
+ if _email_key is None:
+ raise RuntimeError("Hub keypair not loaded")
+ raw = base64.b64decode(stored)
+ aad = f"meshbay:bundle_pepper:{user_id}".encode()
+ return AESGCM(_email_key).decrypt(raw[:12], raw[12:], aad)
+
+
def hash_email_blind(email: str) -> str:
"""Deterministic HMAC-SHA256 of the lowercased email for uniqueness checks.
diff --git a/packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/c3d4e5f6a7b9_bundle_pepper.py b/packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/c3d4e5f6a7b9_bundle_pepper.py
new file mode 100644
index 0000000..dbf1718
--- /dev/null
+++ b/packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/c3d4e5f6a7b9_bundle_pepper.py
@@ -0,0 +1,28 @@
+"""a bundle pepper per account, so a node cannot test passphrase guesses
+
+Revision ID: c3d4e5f6a7b9
+Revises: b2c3d4e5f6a8
+"""
+
+from collections.abc import Sequence
+
+import sqlalchemy as sa
+from alembic import op
+
+revision: str = "c3d4e5f6a7b9"
+down_revision: str | Sequence[str] | None = "b2c3d4e5f6a8"
+branch_labels: str | Sequence[str] | None = None
+depends_on: str | Sequence[str] | None = None
+
+
+def upgrade() -> None:
+ with op.batch_alter_table("users") as batch:
+ batch.add_column(sa.Column("bundle_pepper", sa.String(128), nullable=True))
+ batch.add_column(sa.Column("bundle_pepper_version", sa.Integer(), nullable=False,
+ server_default="1"))
+
+
+def downgrade() -> None:
+ with op.batch_alter_table("users") as batch:
+ batch.drop_column("bundle_pepper_version")
+ batch.drop_column("bundle_pepper")
diff --git a/packages/meshbay-hub/src/meshbay_hub/db/models.py b/packages/meshbay-hub/src/meshbay_hub/db/models.py
index 293b940..1e652a6 100644
--- a/packages/meshbay-hub/src/meshbay_hub/db/models.py
+++ b/packages/meshbay-hub/src/meshbay_hub/db/models.py
@@ -60,6 +60,14 @@ class User(Base):
# active|suspended|revoked
status: Mapped[str] = mapped_column(String(16), default="active")
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now)
+ # The second half of what opens this account's keypair bundles on nodes
+ # (`auth.seal_pepper`, sealed at rest). A bundle is sealed under a key derived
+ # from the passphrase *and* this, so an operator holding one cannot test
+ # passphrase guesses offline: the pepper is handed only to a session that
+ # proved the passphrase or a device key, never to a node. Created the first
+ # time it is asked for; the version names which pepper sealed a bundle.
+ bundle_pepper: Mapped[str | None] = mapped_column(String(128), nullable=True)
+ bundle_pepper_version: Mapped[int] = mapped_column(Integer, default=1, server_default="1")
nodes: Mapped[list["Node"]] = relationship(back_populates="user")
group_memberships: Mapped[list["GroupMember"]] = relationship(back_populates="user")