Open the Link sign-in URL in the default browser

This commit is contained in:
Alex Schapiro 2026-09-01 20:46:49 +00:00
parent 16e1e56576
commit d3ea3e5892

View file

@ -86,6 +86,7 @@ _WALLET_ENV_NAMES = frozenset(
}
)
_AUTHORIZATION_SECRET = re.compile(r"(?i)((?:bearer|payment)\s+)[^\s\"']+")
_HTTPS_URL = re.compile(r"https://[^\s\"'<>]+")
_LOOPBACK_NO_PROXY = ("127.0.0.1", "localhost", "::1")
@ -637,21 +638,7 @@ def _prepare_link_wallet(console: Console, npx: str, *, as_json: bool) -> str |
"The user approves every payment in the Link app."
)
try:
_run_link_cli(
npx,
[
"auth",
"login",
"--client-name",
_LINK_CLI_CLIENT_NAME,
"--interval",
"3",
"--timeout",
str(_LINK_LOGIN_TIMEOUT_S),
],
capture_output=False,
timeout=_LINK_LOGIN_TIMEOUT_S + 30,
)
_run_link_login(npx)
except (OSError, subprocess.SubprocessError):
return manual_setup
if _link_wallet_authenticated(npx):
@ -659,6 +646,46 @@ def _prepare_link_wallet(console: Console, npx: str, *, as_json: bool) -> str |
return manual_setup
def _run_link_login(npx: str) -> None:
"""Start the Link sign-in, echo its output, and open the setup URL in a browser."""
arguments = [
"auth",
"login",
"--client-name",
_LINK_CLI_CLIENT_NAME,
"--interval",
"3",
"--timeout",
str(_LINK_LOGIN_TIMEOUT_S),
]
with tempfile.TemporaryDirectory(prefix="strix-wallet-") as wallet_cwd:
wallet_root = Path(wallet_cwd)
(wallet_root / "user.npmrc").touch(mode=0o600)
(wallet_root / "global.npmrc").touch(mode=0o600)
with subprocess.Popen( # noqa: S603
[*_npx_prefix(npx, wallet_root), _LINK_CLI_PACKAGE, *arguments],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
env=_wallet_environment(),
cwd=wallet_root,
) as process:
opened = False
if process.stdout is None:
process.wait(timeout=_LINK_LOGIN_TIMEOUT_S + 30)
return
for line in process.stdout:
sys.stdout.write(line)
sys.stdout.flush()
if not opened:
match = _HTTPS_URL.search(sanitize_terminal_text(line))
if match:
opened = True
with suppress(Exception):
webbrowser.open(match.group(0))
process.wait(timeout=_LINK_LOGIN_TIMEOUT_S + 30)
def _wallet_environment() -> dict[str, str]:
"""Pass only platform essentials and explicit wallet variables to npm/mppx."""
environment = {