HEL-879 verify endpoint grants and correct final window evidence
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -143,11 +143,46 @@ assert mine_claims["iss"] == sdk_claims["iss"] and mine_claims["sub"] == sdk_cla
|
||||
assert set(mine_claims) == set(sdk_claims), (sorted(mine_claims), sorted(sdk_claims))
|
||||
ok.append("mint_token claims byte-parity with livekit.api.AccessToken (video/iss/sub/keys)")
|
||||
|
||||
# 6. server token decodes as the SDK Service._auth_header shape
|
||||
server_tok = wire.mint_server_token("devkey", "s3cret", ttl_seconds=60, now=1700000000.0)
|
||||
sc = pyjwt.decode(server_tok, "s3cret", algorithms=["HS256"], options={"verify_exp": False})
|
||||
assert sc["iss"] == "devkey" and sc["sub"] == "" and "video" not in sc
|
||||
ok.append("mint_server_token matches SDK no-grant auth header claims")
|
||||
# 6. Exercise BOTH real wire helpers: endpoint-specific grants must match the SDK,
|
||||
# and Twirp JSON must round-trip (a generic grantless token was insufficient on
|
||||
# the actual server; HEL-879's first real window exposed that missing coverage).
|
||||
async def server_endpoint_parity():
|
||||
seen = []
|
||||
def handler(request):
|
||||
assert request.headers["content-type"] == "application/json"
|
||||
body = json.loads(request.content)
|
||||
mine = pyjwt.decode(request.headers["authorization"].removeprefix("Bearer "),
|
||||
"s3cret", algorithms=["HS256"], options={"verify_exp": False})
|
||||
if request.url.path.endswith("CreateDispatch"):
|
||||
assert body == {"agentName": "offline-agent", "room": "offline-room", "metadata": "offline"}
|
||||
grants = api.VideoGrants(room_admin=True, room="offline-room")
|
||||
response = httpx.Response(200, json={"id": "AD_offline", "room": "offline-room"})
|
||||
else:
|
||||
assert request.url.path.endswith("DeleteRoom") and body == {"room": "offline-room"}
|
||||
grants = api.VideoGrants(room_create=True)
|
||||
response = httpx.Response(200, content=b"")
|
||||
sdk = pyjwt.decode(api.AccessToken("devkey", "s3cret").with_grants(grants).to_jwt(),
|
||||
"s3cret", algorithms=["HS256"], options={"verify_exp": False})
|
||||
# The SDK includes its default media permissions even for a service
|
||||
# token. They do not authorize these endpoints; compare the required
|
||||
# service grants without adding unused media privileges to our token.
|
||||
media_defaults = {"canPublish", "canSubscribe", "canPublishData"}
|
||||
assert all(sdk["video"][k] is True for k in media_defaults)
|
||||
required = {k: v for k, v in sdk["video"].items() if k not in media_defaults}
|
||||
assert mine["video"] == required, (mine["video"], required)
|
||||
assert mine["iss"] == sdk["iss"] and mine["sub"] == sdk["sub"]
|
||||
seen.append(request.url.path.rsplit("/", 1)[-1])
|
||||
return response
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
|
||||
dispatch = await wire.create_dispatch(client, internal_url="http://offline.test",
|
||||
api_key="devkey", api_secret="s3cret", agent_name="offline-agent",
|
||||
room="offline-room", metadata="offline")
|
||||
assert dispatch.dispatch_id == "AD_offline"
|
||||
assert await wire.delete_room(client, internal_url="http://offline.test",
|
||||
api_key="devkey", api_secret="s3cret", room="offline-room")
|
||||
assert seen == ["CreateDispatch", "DeleteRoom"]
|
||||
asyncio.run(server_endpoint_parity())
|
||||
ok.append("actual CreateDispatch/DeleteRoom helpers: Twirp JSON and required SDK endpoint authorization grants match")
|
||||
|
||||
# 7. SpeechHandle/worker API surface used by worker.py exists on the real SDK
|
||||
from livekit.agents.voice.speech_handle import SpeechHandle
|
||||
|
||||
@@ -12,6 +12,7 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import contextlib
|
||||
import json
|
||||
import socket
|
||||
import sys
|
||||
import tempfile
|
||||
@@ -48,11 +49,18 @@ class FakeLiveKit:
|
||||
if self.fail:
|
||||
return httpx.Response(500)
|
||||
if request.url.path.endswith("CreateDispatch"):
|
||||
agent, room = _dispatch_fields(request.content)
|
||||
body = json.loads(request.content)
|
||||
agent, room = body["agentName"], body["room"]
|
||||
assert request.headers["content-type"] == "application/json"
|
||||
claims, _ = livekit_wire.decode_token(request.headers["authorization"].removeprefix("Bearer "))
|
||||
assert claims["video"] == {"roomAdmin": True, "room": room}
|
||||
self.dispatches.append({"agent_name": agent, "room": room})
|
||||
return httpx.Response(200, json={"id": f"AD_{len(self.dispatches)}", "room": room})
|
||||
if request.url.path.endswith("DeleteRoom"):
|
||||
self.deleted.append(_dispatch_fields(request.content)[0])
|
||||
assert request.headers["content-type"] == "application/json"
|
||||
claims, _ = livekit_wire.decode_token(request.headers["authorization"].removeprefix("Bearer "))
|
||||
assert claims["video"] == {"roomCreate": True}
|
||||
self.deleted.append(json.loads(request.content)["room"])
|
||||
return httpx.Response(200)
|
||||
return httpx.Response(404)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user