mirror of
https://github.com/block/buzz.git
synced 2026-11-12 04:12:21 +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>
211 lines
7.1 KiB
Dart
211 lines
7.1 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:crypto/crypto.dart';
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
|
|
import '../hosted/organization_environment.dart';
|
|
import '../storage/secure_storage.dart';
|
|
import '../storage/serial_queue.dart';
|
|
|
|
import 'community.dart';
|
|
|
|
class CommunityStorage {
|
|
static const _keyCommunities = 'buzz_communities';
|
|
static const _keyActiveId = 'buzz_active_community_id';
|
|
|
|
// Legacy keys for migration.
|
|
static const _legacyCommunities = 'buzz_workspaces';
|
|
static const _legacyActiveId = 'buzz_active_workspace_id';
|
|
static const _legacyRelayUrl = 'buzz_relay_url';
|
|
static const _legacyToken = 'buzz_token';
|
|
static const _legacyPubkey = 'buzz_pubkey';
|
|
static const _legacyNsec = 'buzz_nsec';
|
|
|
|
final FlutterSecureStorage _secure;
|
|
|
|
/// Whether this build shows a saved community. Others stay stored, with
|
|
/// their keys, for a build that can reach them.
|
|
final bool Function(Community community) _visible;
|
|
|
|
CommunityStorage({
|
|
FlutterSecureStorage? secure,
|
|
bool Function(Community community)? visible,
|
|
}) : _secure = secure ?? communitySecureStorage,
|
|
_visible = visible ?? reachableInBuild;
|
|
|
|
/// An internal build reaches only its organization's community.
|
|
static bool reachableInBuild(Community community) =>
|
|
!OrganizationEnvironment.enabled ||
|
|
OrganizationEnvironment.protects(Uri.parse(community.relayUrl));
|
|
|
|
/// Load all communities this build shows. On first call, migrates legacy
|
|
/// single-community credentials if present.
|
|
Future<List<Community>> loadAll() async =>
|
|
(await _loadStored()).where(_visible).toList();
|
|
|
|
Future<List<Community>> _loadStored() async {
|
|
final raw = await _secure.read(key: _keyCommunities);
|
|
if (raw != null) return _decodeList(raw);
|
|
|
|
final legacyCommunities = await _secure.read(key: _legacyCommunities);
|
|
if (legacyCommunities != null) {
|
|
final communities = _decodeList(legacyCommunities);
|
|
await _saveList(communities);
|
|
final legacyActiveId = await _secure.read(key: _legacyActiveId);
|
|
if (legacyActiveId != null) await saveActiveId(legacyActiveId);
|
|
await _secure.delete(key: _legacyCommunities);
|
|
await _secure.delete(key: _legacyActiveId);
|
|
return communities;
|
|
}
|
|
|
|
// Migration: check for legacy single-community keys.
|
|
final legacyUrl = await _secure.read(key: _legacyRelayUrl);
|
|
final legacyToken = await _secure.read(key: _legacyToken);
|
|
if (legacyUrl != null && legacyToken != null) {
|
|
final legacyPubkey = await _secure.read(key: _legacyPubkey);
|
|
final legacyNsec = await _secure.read(key: _legacyNsec);
|
|
|
|
final name = Community.nameFromUrl(legacyUrl);
|
|
final community = Community.create(
|
|
name: name,
|
|
relayUrl: legacyUrl,
|
|
pubkey: legacyPubkey,
|
|
nsec: legacyNsec,
|
|
sensitiveActionPolicy: SensitiveActionPolicy.disabledByUser,
|
|
);
|
|
|
|
await _saveList([community]);
|
|
await saveActiveId(community.id);
|
|
|
|
// Delete legacy keys.
|
|
await _secure.delete(key: _legacyRelayUrl);
|
|
await _secure.delete(key: _legacyToken);
|
|
await _secure.delete(key: _legacyPubkey);
|
|
await _secure.delete(key: _legacyNsec);
|
|
|
|
return [community];
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
static final _pendingInvites = SerialQueue();
|
|
static const _pendingInvitePrefix = 'buzz_pending_invite_v1_';
|
|
|
|
static String _pendingInviteKey(String relayUrl, String code) {
|
|
final uri = Uri.parse(relayUrl);
|
|
final origin = uri
|
|
.replace(
|
|
scheme: ['https', 'wss'].contains(uri.scheme) ? 'https' : 'http',
|
|
)
|
|
.origin;
|
|
return '$_pendingInvitePrefix${sha256.convert(utf8.encode(jsonEncode([origin, code])))}';
|
|
}
|
|
|
|
/// Retain the claim identity before sending a request with uncertain outcomes.
|
|
/// A failed read/write must never generate a replacement for an unknown key.
|
|
Future<String> pendingInviteIdentity(
|
|
String relayUrl,
|
|
String code,
|
|
String Function() generate,
|
|
) => _pendingInvites.run(() async {
|
|
final key = _pendingInviteKey(relayUrl, code);
|
|
final saved = await _secure.read(key: key);
|
|
if (saved != null) return saved;
|
|
final nsec = generate();
|
|
await _secure.write(key: key, value: nsec);
|
|
if (await _secure.read(key: key) != nsec) {
|
|
throw StateError('Could not retain the pending invite identity');
|
|
}
|
|
return nsec;
|
|
});
|
|
|
|
/// Retire the journal only after the destination identity is durably saved.
|
|
Future<void> completePendingInvite(
|
|
String relayUrl,
|
|
String code,
|
|
Community community,
|
|
) => _pendingInvites.run(() async {
|
|
final saved = await loadAll();
|
|
if (!saved.any(
|
|
(item) => item.id == community.id && item.nsec == community.nsec,
|
|
)) {
|
|
return;
|
|
}
|
|
final key = _pendingInviteKey(relayUrl, code);
|
|
if (await _secure.read(key: key) == community.nsec) {
|
|
await _secure.delete(key: key);
|
|
}
|
|
});
|
|
|
|
Future<void> save(Community community) async {
|
|
final all = await loadAll();
|
|
final index = all.indexWhere((w) => w.id == community.id);
|
|
if (index >= 0) {
|
|
all[index] = community;
|
|
} else {
|
|
all.add(community);
|
|
}
|
|
await _saveList(all);
|
|
}
|
|
|
|
/// Replaces the complete stored community list in one secure-storage write.
|
|
Future<void> saveAll(List<Community> communities) => _saveList(communities);
|
|
|
|
Future<void> remove(String id) => _pendingInvites.run(() async {
|
|
final all = await loadAll();
|
|
final removedKeys = all
|
|
.where((item) => item.id == id)
|
|
.map((item) => item.nsec)
|
|
.whereType<String>()
|
|
.toSet();
|
|
if (removedKeys.isNotEmpty) {
|
|
// Retire every journal copy before removing the saved community. If a
|
|
// deletion fails, keep that community so sign-out can safely retry.
|
|
final records = await _secure.readAll();
|
|
for (final entry in records.entries) {
|
|
if (entry.key.startsWith(_pendingInvitePrefix) &&
|
|
removedKeys.contains(entry.value)) {
|
|
await _secure.delete(key: entry.key);
|
|
if (await _secure.read(key: entry.key) != null) {
|
|
throw StateError('Could not remove the pending invite identity');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
all.removeWhere((w) => w.id == id);
|
|
await _saveList(all);
|
|
});
|
|
|
|
Future<String?> loadActiveId() async {
|
|
return _secure.read(key: _keyActiveId);
|
|
}
|
|
|
|
Future<void> saveActiveId(String id) async {
|
|
await _secure.write(key: _keyActiveId, value: id);
|
|
}
|
|
|
|
Future<void> clearActiveId() async {
|
|
await _secure.delete(key: _keyActiveId);
|
|
}
|
|
|
|
List<Community> _decodeList(String raw) {
|
|
final list = jsonDecode(raw) as List<dynamic>;
|
|
return list
|
|
.map((entry) => Community.fromJson(entry as Map<String, dynamic>))
|
|
.toList();
|
|
}
|
|
|
|
/// Replaces the communities this build shows, keeping the others.
|
|
Future<void> _saveList(List<Community> communities) async {
|
|
final raw = await _secure.read(key: _keyCommunities);
|
|
final hidden = raw == null
|
|
? const <Community>[]
|
|
: _decodeList(raw).where((item) => !_visible(item));
|
|
final json = jsonEncode(
|
|
[...hidden, ...communities].map((item) => item.toJson()).toList(),
|
|
);
|
|
await _secure.write(key: _keyCommunities, value: json);
|
|
}
|
|
}
|