CLI for SidePulse
Python
Use compact Base64URL pairing channels
Peter Kuharefc8ce8e1d5d
diff --git a/src/sidepulse/cli.py b/src/sidepulse/cli.pyindex 4f963ef..6e8c4e4 100644--- a/src/sidepulse/cli.py+++ b/src/sidepulse/cli.py@@ -48,6 +48,7 @@ from .links import (bridge_server,listen_for_ios_registration,load_ios_links,+ new_pairing_channel,normalize_apns_token,pairing_url,render_terminal_qr,@@ -368,7 +369,7 @@ def cmd_sidepulse_write_ios(args: argparse.Namespace) -> int:def cmd_sidepulse_link(_args: argparse.Namespace) -> int:try:server = bridge_server()- channel = str(uuid.uuid4())+ channel = new_pairing_channel()url = pairing_url(server, channel)qr = render_terminal_qr(url)except LinkError as exc:diff --git a/src/sidepulse/links.py b/src/sidepulse/links.pyindex 881bdee..e5abff9 100644--- a/src/sidepulse/links.py+++ b/src/sidepulse/links.py@@ -4,6 +4,7 @@ import jsonimport osimport queueimport re+import secretsimport socketimport threadingimport time@@ -23,6 +24,7 @@ DEFAULT_BRIDGE_SERVER = "https://bridge.sidepulse.io"PAIRING_TIMEOUT_SECONDS = 5 * 60IOS_BUNDLE_ID = "io.sidepulse.ios"APNS_TOKEN_PATTERN = re.compile(r"^[0-9a-fA-F]{64}$")+PAIRING_CHANNEL_PATTERN = re.compile(r"^[A-Za-z0-9_-]{11}$")class LinkError(RuntimeError):@@ -143,16 +145,20 @@ def store_ios_link(link: IOSLink, path: Path | None = None) -> tuple[IOSLink, ..return tuple(updated)+def new_pairing_channel() -> str:+ return secrets.token_urlsafe(8)++def pairing_url(server: str, channel: str, sender: str | None = None) -> str:normalized_server = normalize_server(server)- try:- normalized_channel = str(uuid.UUID(channel))- except ValueError as exc:- raise LinkError("Pairing channel must be a UUID.") from exc+ if not PAIRING_CHANNEL_PATTERN.fullmatch(channel):+ raise LinkError("Pairing channel must be an 11-character Base64URL token.")+ if normalized_server == DEFAULT_BRIDGE_SERVER:+ return f"sidepulse://p/{channel}"query = {"v": "1","server": normalized_server,- "channel": normalized_channel,+ "channel": channel,}clean_sender = (sender or socket.gethostname()).strip()[:80]if clean_sender:diff --git a/tests/test_links.py b/tests/test_links.pyindex f52053a..21d81c2 100644--- a/tests/test_links.py+++ b/tests/test_links.py@@ -21,6 +21,7 @@ from sidepulse.links import ( # noqa: E402LinkError,iter_sse_messages,load_ios_links,+ new_pairing_channel,normalize_apns_token,pairing_url,parse_ios_registration,@@ -58,25 +59,36 @@ class LinkStorageTests(unittest.TestCase):self.assertEqual(len(stored), 1)self.assertEqual(stored[0].name, "New name")- def test_pairing_url_contains_a_uuid_channel_and_no_nonce(self) -> None:- channel = "6f1c2a9e-8f4b-4c1d-9b3a-2e5d7c8f0a11"+ def test_pairing_channel_is_an_eleven_character_base64url_secret(self) -> None:+ channels = {new_pairing_channel() for _ in range(100)}+ self.assertEqual(len(channels), 100)+ self.assertTrue(all(len(channel) == 11 for channel in channels))+ alphabet = set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")+ self.assertTrue(all(set(channel) <= alphabet for channel in channels))++ def test_default_pairing_url_contains_only_the_compact_channel(self) -> None:+ channel = "7kP2_xQ9mLs"url = pairing_url("https://bridge.sidepulse.io/", channel, "Studio Mac")+ self.assertEqual(url, "sidepulse://p/7kP2_xQ9mLs")++ def test_custom_server_pairing_url_keeps_connection_details(self) -> None:+ url = pairing_url("https://bridge.example.com", "7kP2_xQ9mLs", "Studio Mac")self.assertIn("sidepulse://pair?", url)- self.assertIn("channel=6f1c2a9e-8f4b-4c1d-9b3a-2e5d7c8f0a11", url)+ self.assertIn("server=https%3A%2F%2Fbridge.example.com", url)+ self.assertIn("channel=7kP2_xQ9mLs", url)self.assertIn("sender=Studio+Mac", url)- self.assertNotIn("nonce", url)def test_pairing_qr_renders_without_printing_the_raw_link(self) -> None:url = pairing_url("https://bridge.sidepulse.io",- "6f1c2a9e-8f4b-4c1d-9b3a-2e5d7c8f0a11",+ "7kP2_xQ9mLs","Studio Mac",)rendered = render_terminal_qr(url)lines = rendered.splitlines()self.assertTrue(any(character in rendered for character in "▀▄█"))- self.assertLessEqual(len(lines), 30)- self.assertLessEqual(max(map(len, lines)), 60)+ self.assertLessEqual(len(lines), 20)+ self.assertLessEqual(max(map(len, lines)), 40)self.assertNotIn("sidepulse://", rendered)def test_sse_parser_returns_complete_data_events(self) -> None: