Compare commits

..

No commits in common. "be376fd240d59508340a3e2fe79736ca7bb9aec1" and "91c7691fbfa7f012911f9d6e67c352aed55ce77d" have entirely different histories.

5 changed files with 6 additions and 143 deletions

View File

@ -2,7 +2,6 @@ import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:crdt_sync/crdt_sync.dart';
import 'package:path/path.dart' as p;
/// Local HTTP server backing the desktop app.
@ -28,20 +27,7 @@ class WrapperServer {
required this.webRoot,
required this.backlogPath,
required this.logPath,
bool? serveSyncAccount,
String? syncConfigDir,
}) : serveSyncAccount =
serveSyncAccount ??
(Platform.environment[kSyncAccountEnvVar] ?? '').isNotEmpty,
syncConfigDir =
syncConfigDir ??
p.join(Platform.environment['HOME'] ?? '', '.config', 'crdt-sync');
/// Whether the sync-account route answers; off unless explicitly enabled.
final bool serveSyncAccount;
/// Directory holding `firebase.json` and `password`.
final String syncConfigDir;
});
/// Directory holding the built Flutter web assets.
final String webRoot;
@ -93,54 +79,9 @@ class WrapperServer {
if (path == '/backup/log') {
return _file(request, logPath);
}
if (path == kSyncAccountPath) {
return _syncAccount(request);
}
return _static(request, path);
}
/// Serves the shared sync account so a desktop install can self-provision.
///
/// Off unless CRDT_SYNC_SERVE_ACCOUNT is set: this hands out a credential
/// with database write access to anything that can reach the port, so it is
/// something you switch on once to set an install up, not a standing route.
/// 404 (rather than 403) when disabled, so a probe cannot tell the route
/// exists.
///
/// The account is read from the same ~/.config/crdt-sync/ pair the Python
/// daemons already use, so there is one source of truth per machine rather
/// than a per-app copy.
Future<void> _syncAccount(HttpRequest request) async {
if (!serveSyncAccount) {
request.response.statusCode = HttpStatus.notFound;
return;
}
final configFile = File(p.join(syncConfigDir, 'firebase.json'));
final passwordFile = File(p.join(syncConfigDir, 'password'));
if (!configFile.existsSync() || !passwordFile.existsSync()) {
stderr.writeln(
'Sync account requested but ~/.config/crdt-sync/ is incomplete — '
'the desktop app will keep asking for credentials.',
);
request.response.statusCode = HttpStatus.notFound;
return;
}
final email = (jsonDecode(await configFile.readAsString())
as Map<String, dynamic>)['email'];
if (email is! String || email.isEmpty) {
stderr.writeln('firebase.json has no usable "email" — not serving it.');
request.response.statusCode = HttpStatus.notFound;
return;
}
request.response.headers.contentType = ContentType.json;
request.response.write(
jsonEncode({
'email': email,
'password': (await passwordFile.readAsString()).trim(),
}),
);
}
/// GET returns the file's contents (404 when absent); POST overwrites it.
Future<void> _file(HttpRequest request, String filePath) async {
final file = File(filePath);

View File

@ -45,24 +45,11 @@ SecureCredentialStore credentialStore() => SecureCredentialStore(
);
/// Reads the per-device account, or null when sync has not been set up.
///
/// Falls back to the local wrapper when the store is empty, so a desktop
/// install can provision itself from `~/.config/crdt-sync/` instead of the
/// account being retyped per machine. The fetched account is written to the
/// store on first success, so the route is consulted once rather than being
/// load-bearing forever.
Future<FirebaseAccount?> loadAccount() async {
try {
final stored = FirebaseAccount.tryParse(
return FirebaseAccount.tryParse(
await _secure.read(key: kFirebaseAccountKey),
);
if (stored != null) return stored;
// Disconnect must stick: without this the next launch would silently
// re-adopt the account and the button would look broken.
if (await _secure.read(key: kSyncAccountOptOutKey) != null) return null;
final provisioned = await accountFromWrapper(Uri.base);
if (provisioned != null) await saveAccount(provisioned);
return provisioned;
} on Exception {
// No secret service available: behave as "not configured" rather than
// crashing the settings screen.
@ -77,8 +64,6 @@ Future<void> saveAccount(FirebaseAccount account) =>
/// Forgets the account and any cached session.
Future<void> clearAccount() async {
await _secure.delete(key: kFirebaseAccountKey);
// Suppress wrapper re-provisioning; see loadAccount().
await _secure.write(key: kSyncAccountOptOutKey, value: 'true');
await credentialStore().clear();
}

View File

@ -101,11 +101,11 @@ packages:
dependency: "direct main"
description:
path: crdt_sync_dart
ref: "crdt_sync_dart-v0.8.0"
resolved-ref: "6a080333c2ed2b2773fee01605dd67ba713e54ea"
ref: "crdt_sync_dart-v0.6.0"
resolved-ref: fc99eff00599a4c3195e4c1573ba08f15909a10d
url: "https://github.com/kuhyx/utils"
source: git
version: "0.8.0"
version: "0.6.0"
cross_file:
dependency: transitive
description:

View File

@ -33,7 +33,7 @@ dependencies:
crdt_sync:
git:
url: https://github.com/kuhyx/utils
ref: crdt_sync_dart-v0.8.0
ref: crdt_sync_dart-v0.6.0
path: crdt_sync_dart
# The following adds the Cupertino Icons font to your application.

View File

@ -1,7 +1,6 @@
import 'dart:convert';
import 'dart:io';
import 'package:crdt_sync/crdt_sync.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:path/path.dart' as p;
@ -150,66 +149,4 @@ void main() {
expect(kind('a.bin'), 'application/octet-stream');
expect(kind('a.unknown'), 'application/octet-stream');
});
group('sync-account provisioning', () {
/// Starts a second server with provisioning enabled against [files].
Future<String> enabledOrigin(Map<String, String> files) async {
final configDir = Directory(p.join(root.path, 'crdt-sync'))
..createSync(recursive: true);
files.forEach((name, contents) {
File(p.join(configDir.path, name)).writeAsStringSync(contents);
});
final enabled = WrapperServer(
webRoot: p.join(root.path, 'web'),
backlogPath: p.join(root.path, 'todo', 'BACKLOG.md'),
logPath: p.join(root.path, 'state', 'todo_notes.json'),
serveSyncAccount: true,
syncConfigDir: configDir.path,
);
await enabled.start(0);
addTearDown(enabled.stop);
return 'http://localhost:${enabled.port}';
}
test('is 404 when not enabled', () async {
// The default, and the whole security posture: a credential route must
// not be reachable just because the app is running.
final response = await http.get(Uri.parse('$base$kSyncAccountPath'));
expect(response.statusCode, HttpStatus.notFound);
});
test('serves the account when enabled', () async {
final origin = await enabledOrigin({
'firebase.json': '{"email":"a@b.c"}',
'password': 'pw\n',
});
final response = await http.get(Uri.parse('$origin$kSyncAccountPath'));
final account = FirebaseAccount.tryParse(response.body);
expect(response.statusCode, HttpStatus.ok);
expect(account?.email, 'a@b.c');
expect(account?.password, 'pw');
});
test('is 404 when the config files are absent', () async {
final origin = await enabledOrigin({});
final response = await http.get(Uri.parse('$origin$kSyncAccountPath'));
expect(response.statusCode, HttpStatus.notFound);
});
test('is 404 when firebase.json has no usable email', () async {
final origin = await enabledOrigin({
'firebase.json': '{"apiKey":"x"}',
'password': 'pw',
});
final response = await http.get(Uri.parse('$origin$kSyncAccountPath'));
expect(response.statusCode, HttpStatus.notFound);
});
});
}