aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/src/meshbay_hub/api/nodes.py
diff options
context:
space:
mode:
authorChristophe Besson <cbesson@gmail.com>2026-08-13 03:56:30 +0200
committerChristophe Besson <cbesson@gmail.com>2026-08-13 03:56:30 +0200
commitf0248975908ad670fa8a820f865bf22ea8d0172d (patch)
treef4af64d36cacaccb4f6d13436e001aeb57e861e3 /packages/meshbay-hub/src/meshbay_hub/api/nodes.py
parent35130e5528a52161630fd1c93572e1b2b7cd911b (diff)
downloadmeshbay-f0248975908ad670fa8a820f865bf22ea8d0172d.tar.gz
feat: Phase 12 — P2P crypto material, password split, node Ed25519 auth
Baseline commit capturing in-progress Phase 12 work that was already present in the working tree (uncommitted) before the Phase 11.5 security remediation begins. Committed as-is, without review or modification, so that remediation changes arrive as a separable diff. Contents: BundleStore (P2P GEK + keypair bundles), password split (auth_key / bundle_key), node Ed25519 auth (POST /v1/nodes/auth, node-scoped JWT), GEK-HMAC handshake proof with DTLS channel binding, Ed25519 admin challenge-response, node local admin UI rewrite, browser key persistence. Not authored in this session — captured to establish a baseline. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Diffstat (limited to 'packages/meshbay-hub/src/meshbay_hub/api/nodes.py')
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/api/nodes.py71
1 files changed, 70 insertions, 1 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/api/nodes.py b/packages/meshbay-hub/src/meshbay_hub/api/nodes.py
index b970aa8..321e43c 100644
--- a/packages/meshbay-hub/src/meshbay_hub/api/nodes.py
+++ b/packages/meshbay-hub/src/meshbay_hub/api/nodes.py
@@ -1,15 +1,84 @@
"""Node endpoints — /v1/nodes/*"""
+import base64
+import time
+
+from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
+from cryptography.exceptions import InvalidSignature
from fastapi import APIRouter, Depends, HTTPException, Request
from pydantic import BaseModel
+from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
+from meshbay_hub.auth import issue_access_token
from meshbay_hub.api.deps import get_current_user
+from meshbay_hub.api.middleware import limiter
from meshbay_hub.db.engine import get_db
-from meshbay_hub.db.models import IPLog, Node, User
+from meshbay_hub.db.models import GroupMember, IPLog, Node, User
router = APIRouter(prefix="/v1/nodes", tags=["nodes"])
+NODE_AUTH_TIMESTAMP_WINDOW = 60 # seconds
+
+
+class NodeAuthRequest(BaseModel):
+ username: str
+ timestamp: int # unix epoch seconds
+ signature: str # base64 Ed25519 signature
+
+
+@router.post("/auth")
+@limiter.limit("10/minute")
+async def node_auth(
+ body: NodeAuthRequest,
+ request: Request,
+ db: AsyncSession = Depends(get_db),
+):
+ """Authenticate a node daemon via Ed25519 challenge-response. Returns node-scoped JWT."""
+ now = int(time.time())
+ if abs(now - body.timestamp) > NODE_AUTH_TIMESTAMP_WINDOW:
+ raise HTTPException(status_code=401, detail="Timestamp too old or too far in the future")
+
+ result = await db.execute(select(User).where(User.username == body.username))
+ user = result.scalar_one_or_none()
+ if not user:
+ raise HTTPException(status_code=401, detail="Invalid credentials")
+ if user.status != "active":
+ raise HTTPException(status_code=403, detail=f"Account {user.status}")
+
+ if not user.pk_node_ed25519:
+ raise HTTPException(
+ status_code=401,
+ detail="No node key registered — link your node from the browser first",
+ )
+
+ message = f"meshbay:node_auth:{body.username}:{body.timestamp}".encode()
+ try:
+ pk_raw = base64.b64decode(user.pk_node_ed25519)
+ pk = Ed25519PublicKey.from_public_bytes(pk_raw)
+ sig = base64.b64decode(body.signature)
+ pk.verify(sig, message)
+ except (InvalidSignature, Exception):
+ db.add(IPLog(event="node_auth_fail", ip_address=_ip(request), detail=body.username))
+ await db.commit()
+ raise HTTPException(status_code=401, detail="Invalid signature")
+
+ memberships = await db.execute(
+ select(GroupMember.group_id).where(GroupMember.user_id == user.id))
+ group_ids = [gid for (gid,) in memberships.all()]
+
+ access_token = issue_access_token(
+ user.id, user.pk_node_ed25519, ttl=3600, groups=group_ids, scope="node")
+
+ db.add(IPLog(user_id=user.id, event="node_auth", ip_address=_ip(request)))
+ await db.commit()
+
+ return {
+ "access_token": access_token,
+ "token_type": "bearer",
+ "expires_in": 3600,
+ }
+
class NodeAnnounceRequest(BaseModel):
pk_node: str