mirror of
https://github.com/block/buzz.git
synced 2026-11-12 20:31:12 +00:00
Block is turning on NIP-FI enforcement on the Block relay. Desktop gets organization sign-in in block/buzz-app#1424. Mobile can already attach NIP-FI assertions, but only for Builderlab-hosted communities, through the build's single Builderlab account. The dogfood build needs that account for public hosted communities, so it can't simply be pointed at the Block tenant. This adds a separate **organization realm**, configured at build time: - `BUZZ_ORGANIZATION_URL`: the tenant's HTTPS origin (for example `https://block.builderlab.xyz`). - `BUZZ_ORGANIZATION_RELAYS`: the exact relay hosts that trust that tenant. The two must be set together. The app refuses to start if they're malformed, overlap a hosted community, or reuse the hosted origin. Without them, nothing changes. For a configured relay: - The app keeps its own organization session and signs in through the browser at the tenant's existing login (`/api/goose/v1/auth/login`). Block SSO happens there. - It requests assertions from the same `/v1/buzz/identity/assertions` route that hosted communities use, and attaches them to the relay socket, HTTP, media, huddle and remote-agent requests. - Unlike the hosted path, it doesn't require an identity backup before the first binding. As on desktop, the tenant binds the member's existing key on first issuance. A key already bound to a different one is refused before any request is sent. - When signed out, it connects without an assertion and lets the relay decide. A bottom banner offers **Sign in**. After sign-in, the relay reconnects with a fresh assertion. Hosted communities keep their existing banner and flow. The tenant accepts only the registered `xyz.block.buzz.mobile:/auth/callback` return URL, so both realms share it. Each `BrowserAuth` owner leaves callbacks whose `state` belongs to another owner's attempt to that owner. ### Before merge - [ ] Kenny Lopez reviews the visible changes. ### Validation Added tests (`organization_relay_auth_test.dart`): - Configuration parsing and exact host matching. - An assertion is issued for the exact relay and key, then cached. - A different bound key is refused. - Signed out connects without an assertion. - Other relays never receive the assertion. - The shared callback is routed between the hosted and organization owners. Full `mobile-check` and `mobile-test` pass. RunnerTests pass locally except `testNativeNavigationTitleExpandsAgainAtTop`, which this PR doesn't touch. Added tests (`organization_sign_in_test.dart`, `relay_auth_isolation_test.dart`, `builderlab_account_test.dart`), each failing without its fix: - Concurrent account restores at cold start share one adopted account. - An exchange from a cancelled attempt never changes a newer sign-in. - Storage failures while clearing the intent or cancelling leave the busy state. - An unreadable saved organization session is replaced by signing in again. - A stalled organization issuer never delays a hosted relay's assertion. This test needs `BUZZ_AUTH_DOMAINS` and skips without it. CI's configured-auth-domains step doesn't list it yet, because that workflow change needs a token with `workflow` scope. Added tests also cover a restore after an offline start (the banner clears once a relay retry restores the session) and the iOS bridge allowlist (`/auth/native` and tenant login on Builderlab subdomains; other hosts, paths, ports, userinfo and fragments refused). Simulator (iPhone 17, iOS 27) with the dogfood defines against the production Block relay `buzz.block.builderlab.xyz`, on a WARP Mac: - The app starts with the organization configuration and connects to the relay while signed out, showing the organization **Sign in** banner. - **Sign in** opens the system sign-in session for `block.builderlab.xyz`, and the banner shows **Finishing organization sign-in…**. Cancelling returns to **Sign in** with no error. Before the bridge fix, no browser opened and the banner reported that sign-in did not finish. - The bottom banners (organization sign-in, hosted reconnect, and the sign-in recovery row) now sit above Home's floating Search/New bar, which used to cover **Sign in**. A journey test checks the banner clears the bar, and fails without the fix.  - The simulator run stopped at the system consent prompt (SSO does not work in the simulator). The app calls were driven through the VM service (the same `signIn()`/`cancel()` the banner buttons call) because the simulator could not be tapped. Device (iPhone 12 mini) against bb-block staging in enforce mode, where every probe without an assertion gets 401: - Signed out, the organization banner shows and the app stays responsive. Cancel at the consent prompt returns to **Sign in**, and retry works. - Block SSO through Okta completes, and the relay admits the reconnect with the assertion. - After relaunch the app connects, loads channel history, and shows no banner for over 60 seconds. - Before the restore fix, 2 of 4 cold starts showed "cannot use this community with this key" although the session was valid. With it, 8 of 8 cold starts connected. Known limitation: adding an enforcing organization relay by community link is tracked in #8791. The dogfood build enables this in squareup/buzz-releases (`tomb/dogfood-organization-sign-in`). App Store builds stay unchanged. 🤖 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: Claude Opus 5.5 <noreply@anthropic.com>
116 lines
4.2 KiB
Dart
116 lines
4.2 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import 'package:buzz/shared/hosted/builderlab_account.dart';
|
|
import 'package:buzz/shared/hosted/hosted_environment.dart';
|
|
import 'package:buzz/shared/hosted/hosted_relay_auth.dart';
|
|
import 'package:buzz/shared/hosted/organization_environment.dart';
|
|
import 'package:buzz/shared/relay/relay_provider.dart';
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:http/testing.dart';
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
final keys = nostr.Keys(
|
|
'0000000000000000000000000000000000000000000000000000000000000001',
|
|
);
|
|
final organization = Uri.parse('https://block.example.com');
|
|
const relayHost = 'buzz.block.example.com';
|
|
const orgSessionKey = 'buzz_hosted_session_block_example_com_v1';
|
|
final expiry = DateTime.now().toUtc().add(const Duration(days: 1));
|
|
|
|
setUp(() => FlutterSecureStorage.setMockInitialValues({}));
|
|
|
|
http.Response me() => http.Response(
|
|
jsonEncode({'subject': 'member', 'expires_at': expiry.toIso8601String()}),
|
|
200,
|
|
);
|
|
|
|
test(
|
|
'a stalled organization issuer never delays a hosted relay',
|
|
() async {
|
|
final hostedRelay = RelayConfig(
|
|
baseUrl: 'https://team.communities.buzz.xyz',
|
|
nsec: keys.nsec,
|
|
);
|
|
final orgRelay = RelayConfig(
|
|
baseUrl: 'https://$relayHost',
|
|
nsec: keys.nsec,
|
|
);
|
|
for (final (origin, key) in [
|
|
(organization, orgSessionKey),
|
|
(
|
|
BuilderlabAccountClient.origin,
|
|
'buzz_hosted_session_app_builderlab_xyz_v1',
|
|
),
|
|
]) {
|
|
await const FlutterSecureStorage().write(
|
|
key: key,
|
|
value: jsonEncode({
|
|
'origin': origin.toString(),
|
|
'credential': 'secret',
|
|
'subject': 'member',
|
|
'expires_at': expiry.toIso8601String(),
|
|
}),
|
|
);
|
|
}
|
|
final stalled = Completer<http.Response>();
|
|
BuilderlabAccountClient client({required bool stall}) =>
|
|
BuilderlabAccountClient(
|
|
accountOrigin: stall ? organization : null,
|
|
httpClient: MockClient((request) async {
|
|
return switch (request.url.path) {
|
|
'/api/goose/v1/auth/me' => me(),
|
|
'/api/goose/v1/buzz/nostr-identities/current' => http.Response(
|
|
jsonEncode({
|
|
'identity': {'pubkey_hex': keys.public},
|
|
}),
|
|
200,
|
|
),
|
|
'/api/goose/v1/buzz/identity/assertions' =>
|
|
stall
|
|
? stalled.future
|
|
: http.Response(
|
|
jsonEncode({
|
|
'assertion': 'hosted-token',
|
|
'nostr_pubkey': keys.public,
|
|
'expires_at':
|
|
DateTime.now().millisecondsSinceEpoch ~/ 1000 +
|
|
120,
|
|
}),
|
|
200,
|
|
),
|
|
_ => http.Response('{}', 404),
|
|
};
|
|
}),
|
|
);
|
|
final auth = HostedRelayAuth(
|
|
client(stall: false),
|
|
organization: client(stall: true),
|
|
organizationRelay: (relay) =>
|
|
OrganizationEnvironment.matches(relay, [relayHost]),
|
|
enabled: true,
|
|
);
|
|
final orgHeaders = auth.headers(orgRelay);
|
|
await pumpEventQueue();
|
|
expect(
|
|
await auth.headers(hostedRelay).timeout(const Duration(seconds: 2)),
|
|
{'Nostr-Federated-Identity': 'Bearer hosted-token'},
|
|
);
|
|
// The hosted relay then refuses this key.
|
|
auth.rejected(hostedRelay, 'Bearer hosted-token', terminal: true);
|
|
stalled.complete(http.Response('{}', 503));
|
|
await expectLater(orgHeaders, throwsA(isA<AccountFailure>()));
|
|
// The stale organization failure never hides the hosted recovery.
|
|
expect(auth.failedRelay, hostedRelay.baseUrl);
|
|
expect(auth.failure, AccountFailure.denied);
|
|
auth.dispose();
|
|
},
|
|
// Hosted relays are protected only in builds with authentication domains.
|
|
skip: !HostedEnvironment.enabled,
|
|
);
|
|
}
|