signal-bot/fake_signal/routes_account.py
Krzysztof kuhy Rudnicki 7984c65f8a feat: register the bot as its own Signal account, no phone app needed
Shelter cannot work on this phone, and it would not have helped anyway.

It cannot work: setting a Device Owner makes Android enable
no_add_managed_profile, no_add_private_profile and no_add_clone_profile on
every user, which kills Shelter, Island, Private Space and app cloning
alike. Verified on-device rather than inferred -- `pm create-user
--profileOf 0 --managed` returns "Cannot add user: no_add_managed_profile is
enabled". This is the Android 11 COMP deprecation, enforced in three places
in DevicePolicyManagerService and unchanged through Android 16.

It would not have helped: a second Signal *app* is not a second Signal
*account*. Every account needs its own phone number, so the second number
was always the real requirement -- and once you have one, signal-cli can
register it as a PRIMARY device with no phone app, no QR scan and no work
profile at all. That is strictly better than both the linked-device setup
and the Shelter plan: the bot cannot read personal DMs and never posts as
the account owner.

scripts/register_bot.sh drives that flow. It gates on signal-cli >= 0.14.7
rather than hoping: stable container tag 0.100 bundles 0.14.5, and on
2026-03-06 Signal made the post-quantum ratchet mandatory and de-registered
every account whose client lacked it -- a stale client is how a bot dies
silently months later. Verified that the recommended CI tag really does
bundle 0.14.7 instead of assuming it.

Rehearsed offline, not written blind. fake_signal grew /v1/accounts,
/v1/register/{number}, /v1/register/{number}/verify/{token} and
/v1/groups/{number}, so the whole runbook including every error path
(missing captcha, stray verify, wrong code, unregistered number, empty group
list, out-of-range and non-numeric picks) runs against the fake. The picker
was proven to resolve the group the user actually pointed at.

Prompts now read from $SIGNAL_TTY instead of /dev/tty directly. A hard-coded
/dev/tty made the interactive path untestable without a real terminal; the
seam is the same idea as the injected clock and transport on the Python side,
and is what let the picker be rehearsed at all.

live_smoke.sh and register_bot.sh share scripts/lib_signal.sh so the two
cannot drift, and shellcheck now runs with -x so the sourced lib is actually
linted from its callers.

201 tests, 100% branch coverage, all 23 gates green.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013yaBT45KhHqxTKVCTA87iy
2026-08-29 13:41:13 +02:00

81 lines
2.9 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""Registration and group endpoints.
Mirrors the shapes scripts/register_bot.sh depends on, so the whole runbook
can be rehearsed offline. The real captcha and SMS steps are the only parts
that cannot be faked, and both are represented by their exact failure mode.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Any
from fastapi import APIRouter, Request
from fastapi.responses import JSONResponse
if TYPE_CHECKING:
from fake_signal.state import State
CAPTCHA_REQUIRED = (
"Captcha required for verification, use --captcha CAPTCHA\n"
"To get the token, go to https://signalcaptchas.org/registration/generate.html\n"
'After solving the captcha, right-click on the "Open Signal" link and copy the link.'
)
# Mirrors signalapp/registration-service in dev mode, which accepts the last
# six digits of the number as the verification code.
CODE_LENGTH = 6
def _state(request: Request) -> State:
"""Return the State attached to the running app."""
state: State = request.app.state.fake
return state
def expected_code(number: str) -> str:
"""The verification code the fake accepts for a number."""
digits = "".join(c for c in number if c.isdigit())
return digits[-CODE_LENGTH:]
def build_account_router() -> APIRouter:
"""Build the router covering accounts, registration and groups."""
router = APIRouter()
@router.get("/v1/accounts")
async def accounts(request: Request) -> list[str]:
return list(_state(request).accounts)
@router.post("/v1/register/{number}")
async def register(
request: Request, number: str, payload: dict[str, Any] | None = None
) -> JSONResponse:
captcha = (payload or {}).get("captcha")
if not captcha:
return JSONResponse({"error": CAPTCHA_REQUIRED}, status_code=400)
_state(request).pending[number] = expected_code(number)
return JSONResponse({}, status_code=201)
@router.post("/v1/register/{number}/verify/{token}")
async def verify(request: Request, number: str, token: str) -> JSONResponse:
state = _state(request)
if number not in state.pending:
return JSONResponse(
{"error": f"{number} has no registration in progress"}, status_code=400
)
if token != state.pending[number]:
return JSONResponse({"error": "invalid verification code"}, status_code=400)
del state.pending[number]
if number not in state.accounts:
state.accounts.append(number)
return JSONResponse({}, status_code=201)
@router.get("/v1/groups/{number}")
async def groups(request: Request, number: str) -> JSONResponse:
state = _state(request)
if number not in state.accounts:
return JSONResponse({"error": f"{number} is not registered"}, status_code=400)
return JSONResponse(state.groups, status_code=200)
return router