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>
270 lines
9.0 KiB
Dart
270 lines
9.0 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||
|
||
import 'features/pairing/pairing_page.dart';
|
||
import 'organization_app_entry.dart';
|
||
import 'features/pairing/pairing_provider.dart';
|
||
import 'shared/hosted/hosted_flow_provider.dart';
|
||
import 'shared/hosted/hosted_flow.dart';
|
||
import 'shared/hosted/hosted_communities.dart';
|
||
import 'shared/hosted/use_known_communities.dart';
|
||
import 'shared/hosted/hosted_environment.dart';
|
||
import 'shared/hosted/organization_environment.dart';
|
||
import 'shared/hosted/hosted_community_creation.dart';
|
||
import 'shared/hosted/hosted_flow_problem_message.dart';
|
||
import 'shared/widgets/confirmation_dialog.dart';
|
||
|
||
/// Navigation over the existing branded welcome surface.
|
||
class OnboardingAppEntry extends HookConsumerWidget {
|
||
const OnboardingAppEntry({
|
||
super.key,
|
||
this.addingCommunity = false,
|
||
this.restoreAccount = true,
|
||
this.onExit,
|
||
this.onComplete,
|
||
});
|
||
final bool addingCommunity;
|
||
final bool restoreAccount;
|
||
final VoidCallback? onExit;
|
||
|
||
/// Continues picker restoration before the completed Add route is removed.
|
||
final VoidCallback? onComplete;
|
||
|
||
@override
|
||
Widget build(BuildContext context, WidgetRef ref) {
|
||
// Fixed per build, so the hooks below always run in the same order.
|
||
if (OrganizationEnvironment.enabled) return const OrganizationAppEntry();
|
||
final existingUser = useState(false);
|
||
final creating = useState(false);
|
||
final enteringCommunityLink = useState(false);
|
||
final flow = HostedEnvironment.enabled
|
||
? ref.watch(hostedFlowProvider)
|
||
: null;
|
||
useListenable(flow);
|
||
useListenable(flow?.account.subjectChanges);
|
||
useEffect(() {
|
||
if (flow == null || !restoreAccount) return null;
|
||
Future<void> restore() async {
|
||
await flow.initialize();
|
||
if (!context.mounted) return;
|
||
if (addingCommunity && flow.stage == HostedFlowStage.complete) {
|
||
await flow.leave();
|
||
}
|
||
if (context.mounted) await flow.signInAccount(openBrowser: false);
|
||
}
|
||
|
||
unawaited(restore());
|
||
return null;
|
||
}, [flow]);
|
||
final finishing = useRef(false);
|
||
void finishEntry() {
|
||
if (!addingCommunity || finishing.value) return;
|
||
finishing.value = true;
|
||
onComplete?.call();
|
||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||
if (!context.mounted) return;
|
||
final route = ModalRoute.of(context);
|
||
if (route != null && route.isActive) {
|
||
route.navigator?.removeRoute(route);
|
||
}
|
||
});
|
||
}
|
||
|
||
ref.listen(communityEntryCompletionProvider, (_, _) => finishEntry());
|
||
useEffect(() {
|
||
final presentation = ref.read(hostedInlineEntryProvider.notifier);
|
||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||
if (context.mounted) presentation.setActive(true);
|
||
});
|
||
void completed() {
|
||
if (flow?.stage == HostedFlowStage.complete) finishEntry();
|
||
}
|
||
|
||
flow?.addListener(completed);
|
||
return () {
|
||
flow?.removeListener(completed);
|
||
scheduleMicrotask(() {
|
||
presentation.setActive(false);
|
||
if (addingCommunity &&
|
||
onExit == null &&
|
||
flow != null &&
|
||
flow.stage != HostedFlowStage.complete) {
|
||
unawaited(flow.leave());
|
||
}
|
||
});
|
||
};
|
||
}, [flow]);
|
||
final signingIn = flow?.stage == HostedFlowStage.signIn;
|
||
final busy =
|
||
flow != null &&
|
||
flow.accountOnly &&
|
||
(flow.stage == HostedFlowStage.working ||
|
||
flow.stage == HostedFlowStage.signIn);
|
||
final account = flow?.account.account;
|
||
final connected = account != null;
|
||
final signedInLanding = connected && !addingCommunity && !signingIn;
|
||
final email = account?.email?.trim();
|
||
final hasKnownCommunities = useKnownCommunities(
|
||
accountKey: flow?.account.account?.subject,
|
||
active:
|
||
!existingUser.value &&
|
||
!enteringCommunityLink.value &&
|
||
!creating.value &&
|
||
flow?.stage == HostedFlowStage.idle &&
|
||
(ModalRoute.of(context)?.isCurrent ?? true),
|
||
load: () async =>
|
||
(await loadKnownHostedCommunities(flow!.account)).isNotEmpty,
|
||
);
|
||
final problem = flow != null && flow.accountOnly ? flow.problem : null;
|
||
final status = flow == null
|
||
? null
|
||
: problem != null
|
||
? hostedFlowProblemMessage(problem)
|
||
: busy
|
||
? null
|
||
: connected
|
||
? email != null && email.isNotEmpty
|
||
? 'Signed in as $email'
|
||
: 'Signed in to BuilderLab'
|
||
: flow.noCommunitiesToAdd
|
||
? 'No communities available to add. Scan a QR code or use an invite link.'
|
||
: null;
|
||
final confirmingSignOut = useRef(false);
|
||
Future<void> confirmSignOut() async {
|
||
if (confirmingSignOut.value) return;
|
||
confirmingSignOut.value = true;
|
||
try {
|
||
final confirmed = await showDestructiveConfirmation(
|
||
context: context,
|
||
title: 'Sign out of BuilderLab?',
|
||
message:
|
||
'${email != null && email.isNotEmpty ? 'You’re signed in as $email. ' : ''}'
|
||
'Your saved Buzz identities will stay on this device.',
|
||
confirmLabel: 'Sign out',
|
||
);
|
||
if (!context.mounted ||
|
||
!confirmed ||
|
||
flow?.account.account?.subject != account?.subject) {
|
||
return;
|
||
}
|
||
await flow?.signOutAccount();
|
||
} finally {
|
||
confirmingSignOut.value = false;
|
||
}
|
||
}
|
||
|
||
final handlingBack = useRef(false);
|
||
void back() {
|
||
final pairingStatus = ref.read(pairingProvider).status;
|
||
if (pairingStatus == PairingStatus.transferring ||
|
||
pairingStatus == PairingStatus.storing ||
|
||
pairingStatus == PairingStatus.success) {
|
||
return;
|
||
}
|
||
// Nested page and entry PopScopes receive the same system Back event.
|
||
if (handlingBack.value) return;
|
||
handlingBack.value = true;
|
||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||
handlingBack.value = false;
|
||
});
|
||
if (signedInLanding &&
|
||
!busy &&
|
||
!existingUser.value &&
|
||
!enteringCommunityLink.value &&
|
||
!creating.value) {
|
||
unawaited(confirmSignOut());
|
||
return;
|
||
}
|
||
if (!existingUser.value &&
|
||
!enteringCommunityLink.value &&
|
||
!creating.value &&
|
||
(onExit != null || addingCommunity)) {
|
||
if (onExit != null) {
|
||
onExit!();
|
||
} else {
|
||
Navigator.of(context).pop();
|
||
}
|
||
return;
|
||
}
|
||
unawaited(flow?.leave());
|
||
existingUser.value = false;
|
||
creating.value = false;
|
||
enteringCommunityLink.value = false;
|
||
FocusManager.instance.primaryFocus?.unfocus();
|
||
ref.read(pairingProvider.notifier).reset();
|
||
}
|
||
|
||
return PopScope(
|
||
canPop:
|
||
!signedInLanding &&
|
||
onExit == null &&
|
||
!existingUser.value &&
|
||
!enteringCommunityLink.value &&
|
||
!creating.value,
|
||
onPopInvokedWithResult: (didPop, _) {
|
||
if (!didPop &&
|
||
onExit != null &&
|
||
!existingUser.value &&
|
||
!enteringCommunityLink.value &&
|
||
!creating.value) {
|
||
back();
|
||
return;
|
||
}
|
||
if (!didPop &&
|
||
(signedInLanding ||
|
||
existingUser.value ||
|
||
creating.value ||
|
||
enteringCommunityLink.value)) {
|
||
back();
|
||
}
|
||
},
|
||
child: PairingPage(
|
||
connectionLanding: true,
|
||
showConnectionOptions:
|
||
!HostedEnvironment.enabled || addingCommunity || connected,
|
||
addingCommunity: addingCommunity,
|
||
signedInOnboarding: connected && !addingCommunity && !signingIn,
|
||
hasKnownCommunities: hasKnownCommunities,
|
||
existingUser: existingUser.value,
|
||
enteringCommunityLink: enteringCommunityLink.value,
|
||
creatingCommunity: creating.value,
|
||
onSubmitCommunity: flow == null
|
||
? null
|
||
: (name) {
|
||
creating.value = false;
|
||
unawaited(flow.start(HostedCreateCommunity(name)));
|
||
},
|
||
onCommunityLink: () => enteringCommunityLink.value = true,
|
||
onCreateCommunity: flow == null || busy
|
||
? null
|
||
: () => creating.value = true,
|
||
onBuilderlab: flow == null
|
||
? null
|
||
: signingIn
|
||
? () => unawaited(flow.leave())
|
||
: busy
|
||
? null
|
||
: () => unawaited(flow.signInAccount()),
|
||
onImportIdentity: flow == null || busy
|
||
? null
|
||
: () => unawaited(flow.signInAccount(importIdentity: true)),
|
||
accountStatus: status,
|
||
builderlabBusy: busy && !signingIn,
|
||
signInPending: signingIn || busy,
|
||
builderlabLabel: signingIn
|
||
? 'Cancel sign-in'
|
||
: busy
|
||
? 'Connecting…'
|
||
: connected
|
||
? 'Continue with Builderlab'
|
||
: 'Sign in with email',
|
||
onWelcomeBack: back,
|
||
onAlreadyUseBuzz: () => existingUser.value = true,
|
||
),
|
||
);
|
||
}
|
||
}
|