diff --git a/lib/cli/cli_session.dart b/lib/cli/cli_session.dart index 8c2494f..997c1f5 100644 --- a/lib/cli/cli_session.dart +++ b/lib/cli/cli_session.dart @@ -51,8 +51,13 @@ class CliSession { this.repository, this._firebase, { this.passphraseFile, + this.sharedPasswordFile, }); + /// The fleet-wide crdt-sync password the Python daemons read too + /// (`~/.config/crdt-sync/password`), found from the seed's config root. + final File? sharedPasswordFile; + /// Where the passphrase is read from besides the environment: next to the /// seeded Firebase session, e.g. `~/.config/todo/e2e_passphrase`. final File? passphraseFile; @@ -121,6 +126,9 @@ class CliSession { passphraseFile: File( p.join(p.dirname(seedCredentials), e2ePassphraseFileName), ), + sharedPasswordFile: File( + p.join(p.dirname(p.dirname(seedCredentials)), 'crdt-sync', 'password'), + ), ); } @@ -142,10 +150,19 @@ class CliSession { oldPrefix: plainService.notesDir, readPassphrase: _readPassphrase, ); - if (e2e is! E2eReady && !push) { - // Read-only: nothing encrypted yet (or no key) falls back to the old - // plaintext prefix, which still holds the pre-migration data. - return await const SyncService().sync(repository, _remote(push: false)); + if (!push) { + // Read-only during the migration: old builds still write the plaintext + // prefix, so merge it, then the encrypted one when this host has a key. + final plain = await const SyncService().sync( + repository, + _remote(push: false), + ); + if (e2e is! E2eReady) return plain; + final layout = E2eLayout.of(plainService.notesDir); + return await SyncService( + notesDir: layout.prefix, + revsPath: layout.revsPath, + ).sync(repository, e2e.store); } if (e2e is! E2eReady) { throw StateError( @@ -173,9 +190,13 @@ class CliSession { Future _readPassphrase() async { final fromEnv = Platform.environment[e2ePassphraseEnv]; if (fromEnv != null && fromEnv.isNotEmpty) return fromEnv; - final file = passphraseFile; - if (file == null || !file.existsSync()) return null; - return (await file.readAsString()).trim(); + for (final file in [passphraseFile, sharedPasswordFile]) { + if (file != null && file.existsSync()) { + final text = (await file.readAsString()).trim(); + if (text.isNotEmpty) return text; + } + } + return null; } /// Releases the log and the HTTP client. diff --git a/lib/sync/run_sync.dart b/lib/sync/run_sync.dart index 8965f7a..7d5028b 100644 --- a/lib/sync/run_sync.dart +++ b/lib/sync/run_sync.dart @@ -8,8 +8,8 @@ library; import 'package:crdt_sync/crdt_sync.dart'; import 'package:crdt_sync_flutter/crdt_sync_flutter.dart' - show kE2ePassphraseKey, kSecureStorage; -import 'package:flutter/foundation.dart' show visibleForTesting; + show kE2ePassphraseKey, kSecureStorage, keystoreKeyCache; +import 'package:flutter/foundation.dart' show ValueNotifier, visibleForTesting; import 'package:http/http.dart' as http; import 'package:todo/analytics/analytics_service.dart'; import 'package:todo/data/app_settings.dart'; @@ -23,6 +23,10 @@ import 'package:todo/sync/sync_state_factory.dart'; @visibleForTesting Argon2Params e2eArgon2Params = Argon2Params.recommended; +/// Whether the last sync found no usable end-to-end key; drives the lock +/// tile in settings. +final ValueNotifier e2eLocked = ValueNotifier(false); + /// Bundles a [SyncResult] with the [AppSettings] as reconciled against the /// same Firebase session — so a settings change made on another device is /// picked up without a second authenticated round trip. @@ -93,7 +97,10 @@ Future runSync( readPassphrase: readPassphrase ?? () => kSecureStorage.read(key: kE2ePassphraseKey), params: e2eArgon2Params, + // Argon2id once per device, not once per tick. + keyCache: keystoreKeyCache(service.notesDir), ); + e2eLocked.value = e2e is! E2eReady; if (e2e is! E2eReady) { return const SyncRunResult( syncResult: SyncResult(mergedDevices: 0, pushed: false, locked: true), diff --git a/lib/ui/settings_screen.dart b/lib/ui/settings_screen.dart index 6ee3651..3e7bcb1 100644 --- a/lib/ui/settings_screen.dart +++ b/lib/ui/settings_screen.dart @@ -15,6 +15,7 @@ import 'package:todo/sync/backlog_export.dart'; import 'package:todo/sync/firebase_backend.dart'; import 'package:todo/sync/google_sign_in_backend.dart'; import 'package:todo/sync/notes_markdown.dart'; +import 'package:todo/sync/run_sync.dart'; import 'package:todo/sync/sync_settings.dart'; import 'package:todo/ui/app_route.dart'; import 'package:todo/ui/cloud_images_settings.dart'; @@ -166,6 +167,22 @@ class _SettingsScreenState extends State { await widget.repository.importNotes(notes); } + /// Retries the sync after a passphrase is entered; the lock tile follows + /// [e2eLocked], which the run updates. + Future _syncNow() async { + try { + await runSync( + widget.repository, + widget.initial, + httpClient: widget.httpClient, + firebaseFactory: widget.firebaseFactory, + stateStore: widget.stateStore, + ); + } on Exception { + // The capture screen's status line reports sync failures. + } + } + Future _openSyncSettings() async { await Navigator.of(context).push( appRoute( @@ -225,9 +242,16 @@ class _SettingsScreenState extends State { const Icon(Icons.sync, semanticLabel: 'Sync settings: Firebase'), _openSyncSettings, ), - const ListTile( - contentPadding: EdgeInsets.zero, - leading: E2ePassphraseButton(locked: true), + ValueListenableBuilder( + valueListenable: e2eLocked, + builder: (context, locked, _) => ListTile( + contentPadding: EdgeInsets.zero, + leading: E2ePassphraseButton( + locked: locked, + // A new passphrase is only proven by a sync that unlocks. + onSaved: () => unawaited(_syncNow()), + ), + ), ), CloudImagesTile(httpClient: widget.httpClient), _link( diff --git a/pubspec.lock b/pubspec.lock index 8a98cdb..e61ea01 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -109,20 +109,20 @@ packages: dependency: "direct main" description: path: crdt_sync_dart - ref: "crdt_sync_dart-v0.13.1" - resolved-ref: eebeb0b51053f160f163140141aba10a447ba1e8 + ref: "crdt_sync_dart-v0.14.0" + resolved-ref: "84343aec2dd07c38cba40690c3b5f2c84ae9cdf6" url: "https://github.com/kuhyx/utils" source: git - version: "0.13.1" + version: "0.14.0" crdt_sync_flutter: dependency: "direct main" description: path: crdt_sync_flutter - ref: "crdt_sync_flutter-v0.7.0" - resolved-ref: "307248b60bbb4e71f8625047a379c5aa275b147e" + ref: "crdt_sync_flutter-v0.8.0" + resolved-ref: e5990c9c15761934110f955529bc1d68479b06ff url: "https://github.com/kuhyx/utils" source: git - version: "0.7.0" + version: "0.8.0" cross_file: dependency: transitive description: @@ -1023,11 +1023,11 @@ packages: dependency: "direct main" description: path: sync_settings_ui - ref: "sync_settings_ui-v0.7.1" - resolved-ref: eb9bdc5796755c4e02b7a54d83efcdeb4db91af1 + ref: "sync_settings_ui-v0.7.2" + resolved-ref: "4567e7ca3042e683befdf6697ace1c8be9c829fc" url: "https://github.com/kuhyx/utils" source: git - version: "0.7.1" + version: "0.7.2" synchronized: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 90f5160..b0db8b9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.0.84+1 +version: 1.0.85+1 environment: sdk: ^3.12.2 @@ -33,12 +33,12 @@ dependencies: crdt_sync: git: url: https://github.com/kuhyx/utils - ref: crdt_sync_dart-v0.13.1 + ref: crdt_sync_dart-v0.14.0 path: crdt_sync_dart crdt_sync_flutter: git: url: https://github.com/kuhyx/utils - ref: crdt_sync_flutter-v0.7.0 + ref: crdt_sync_flutter-v0.8.0 path: crdt_sync_flutter # The following adds the Cupertino Icons font to your application. @@ -102,7 +102,7 @@ dependencies: sync_settings_ui: git: url: https://github.com/kuhyx/utils - ref: sync_settings_ui-v0.7.1 + ref: sync_settings_ui-v0.7.2 path: sync_settings_ui url_launcher: 6.3.3 uuid: 4.6.0 diff --git a/test/cli/cli_harness.dart b/test/cli/cli_harness.dart index f5be5f9..4ac8088 100644 --- a/test/cli/cli_harness.dart +++ b/test/cli/cli_harness.dart @@ -69,6 +69,7 @@ class CliHarness { /// Publishes [notes] as device [node]'s log. void seedPeer(List notes, {String node = 'peer'}) { + resetE2eCopyMemo(); db.files['todo-sync/notes/$node~2Ejson'] = logToJson({ for (final n in notes) n.id: NoteRepository.recordAtNoteTime(n, node), }); diff --git a/test/cli/fake_rtdb.dart b/test/cli/fake_rtdb.dart index 6ec2f9b..f1050a6 100644 --- a/test/cli/fake_rtdb.dart +++ b/test/cli/fake_rtdb.dart @@ -8,6 +8,7 @@ library; import 'dart:convert'; import 'dart:io'; +import 'package:crdt_sync/crdt_sync.dart' show resetE2eCopyMemo; import 'package:http/http.dart' as http; import 'package:http/testing.dart' as http_testing; @@ -64,6 +65,8 @@ class FakeRtdb { /// Writes a Firebase session that does not expire during a test run. File writeSeedCredentials(Directory dir, {String name = 'seed.json'}) { + // A fresh fake database: the one-time copy must run again for it. + resetE2eCopyMemo(); // The CLI reads its crdt-sync passphrase from beside the seed file. File('${dir.path}/e2e_passphrase').writeAsStringSync('test-passphrase'); return File('${dir.path}/$name')..writeAsStringSync( diff --git a/test/fake_secure_storage.dart b/test/fake_secure_storage.dart index 4fc6d8f..c3abcc3 100644 --- a/test/fake_secure_storage.dart +++ b/test/fake_secure_storage.dart @@ -1,4 +1,4 @@ -import 'package:crdt_sync/crdt_sync.dart' show Argon2Params; +import 'package:crdt_sync/crdt_sync.dart' show Argon2Params, resetE2eCopyMemo; import 'package:flutter/services.dart'; import 'package:todo/sync/run_sync.dart' show e2eArgon2Params; import 'package:flutter_test/flutter_test.dart'; @@ -15,6 +15,7 @@ void installFakeSecureStorage({ bool throwing = false, }) { const channel = MethodChannel('plugins.it_nomads.com/flutter_secure_storage'); + resetE2eCopyMemo(); e2eArgon2Params = const Argon2Params( memoryKiB: 8, iterations: 1, diff --git a/test/settings_screen_e2e_lock_test.dart b/test/settings_screen_e2e_lock_test.dart new file mode 100644 index 0000000..db2c883 --- /dev/null +++ b/test/settings_screen_e2e_lock_test.dart @@ -0,0 +1,35 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:todo/sync/run_sync.dart'; + +import 'capture_screen_harness.dart' show recordingMock; +import 'settings_screen_harness.dart'; + +void main() { + tearDown(() => e2eLocked.value = false); + + testWidgets('the lock tile follows the last sync', (tester) async { + e2eLocked.value = true; + await pumpSettings(tester); + expect(find.byIcon(Icons.lock), findsOneWidget); + e2eLocked.value = false; + await tester.pump(); + expect(find.byIcon(Icons.lock_open), findsOneWidget); + }); + + testWidgets('saving a passphrase retries the sync, which unlocks', ( + tester, + ) async { + e2eLocked.value = true; + await pumpSettings(tester, httpClient: recordingMock([])); + await tester.tap(find.byIcon(Icons.lock)); + await tester.pumpAndSettle(); + await tester.enterText(find.byType(TextField).last, 'test-passphrase'); + await tester.tap(find.byIcon(Icons.check)); + await tester.runAsync( + () => Future.delayed(const Duration(seconds: 1)), + ); + await tester.pump(); + expect(e2eLocked.value, isFalse); + }); +}