Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

feat(mobile): unlock another computer by scanning its code, not its password

dee3aad92b8278d
diff --git a/mobile/lib/phone/machines_tab.dart b/mobile/lib/phone/machines_tab.dart
index 22d0f8307..cb47f042a 100644
--- a/mobile/lib/phone/machines_tab.dart
+++ b/mobile/lib/phone/machines_tab.dart
@@ -122,8 +122,7 @@ class _Body extends StatelessWidget {
Padding(
padding: const EdgeInsets.fromLTRB(Tty.origin, 0, Tty.origin, 8),
child: Text(
- 'Your harnesses run on these. Each one is unlocked with its own '
- 'phone password.',
+ 'Your harnesses run on these.',
style: tty.style(size: TtySize.meta, color: tty.faint),
),
),
@@ -161,7 +160,7 @@ class _Body extends StatelessWidget {
PhoneMachineStatus.needsPassword => (
'locked',
tty.yellow,
- 'tap to unlock with its phone password',
+ 'tap to unlock: scan its code',
),
PhoneMachineStatus.offline => (
'asleep',
diff --git a/mobile/lib/phone/welcome/scan_to_connect.dart b/mobile/lib/phone/welcome/scan_to_connect.dart
index a72062110..2a80d1eb5 100644
--- a/mobile/lib/phone/welcome/scan_to_connect.dart
+++ b/mobile/lib/phone/welcome/scan_to_connect.dart
@@ -32,6 +32,7 @@ class ScanToConnectPage extends StatefulWidget {
required this.onUseEmail,
required this.onBack,
this.signingIn = false,
+ this.fallbackLabel = 'Use email instead',
this.camera,
});
@@ -42,6 +43,10 @@ class ScanToConnectPage extends StatefulWidget {
/// A code was read and the phone is signing in with it.
final bool signingIn;
+ /// The way out without a camera or a code: email on the first screen, the computer's password
+ /// when unlocking one ([onUseEmail] is called either way).
+ final String fallbackLabel;
+
/// Stands in for the camera in tests and renders. Null opens the real one.
final Widget? camera;
@@ -130,7 +135,7 @@ class _ScanToConnectPageState extends State<ScanToConnectPage> {
const SizedBox(height: 16),
Center(
child: TtyTextButton(
- label: 'Use email instead',
+ label: widget.fallbackLabel,
color: tty.faint,
onPressed: widget.onUseEmail,
),
diff --git a/mobile/lib/phone/welcome/unlock_computer.dart b/mobile/lib/phone/welcome/unlock_computer.dart
index 77eb1fb50..e3c351267 100644
--- a/mobile/lib/phone/welcome/unlock_computer.dart
+++ b/mobile/lib/phone/welcome/unlock_computer.dart
@@ -6,17 +6,25 @@ import 'package:lucide_icons_flutter/lucide_icons.dart';
import 'package:harness_mobile/state/app_state.dart';
+import '../phone_navigation.dart' show phoneRoute;
import '../tty.dart';
import '../tty_controls.dart';
+import 'connect_code.dart';
+import 'scan_to_connect.dart';
-/// Unlocking a computer from this phone: its phone password, once — the page behind a locked
-/// computer's row.
+/// Unlocking a computer from this phone — the page behind a locked computer's row. The way the
+/// first computer was added comes first: scan the QR its Harness ▸ Add Phone… shows, no password.
+/// A computer with no desktop app (a server over SSH) has no QR to show, so its phone password
+/// stays, right under it.
///
/// ```
/// Unlock M2
-/// Enter the phone password you set on M2.
+/// On M2, open Harness ▸ Add Phone…, then scan it.
+/// [ Scan its code ]
+///
+/// or enter its phone password
/// [ •••••••• Show ]
-/// [ Unlock ]
+/// Unlock
///
/// Forgot it, or never set one? On M2, run
/// $ harness remote-password set Copy
@@ -30,11 +38,15 @@ class UnlockComputer extends StatefulWidget {
required this.notifier,
required this.machineState,
this.onUnlocked,
+ this.scanCamera,
});
final AppNotifier notifier;
final MachineState machineState;
+ /// Stands in for the camera on the scan page, in tests. Null opens the real one.
+ final Widget? scanCamera;
+
/// Called once the computer is unlocked.
final VoidCallback? onUnlocked;
@@ -47,17 +59,15 @@ class _UnlockComputerState extends State<UnlockComputer> {
final _focus = FocusNode();
bool _obscure = true;
bool _busy = false;
+
+ /// Pairing by a scanned code — the scan button's own busy state, apart from the password's.
+ bool _pairing = false;
String? _error;
String? _stage;
bool _copied = false;
- @override
- void initState() {
- super.initState();
- WidgetsBinding.instance.addPostFrameCallback((_) {
- if (mounted) _focus.requestFocus();
- });
- }
+ // ⚠️ No autofocus on the password: it is the second way in now, and a keyboard raised on
+ // arrival covered the scan button — the first.
@override
void dispose() {
@@ -107,6 +117,68 @@ class _UnlockComputerState extends State<UnlockComputer> {
}
}
+ /// The camera, over this page; a code of this computer's pairs the phone with it, the way the
+ /// first one was added — its one-time code, armed by its Add Phone dialog, and no password.
+ Future<void> _scan() async {
+ if (_busy || _pairing) return;
+ ConnectCode? scanned;
+ await Navigator.of(context).push(
+ phoneRoute(
+ (page) => Scaffold(
+ backgroundColor: Tty.of(page).ground,
+ body: SafeArea(
+ child: ScanToConnectPage(
+ camera: widget.scanCamera,
+ fallbackLabel: 'Use its password instead',
+ onCode: (code) {
+ scanned = code;
+ Navigator.of(page).pop();
+ },
+ onUseEmail: () => Navigator.of(page).pop(),
+ onBack: () => Navigator.of(page).pop(),
+ ),
+ ),
+ ),
+ ),
+ );
+ final code = scanned;
+ if (!mounted || code == null) return;
+ final machine = widget.machineState.machine;
+ final pairCode = code.pairCode;
+ if (code.machineId == null || pairCode == null) {
+ setState(
+ () => _error = "That code can't unlock a computer. Scan the one in Harness ▸ Add Phone….",
+ );
+ return;
+ }
+ if (code.machineId != machine.machineId) {
+ setState(
+ () => _error =
+ 'That code is for another computer. Scan the one on ${machine.displayName}.',
+ );
+ return;
+ }
+ setState(() {
+ _pairing = true;
+ _error = null;
+ });
+ final error = await widget.notifier.connectWithCode(
+ machine.machineId,
+ pairCode,
+ );
+ if (!mounted) return;
+ setState(() {
+ _pairing = false;
+ _error = error;
+ });
+ if (error == null) {
+ HapticFeedback.mediumImpact();
+ widget.onUnlocked?.call();
+ } else {
+ HapticFeedback.heavyImpact();
+ }
+ }
+
@override
Widget build(BuildContext context) {
final tty = Tty.of(context);
@@ -117,10 +189,23 @@ class _UnlockComputerState extends State<UnlockComputer> {
TtyText('Unlock $name', size: 24, weight: FontWeight.w600),
const SizedBox(height: 12),
Text(
- 'Enter the phone password you set on $name.',
+ 'On $name, open Harness ▸ Add Phone…, then scan it.',
style: tty.style(size: TtySize.row, color: tty.faint),
),
const SizedBox(height: 18),
+ TtyPrimaryButton(
+ key: const ValueKey('unlock-scan'),
+ label: 'Scan its code',
+ busy: _pairing,
+ busyLabel: 'Pairing with $name…',
+ onPressed: _busy ? null : () => unawaited(_scan()),
+ ),
+ const SizedBox(height: 28),
+ Text(
+ 'or enter its phone password',
+ style: tty.style(size: TtySize.meta, color: tty.faint),
+ ),
+ const SizedBox(height: 8),
_PasswordField(
controller: _password,
focus: _focus,
@@ -136,12 +221,15 @@ class _UnlockComputerState extends State<UnlockComputer> {
style: tty.style(size: TtySize.meta, color: tty.red),
),
),
- const SizedBox(height: 16),
- TtyPrimaryButton(
- label: 'Unlock',
- busy: _busy,
- busyLabel: _stage == null ? 'Unlocking…' : _say(_stage!),
- onPressed: _unlock,
+ const SizedBox(height: 4),
+ Align(
+ alignment: Alignment.centerRight,
+ child: TtyTextButton(
+ label: _busy
+ ? (_stage == null ? 'Unlocking…' : _say(_stage!))
+ : 'Unlock',
+ onPressed: _busy || _pairing ? null : _unlock,
+ ),
),
const SizedBox(height: 32),
Text(
diff --git a/mobile/test/unlock_by_scan_test.dart b/mobile/test/unlock_by_scan_test.dart
new file mode 100644
index 000000000..bc33c6614
--- /dev/null
+++ b/mobile/test/unlock_by_scan_test.dart
@@ -0,0 +1,156 @@
+import 'package:flutter/material.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/auth/auth_session.dart';
+import 'package:harness_mobile/auth/cli_link.dart';
+import 'package:harness_mobile/auth/peer_link_client.dart';
+import 'package:harness_mobile/core/config.dart';
+import 'package:harness_mobile/core/models.dart';
+import 'package:harness_mobile/phone/welcome/connect_code.dart';
+import 'package:harness_mobile/phone/welcome/scan_to_connect.dart';
+import 'package:harness_mobile/phone/welcome/unlock_computer.dart';
+import 'package:harness_mobile/state/app_state.dart';
+
+/// Pairs by code and answers [result]; records what it was asked.
+class _Links implements PeerLinkClient {
+ _Links(this.result);
+
+ final CliLinkConnectResult result;
+ final codes = <(String, String)>[];
+
+ @override
+ Future<CliLinkConnectResult> connectWithCode(
+ String machineId,
+ String code, {
+ required String label,
+ String? displayName,
+ }) async {
+ codes.add((machineId, code));
+ return result;
+ }
+
+ @override
+ Future<CliLinkConnectResult> connect(
+ String machineId,
+ String password, {
+ void Function(String stage)? onProgress,
+ String? displayName,
+ }) async => const CliLinkConnectResult(error: 'not used');
+
+ @override
+ Future<CliLinkListResult> list() async =>
+ const CliLinkListResult(machines: []);
+
+ @override
+ Future<String?> unlink(String machineId) async => null;
+}
+
+/// A locked computer is unlocked the way the first one was added: scan the QR its Add Phone
+/// shows. The password is still there, second, for a computer with no desktop app.
+void main() {
+ const studio = Machine(
+ machineId: 'studio',
+ authMode: MachineAuthMode.remote,
+ name: 'studio',
+ );
+
+ Future<(AppNotifier, List<bool>)> pump(
+ WidgetTester tester,
+ _Links links,
+ ) async {
+ final app = AppNotifier(
+ config: AppConfig.dev,
+ authSession: AuthSession(),
+ configStore: null,
+ peerLinks: links,
+ );
+ addTearDown(app.dispose);
+ app.machines = [studio];
+ final state = app.machineStates['studio'] = MachineState(studio)
+ ..nodeOnline = true
+ ..needsLink = true;
+ final unlocked = <bool>[];
+ await tester.pumpWidget(
+ MaterialApp(
+ home: Scaffold(
+ body: UnlockComputer(
+ notifier: app,
+ machineState: state,
+ scanCamera: const SizedBox(),
+ onUnlocked: () => unlocked.add(true),
+ ),
+ ),
+ ),
+ );
+ return (app, unlocked);
+ }
+
+ Future<void> scan(WidgetTester tester, String link) async {
+ await tester.tap(find.byKey(const ValueKey('unlock-scan')));
+ await tester.pumpAndSettle();
+ final page = tester.widget<ScanToConnectPage>(
+ find.byType(ScanToConnectPage),
+ );
+ expect(page.fallbackLabel, 'Use its password instead');
+ page.onCode(ConnectCode.parse(link)!);
+ await tester.pumpAndSettle();
+ }
+
+ testWidgets('scanning the computer\'s own code pairs it, no password', (
+ tester,
+ ) async {
+ final links = _Links(const CliLinkConnectResult(linkedMachineId: 'studio'));
+ final (_, unlocked) = await pump(tester, links);
+ expect(find.text('Scan its code'), findsOneWidget);
+ expect(find.text('or enter its phone password'), findsOneWidget);
+
+ await scan(
+ tester,
+ ConnectCode.link('[email protected]', machineId: 'studio', pairCode: 'K7QM4XPT'),
+ );
+ expect(links.codes, [('studio', 'K7QM4XPT')]);
+ expect(unlocked, [true]);
+ });
+
+ testWidgets('another computer\'s code is refused, and says whose it is', (
+ tester,
+ ) async {
+ final links = _Links(const CliLinkConnectResult(linkedMachineId: 'mini'));
+ final (_, unlocked) = await pump(tester, links);
+ await scan(
+ tester,
+ ConnectCode.link('[email protected]', machineId: 'mini', pairCode: 'K7QM4XPT'),
+ );
+ expect(links.codes, isEmpty);
+ expect(unlocked, isEmpty);
+ expect(find.textContaining('for another computer'), findsOneWidget);
+ });
+
+ testWidgets('a code with nothing to pair with is refused', (tester) async {
+ final links = _Links(const CliLinkConnectResult(linkedMachineId: 'studio'));
+ final (_, unlocked) = await pump(tester, links);
+ await scan(tester, ConnectCode.link('[email protected]'));
+ expect(links.codes, isEmpty);
+ expect(unlocked, isEmpty);
+ expect(find.textContaining("can't unlock a computer"), findsOneWidget);
+ });
+
+ testWidgets(
+ 'a pairing that fails says why, and the password is still there',
+ (tester) async {
+ final links = _Links(
+ const CliLinkConnectResult(error: 'That code didn’t match.'),
+ );
+ final (_, unlocked) = await pump(tester, links);
+ await scan(
+ tester,
+ ConnectCode.link('[email protected]', machineId: 'studio', pairCode: 'K7QM4XPT'),
+ );
+ expect(unlocked, isEmpty);
+ expect(find.textContaining('didn’t match'), findsOneWidget);
+ expect(
+ find.byKey(const Key('remote-password-connect-field')),
+ findsOneWidget,
+ );
+ },
+ );
+}