1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
"""Group endpoints — /v1/groups/*"""
from fastapi import APIRouter, Depends, HTTPException, Request
from pydantic import BaseModel
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from meshbay_hub.api.deps import get_current_user
from meshbay_hub.db.engine import get_db
from meshbay_hub.db.models import GEKBundle, Group, GroupMember, IPLog, User
router = APIRouter(prefix="/v1/groups", tags=["groups"])
@router.get("")
async def list_public_groups(
db: AsyncSession = Depends(get_db),
limit: int = 50,
offset: int = 0,
):
"""List public groups — browsable without authentication."""
result = await db.execute(
select(Group)
.where(Group.visibility == "public", Group.status == "active")
.order_by(Group.created_at.desc())
.limit(limit)
.offset(offset)
)
groups = result.scalars().all()
return {
"groups": [
{
"id": g.id,
"name": g.name,
"join_policy": g.join_policy,
"created_at": g.created_at.isoformat(),
}
for g in groups
],
"total": len(groups),
}
class GroupCreateRequest(BaseModel):
name: str
visibility: str = "private" # public|private
join_policy: str = "invite" # open|request|invite
class GEKBundleRequest(BaseModel):
pk_eph_b64: str
nonce_b64: str
wrapped_b64: str
@router.post("", status_code=201)
async def create_group(
body: GroupCreateRequest,
request: Request,
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
group = Group(
name=body.name,
admin_id=current_user.id,
visibility=body.visibility,
join_policy=body.join_policy,
)
db.add(group)
await db.flush() # get group.id
db.add(GroupMember(group_id=group.id, user_id=current_user.id))
db.add(IPLog(user_id=current_user.id, event="group_create",
ip_address=_ip(request), detail=body.name))
await db.commit()
await db.refresh(group)
return {"group_id": group.id, "name": group.name}
@router.post("/{group_id}/members/{username}/gek", status_code=201)
async def store_gek_bundle(
group_id: str,
username: str,
body: GEKBundleRequest,
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
group = await db.get(Group, group_id)
if not group:
raise HTTPException(status_code=404, detail="Group not found")
if group.admin_id != current_user.id:
raise HTTPException(status_code=403, detail="Only admin can add members")
result = await db.execute(select(User).where(User.username == username))
target = result.scalar_one_or_none()
if not target:
raise HTTPException(status_code=404, detail="User not found")
# Upsert GEK bundle
existing = await db.get(GEKBundle, (group_id, target.id))
if existing:
existing.pk_eph_b64 = body.pk_eph_b64
existing.nonce_b64 = body.nonce_b64
existing.wrapped_b64 = body.wrapped_b64
else:
db.add(GEKBundle(
group_id=group_id,
user_id=target.id,
pk_eph_b64=body.pk_eph_b64,
nonce_b64=body.nonce_b64,
wrapped_b64=body.wrapped_b64,
))
# Add member if not already in group
mem = await db.get(GroupMember, (group_id, target.id))
if not mem:
db.add(GroupMember(group_id=group_id, user_id=target.id))
await db.commit()
return {"status": "stored", "group_id": group_id, "username": username}
@router.get("/{group_id}/gek")
async def get_my_gek_bundle(
group_id: str,
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
group = await db.get(Group, group_id)
if not group:
raise HTTPException(status_code=404, detail="Group not found")
bundle = await db.get(GEKBundle, (group_id, current_user.id))
if not bundle:
raise HTTPException(status_code=404, detail="No GEK bundle for this user in this group")
return {
"group_id": group_id,
"pk_eph_b64": bundle.pk_eph_b64,
"nonce_b64": bundle.nonce_b64,
"wrapped_b64": bundle.wrapped_b64,
}
def _ip(request: Request) -> str:
fwd = request.headers.get("X-Forwarded-For")
return fwd.split(",")[0].strip() if fwd else (
request.client.host if request.client else "unknown")
|