mirror of
https://github.com/block/buzz.git
synced 2026-11-10 19:32:00 +00:00
The mobile app can now be built as a public build or an internal build, set explicitly with `BUZZ_DISTRIBUTION=internal|public`. Each build trusts exactly one Builderlab (`BUZZ_BUILDERLAB_URL`), and sends that Builderlab's relay assertions only to the relays in `BUZZ_BUILDERLAB_RELAYS`. The three settings are set together or not at all; with none set, the app is a Nostr-only development build. The decision is recorded in `docs/adr/0001-internal-and-public-mobile-builds.md`, and the terms are defined in `CONTEXT.md`. Before this change, one build supported a public Builderlab and an organization Builderlab at the same time, with two sessions. An organization's members could reach public communities from the organization's app. ### Behavior - **Public build:** QR pairing, email sign-in and multiple communities are unchanged. It has no knowledge of any organization. An organization's relay already rejects clients that can't present its Builderlab's assertion, and the public build relies on that. - **Internal build:** the first screen has a single **Sign in** button that goes straight to the organization's single sign-on and then joins the organization's community. There is no QR pairing, community link or community creation, and the community switcher is hidden. Only the exact relay hosts listed in `BUZZ_BUILDERLAB_RELAYS` are reachable. - **Identity (internal):** a member whose account has no Nostr identity yet gets a new key on first sign-in; the first relay assertion binds it, and the app backs it up to the organization automatically. A member whose account is already bound uses that key from this device, or restores it from the organization's backup. If neither is possible, Sign in says it couldn't restore the identity and saves nothing. - **Sign-out (internal):** a destructive **Sign out** row at the bottom of Settings, the build's only way out. It removes the community, closes Settings and returns to Sign in. The Builderlab session is cleared from storage immediately and can never be reused to sign back in. The relay push-stop keeps the retired credential in memory only, for up to 10 minutes, and uses it only to get an assertion for a key that is already bound. It is a best-effort request. - **Upgrade to an internal build:** a saved public Builderlab session is cleared. Communities on other relays are hidden, not deleted, and their keys stay on the device. - `BUZZ_HOSTED_RELAY_AUTH` is unchanged here. ### Screenshots (internal build) | Sign in | Community menu | Settings | | --- | --- | --- | |  |  |  | Gateway-side push revocation, which needs no Builderlab session, is planned as a separate PR. ### Validation Added tests: - An internal-build journey: one Sign in creates and binds a key, and a retry after a cancelled join reuses it. The app uploads that key's backup itself. Sign out from Settings closes it and returns to the landing, which ends the session, and Sign in then restores the same identity from the backup. Communities on other relays stay stored but hidden. - Internal Sign in for a bound account: the key on this device, a key restored from backup, and no backup (clear error, nothing saved). Each guard was shown to fail when removed. - Organization relay assertions: assertions go only to exact listed hosts, not subdomains. After sign-out, only a push stop for a bound key gets an assertion, and nothing can sign back in. Proven to fail without the fix. - Builderlab build setting parsing. CI runs the internal and public journeys in their own lanes with those settings. The full suite passes in the default configuration. The full suite isn't meant to run under the internal configuration, because most fixtures use relays that an internal build hides. Automated full-app run on an iPhone 17 Pro simulator at the current head: - Public build, upgraded in place over a signed-in install: home loads, a channel opens and a message sends. - Internal build: a fresh install shows only Sign in. With a saved organization community, the community menu has no Switch community, and Settings → Sign out → confirm returns to Sign in. Real single sign-on can't run in the simulator. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Tom Brow <tomb@squareup.com> Co-authored-by: Tom Brow <tomb@squareup.com> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
187 lines
6.2 KiB
Dart
187 lines
6.2 KiB
Dart
import '../storage/serial_queue.dart';
|
|
import 'dart:convert';
|
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
|
|
import '../community/community.dart';
|
|
import '../relay/relay_socket.dart';
|
|
import '../storage/secure_storage.dart';
|
|
import 'account_identity_store.dart';
|
|
import 'community_entry_requirements.dart';
|
|
import 'device_identity_source.dart';
|
|
|
|
/// A self-hosted attempt keeps its key before any network operation. Retrying
|
|
/// after a refused connection or process death never rotates that identity.
|
|
class SelfHostedEntry {
|
|
SelfHostedEntry({
|
|
FlutterSecureStorage? storage,
|
|
DeviceIdentitySource? identities,
|
|
AccountIdentityStore? accountStore,
|
|
Future<void> Function(Community)? probe,
|
|
}) : _storage = storage ?? communitySecureStorage,
|
|
identities = identities ?? DeviceIdentitySource(),
|
|
_accountStore = accountStore ?? AccountIdentityStore(),
|
|
_probe = probe ?? probeRelay;
|
|
|
|
final FlutterSecureStorage _storage;
|
|
final DeviceIdentitySource identities;
|
|
final AccountIdentityStore _accountStore;
|
|
final Future<void> Function(Community) _probe;
|
|
static final _queue = SerialQueue();
|
|
|
|
/// Saved keys available for explicit reuse, including a hosted account that
|
|
/// has not joined any community yet. This performs no account network calls.
|
|
Future<({List<DeviceIdentityChoice> choices, bool complete})>
|
|
choices() async {
|
|
var complete = true;
|
|
var device = <DeviceIdentityChoice>[];
|
|
var accounts = <AccountIdentitySnapshot>[];
|
|
try {
|
|
device = await identities.choices();
|
|
} on AccountIdentityFailure {
|
|
complete = false;
|
|
}
|
|
try {
|
|
accounts = await _accountStore.identities();
|
|
} on AccountIdentityFailure {
|
|
complete = false;
|
|
}
|
|
return (
|
|
choices: [
|
|
...device,
|
|
for (final account in accounts)
|
|
if (!device.any((item) => item.pubkey == account.pubkey))
|
|
DeviceIdentityChoice(account.pubkey, 'Saved identity'),
|
|
],
|
|
complete: complete,
|
|
);
|
|
}
|
|
|
|
Future<Community> prepare({
|
|
required String relay,
|
|
required String name,
|
|
String? nsec,
|
|
String? savedPubkey,
|
|
bool create = false,
|
|
required bool Function() isCurrent,
|
|
}) {
|
|
final result = _queue.run(() async {
|
|
CommunityEntryRequirements.requireSelfHosted(relay);
|
|
final uri = Uri.parse(relay);
|
|
if (uri.scheme != 'wss' ||
|
|
uri.host.isEmpty ||
|
|
uri.userInfo.isNotEmpty ||
|
|
uri.hasQuery ||
|
|
uri.hasFragment ||
|
|
(uri.path.isNotEmpty && uri.path != '/')) {
|
|
throw const FormatException('Invalid community address');
|
|
}
|
|
void check() {
|
|
if (!isCurrent()) throw StateError('Setup cancelled');
|
|
}
|
|
|
|
check();
|
|
final key = _pendingKey(relay);
|
|
final retained = await _storage.read(key: key);
|
|
String? candidate = nsec;
|
|
// A retained key for this exact identity is the one an interrupted
|
|
// attempt chose; it may exist nowhere else yet.
|
|
if (savedPubkey != null &&
|
|
(retained == null ||
|
|
AccountIdentityStore.publicKeyForNsec(retained) != savedPubkey)) {
|
|
// Selection is explicit: resolve the exact key without requiring a
|
|
// complete inventory of unrelated account slots.
|
|
try {
|
|
candidate = await identities.withIdentity(
|
|
savedPubkey,
|
|
(nsec) async => nsec,
|
|
);
|
|
} on AccountIdentityFailure {
|
|
candidate = await _accountStore.withSavedIdentity(
|
|
savedPubkey,
|
|
(nsec) async => nsec,
|
|
);
|
|
}
|
|
}
|
|
if (retained != null) {
|
|
final retainedPubkey = AccountIdentityStore.publicKeyForNsec(retained);
|
|
if (candidate != null &&
|
|
AccountIdentityStore.publicKeyForNsec(candidate) !=
|
|
retainedPubkey) {
|
|
// An explicit replacement may follow a refused or uncertain probe.
|
|
// Preserve the previous key before changing the relay's retry slot.
|
|
await _accountStore.forPairing(retainedPubkey).create(retained);
|
|
check();
|
|
}
|
|
candidate ??= retained;
|
|
}
|
|
if (candidate == null) {
|
|
if (!create) throw AccountIdentityFailure.absent;
|
|
final inventory = await choices();
|
|
if (!inventory.complete) throw AccountIdentityFailure.unreadable;
|
|
if (inventory.choices.isNotEmpty) {
|
|
throw AccountIdentityFailure.absent;
|
|
}
|
|
candidate = nostr.Keys.generate().nsec;
|
|
}
|
|
final pubkey = AccountIdentityStore.publicKeyForNsec(candidate);
|
|
check();
|
|
if (retained != candidate) {
|
|
await _storage.write(key: key, value: candidate);
|
|
if (await _storage.read(key: key) != candidate) {
|
|
throw AccountIdentityFailure.unreadable;
|
|
}
|
|
}
|
|
check();
|
|
final community = Community.create(
|
|
name: name,
|
|
relayUrl: relay,
|
|
pubkey: pubkey,
|
|
nsec: candidate,
|
|
);
|
|
await _probe(community);
|
|
check();
|
|
return community;
|
|
});
|
|
return result;
|
|
}
|
|
|
|
static String _pendingKey(String relay) =>
|
|
'buzz_pending_self_hosted_${base64Url.encode(utf8.encode(relay))}';
|
|
|
|
/// Retire this exact pending key only after its community is durably saved.
|
|
Future<void> complete(Community community) => _queue.run(() async {
|
|
final key = _pendingKey(community.relayUrl);
|
|
if (await _storage.read(key: key) != community.nsec) return;
|
|
await _storage.delete(key: key);
|
|
if (await _storage.read(key: key) != null) {
|
|
throw AccountIdentityFailure.unreadable;
|
|
}
|
|
});
|
|
|
|
/// Authentication must succeed before a new community becomes active.
|
|
/// [headers] go on the upgrade, for a relay that requires an assertion.
|
|
static Future<void> probeRelay(
|
|
Community community, [
|
|
Map<String, String> headers = const {},
|
|
]) async {
|
|
final socket = RelaySocket(
|
|
wsUrl: community.relayUrl,
|
|
nsec: community.nsec,
|
|
onMessage: (_) {},
|
|
onConnected: () {},
|
|
onDisconnected: (_) {},
|
|
);
|
|
try {
|
|
socket.headers = headers;
|
|
await socket.connect().timeout(const Duration(seconds: 20));
|
|
if (socket.state != SocketState.connected) {
|
|
throw StateError('Community refused entry');
|
|
}
|
|
} finally {
|
|
socket.dispose();
|
|
}
|
|
}
|
|
}
|