Pair a wall panel through the portal
A screen somewhere this installation is not reachable from asks the portal for a code instead, and the portal mints its credential — because a token signed here is one such a device could never present. Where it was minted changes nothing about what it may do. The panel gate moved off the branch that decodes a local panel token and onto whatever claims name a panel, so the portal's and this installation's are bounded by the same check against the same panel's dashboards. A token of that scope naming no panel is refused rather than left holding the account it borrows. The connector marks what arrives on its socket, since that is the only thing that makes it true, and the approval screen now names what is holding a code — approving adopts whatever answers, so it is worth a look first. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017F9RnYCJgASuBTcAjxmnsp
This commit is contained in:
@@ -203,3 +203,106 @@ def test_removing_the_panel_revokes_its_credential(
|
||||
assert (
|
||||
client.get(f"{DASHBOARDS}/panel_gone", headers=panel_headers).status_code == 401
|
||||
)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# A screen that reached the portal but not this installation
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_the_device_asking_is_named_before_anyone_approves(
|
||||
client: TestClient, superuser_token_headers: dict[str, str]
|
||||
) -> None:
|
||||
"""Approving a code adopts whatever holds it, so it is worth a look."""
|
||||
started = client.post(
|
||||
f"{PREFIX}/pair",
|
||||
headers={
|
||||
"user-agent": "Mozilla/5.0 (X11; CrOS aarch64)",
|
||||
"x-forwarded-for": "203.0.113.7, 10.0.0.1",
|
||||
},
|
||||
).json()
|
||||
|
||||
looked = client.get(
|
||||
f"{PREFIX}/pair/{started['code']}/device", headers=superuser_token_headers
|
||||
)
|
||||
assert looked.status_code == 200
|
||||
assert "CrOS" in looked.json()["device"]
|
||||
# The first hop, not the proxy that relayed it.
|
||||
assert "203.0.113.7" in looked.json()["device"]
|
||||
assert looked.json()["remote"] is False
|
||||
|
||||
# Nobody without an account gets to enumerate what is waiting.
|
||||
assert client.get(f"{PREFIX}/pair/{started['code']}/device").status_code == 401
|
||||
assert (
|
||||
client.get(
|
||||
f"{PREFIX}/pair/ZZZZZZ/device", headers=superuser_token_headers
|
||||
).status_code
|
||||
== 404
|
||||
)
|
||||
|
||||
|
||||
def test_a_remote_device_is_paired_at_the_portal(
|
||||
client: TestClient,
|
||||
superuser_token_headers: dict[str, str],
|
||||
enrolled: object, # noqa: ARG001 (fixture installs the enrolment)
|
||||
monkeypatch, # type: ignore[no-untyped-def]
|
||||
) -> None:
|
||||
"""A device that arrived through the tunnel gets the portal's credential.
|
||||
|
||||
It could never present one this installation signed: the portal verifies
|
||||
what crosses it, and it verifies against its own key.
|
||||
"""
|
||||
import httpx
|
||||
|
||||
from app.api.routes import panels as panels_route
|
||||
|
||||
calls: list[dict[str, object]] = []
|
||||
|
||||
def fake_post(url: str, **kwargs: object) -> httpx.Response:
|
||||
calls.append({"url": url, **kwargs})
|
||||
return httpx.Response(
|
||||
200,
|
||||
json={"access_token": "minted-by-the-portal", "expires_in": 31536000},
|
||||
request=httpx.Request("POST", url),
|
||||
)
|
||||
|
||||
monkeypatch.setattr(panels_route.httpx, "post", fake_post)
|
||||
|
||||
_panels(client, superuser_token_headers, {"panels": [{"id": "hallway"}]})
|
||||
started = client.post(f"{PREFIX}/pair", headers={"x-fluksio-via": "portal"}).json()
|
||||
|
||||
looked = client.get(
|
||||
f"{PREFIX}/pair/{started['code']}/device", headers=superuser_token_headers
|
||||
).json()
|
||||
assert looked["remote"] is True
|
||||
|
||||
approved = client.post(
|
||||
f"{PREFIX}/hallway/pair",
|
||||
headers=superuser_token_headers,
|
||||
json={"code": started["code"]},
|
||||
)
|
||||
assert approved.status_code == 200, approved.text
|
||||
|
||||
assert calls[0]["url"].endswith("/api/v1/panel-tokens/") # type: ignore[union-attr]
|
||||
assert calls[0]["headers"]["Authorization"] == "Bearer installation-token" # type: ignore[index]
|
||||
assert calls[0]["json"] == {"panel": "hallway"} # type: ignore[index]
|
||||
|
||||
collected = client.get(
|
||||
f"{PREFIX}/pair/{started['code']}", params={"secret": started["secret"]}
|
||||
).json()
|
||||
assert collected["access_token"] == "minted-by-the-portal"
|
||||
|
||||
|
||||
def test_a_remote_device_needs_an_enrolment(
|
||||
client: TestClient, superuser_token_headers: dict[str, str]
|
||||
) -> None:
|
||||
"""Unenrolled, there is nowhere to ask — and no token to invent locally."""
|
||||
_panels(client, superuser_token_headers, {"panels": [{"id": "shed"}]})
|
||||
started = client.post(f"{PREFIX}/pair", headers={"x-fluksio-via": "portal"}).json()
|
||||
|
||||
approved = client.post(
|
||||
f"{PREFIX}/shed/pair",
|
||||
headers=superuser_token_headers,
|
||||
json={"code": started["code"]},
|
||||
)
|
||||
assert approved.status_code == 409
|
||||
|
||||
Reference in New Issue
Block a user