mirror of
https://github.com/block/buzz.git
synced 2026-11-01 15:01:12 +00:00
## Summary Make community onboarding easier to scan: shorten hosted hostname fallbacks, clarify Add Community instructions, and replace raw saved-key buttons with identity cards. Use white pages, grey cards, ghost secondary actions, standard page transitions, and a “Getting ready” screen during joining. Native profile setup keeps a horizontal modal transition. Identity cards load signed name/avatar metadata from saved communities without switching accounts; unavailable profiles use a neutral fallback. Cards show only the avatar, a 16px medium-weight name, and muted npub; community lists are omitted. Retiring profile setup closes its avatar editor too. ### Related issue Follow-up to #8687 and #8599; no duplicate PR found. ### Testing - Formatting and analysis passed. Full suite with hosted-auth defines: 4,019 passed, 5 skipped; includes profile-fetch and cancellation regressions. - Prior iOS device preview and native profile tests passed; hosted CI native Swift checks passed on the preceding head. Latest Dart fixes are not yet reinstalled on the phone. - Simulator walkthrough and owner-tested iPhone preview use sample identities and simulated admission. Sample-data previews; the identity snapshot shows the unavailable-profile fallback rendered by Flutter. Media is attached here only. | Community picker | Add Community | Saved identity | | --- | --- | --- | |  |  |  | --------- Signed-off-by: kenny lopez <klopez4212@gmail.com> Co-authored-by: Terp <5856f6900e8e0a4e0ea1f4f8d2ee33e4753d2119bc122f94ecafc660b46c0511@buzz.block.builderlab.xyz>
262 lines
8.3 KiB
Dart
262 lines
8.3 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import '../relay/relay_provider.dart';
|
|
import 'builderlab_account.dart';
|
|
import 'account_identity_store.dart';
|
|
import 'hosted_environment.dart';
|
|
|
|
final builderlabAccountProvider = Provider<BuilderlabAccountClient>((ref) {
|
|
final account = BuilderlabAccountClient();
|
|
ref.onDispose(account.dispose);
|
|
return account;
|
|
});
|
|
|
|
final hostedRelayAuthProvider = Provider<HostedRelayAuth>((ref) {
|
|
final auth = HostedRelayAuth(ref.watch(builderlabAccountProvider));
|
|
ref.onDispose(auth.dispose);
|
|
return auth;
|
|
});
|
|
|
|
/// Each transport owns its client lifetime; configuration and auth are shared.
|
|
final hostedHttpClientFactoryProvider = Provider<http.Client Function()>((ref) {
|
|
final auth = ref.watch(hostedRelayAuthProvider);
|
|
final config = ref.watch(relayConfigProvider);
|
|
return () => HostedRelayHttpClient(auth, config);
|
|
});
|
|
|
|
/// One account session, with a short-lived assertion for the current relay/key.
|
|
/// Enable alongside backend issuance; legacy and self-hosted builds stay usable.
|
|
class HostedRelayAuth extends ChangeNotifier {
|
|
HostedRelayAuth(
|
|
this.account, {
|
|
this.enabled = const bool.fromEnvironment('BUZZ_HOSTED_RELAY_AUTH'),
|
|
DateTime Function()? now,
|
|
}) : _now = now ?? DateTime.now;
|
|
|
|
final BuilderlabAccountClient account;
|
|
final bool enabled;
|
|
final DateTime Function() _now;
|
|
({
|
|
String relay,
|
|
String pubkey,
|
|
String token,
|
|
DateTime expiry,
|
|
BuilderlabAccount account,
|
|
})?
|
|
_cached;
|
|
Future<void>? _pending;
|
|
bool _disposed = false;
|
|
String? failedRelay;
|
|
String? failedPubkey;
|
|
AccountFailure? failure;
|
|
|
|
bool protects(RelayConfig config) =>
|
|
enabled &&
|
|
HostedEnvironment.requiresAuth(Uri.parse(config.baseUrl).host) &&
|
|
HostedEnvironment.supportsRelay(Uri.parse(config.baseUrl));
|
|
|
|
/// Headers for optional reads, without starting account work or changing
|
|
/// recovery state. Null means protected metadata must wait for active auth.
|
|
Map<String, String>? cachedHeaders(RelayConfig config) {
|
|
if (_disposed) return null;
|
|
if (!protects(config)) return const {};
|
|
final cached = _cached;
|
|
if (cached == null ||
|
|
cached.relay != Uri.parse(config.wsUrl).replace(path: '').toString() ||
|
|
cached.pubkey != pubkeyFromNsec(config.nsec) ||
|
|
!identical(cached.account, account.account) ||
|
|
!cached.expiry.isAfter(_now())) {
|
|
return null;
|
|
}
|
|
return {'Nostr-Federated-Identity': 'Bearer ${cached.token}'};
|
|
}
|
|
|
|
Future<Map<String, String>> headers(RelayConfig config) async {
|
|
if (!protects(config)) return const {};
|
|
final relay = Uri.parse(config.wsUrl).replace(path: '').toString();
|
|
final pubkey = pubkeyFromNsec(config.nsec);
|
|
if (pubkey == null) throw AccountFailure.denied;
|
|
while (!_disposed) {
|
|
final cached = _cached;
|
|
if (cached != null &&
|
|
cached.relay == relay &&
|
|
cached.pubkey == pubkey &&
|
|
identical(cached.account, account.account) &&
|
|
cached.expiry.isAfter(_now())) {
|
|
return {'Nostr-Federated-Identity': 'Bearer ${cached.token}'};
|
|
}
|
|
final pending = _pending;
|
|
if (pending != null) {
|
|
// Different communities share account restoration, never assertions.
|
|
try {
|
|
await pending;
|
|
} on AccountFailure {
|
|
if (failedRelay == config.baseUrl && failedPubkey == pubkey) rethrow;
|
|
}
|
|
continue;
|
|
}
|
|
final operation = _issue(config, relay, pubkey);
|
|
_pending = operation;
|
|
try {
|
|
await operation;
|
|
} finally {
|
|
_pending = null;
|
|
}
|
|
}
|
|
throw AccountFailure.unavailable;
|
|
}
|
|
|
|
Future<void> _issue(RelayConfig config, String relay, String pubkey) async {
|
|
try {
|
|
if (account.account == null && await account.restore() == null) {
|
|
throw AccountFailure.signedOut;
|
|
}
|
|
final session = account.account!;
|
|
// Issuance can create a binding. Reconnect must not silently give a
|
|
// different, unbound account this community's saved identity.
|
|
final current = await account.currentIdentity();
|
|
if (!identical(session, account.account)) throw AccountFailure.denied;
|
|
final identity = current['identity'];
|
|
if (current['error'] != null) throw AccountFailure.invalidResponse;
|
|
if (identity == null) {
|
|
final AccountIdentitySnapshot? saved;
|
|
try {
|
|
saved = await AccountIdentityStore()
|
|
.forSubject(session.subject)
|
|
.read();
|
|
} on AccountIdentityFailure {
|
|
throw AccountFailure.storage;
|
|
}
|
|
if (saved?.pubkey != pubkey ||
|
|
!saved!.hasBackupFor(
|
|
backend: BuilderlabAccountClient.origin.origin,
|
|
subject: session.subject,
|
|
)) {
|
|
throw AccountFailure.denied;
|
|
}
|
|
} else if (identity is! Map || identity['pubkey_hex'] != pubkey) {
|
|
throw AccountFailure.denied;
|
|
}
|
|
final value = await account.relayAssertion(
|
|
relay,
|
|
config.nsec!,
|
|
expectedSubject: session.subject,
|
|
);
|
|
final token = value['assertion'];
|
|
final expiry = value['expires_at'];
|
|
if (token is! String ||
|
|
token.isEmpty ||
|
|
token.length > 16384 ||
|
|
token.contains(RegExp(r'[\r\n]')) ||
|
|
value['nostr_pubkey'] != pubkey ||
|
|
expiry is! int) {
|
|
throw AccountFailure.invalidResponse;
|
|
}
|
|
final now = _now().millisecondsSinceEpoch ~/ 1000;
|
|
if (expiry <= now || expiry > now + 300) {
|
|
throw AccountFailure.invalidResponse;
|
|
}
|
|
final expires = DateTime.fromMillisecondsSinceEpoch(
|
|
expiry * 1000,
|
|
isUtc: true,
|
|
);
|
|
if (_disposed) throw AccountFailure.unavailable;
|
|
if (!identical(session, account.account)) {
|
|
return;
|
|
}
|
|
_cached = (
|
|
relay: relay,
|
|
pubkey: pubkey,
|
|
token: token,
|
|
expiry: expires.subtract(
|
|
Duration(seconds: (expiry - now) > 60 ? 30 : (expiry - now) ~/ 2),
|
|
),
|
|
account: session,
|
|
);
|
|
// Issuance alone cannot prove admission after a relay refusal.
|
|
if (failure != AccountFailure.denied) connected(config);
|
|
} on AccountFailure catch (error) {
|
|
if (!_disposed) {
|
|
failedRelay = config.baseUrl;
|
|
failedPubkey = pubkey;
|
|
failure = error;
|
|
notifyListeners();
|
|
}
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
/// Clear a scoped failure only after this community actually reconnects.
|
|
void connected(RelayConfig config) {
|
|
if (_disposed || (failedRelay != null && failedRelay != config.baseUrl)) {
|
|
return;
|
|
}
|
|
failure = null;
|
|
failedRelay = null;
|
|
failedPubkey = null;
|
|
notifyListeners();
|
|
}
|
|
|
|
void rejected(
|
|
RelayConfig config,
|
|
String? usedToken, {
|
|
bool terminal = false,
|
|
}) {
|
|
if (!protects(config) || usedToken == null) return;
|
|
if ('Bearer ${_cached?.token}' == usedToken) _cached = null;
|
|
if (terminal && !_disposed) {
|
|
failedRelay = config.baseUrl;
|
|
failedPubkey = pubkeyFromNsec(config.nsec);
|
|
failure = AccountFailure.denied;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_disposed = true;
|
|
_cached = null;
|
|
super.dispose();
|
|
}
|
|
}
|
|
|
|
/// Captures the signing context with the request. Never follows redirects with
|
|
/// credentials or replays a failed write automatically.
|
|
class HostedRelayHttpClient extends http.BaseClient {
|
|
HostedRelayHttpClient(
|
|
this.auth,
|
|
this.config, {
|
|
http.Client? inner,
|
|
this.canSend,
|
|
}) : _inner = inner ?? http.Client();
|
|
final HostedRelayAuth auth;
|
|
final RelayConfig config;
|
|
final http.Client _inner;
|
|
final bool Function()? canSend;
|
|
|
|
@override
|
|
Future<http.StreamedResponse> send(http.BaseRequest request) async {
|
|
final origin = Uri.parse(config.baseUrl);
|
|
final scoped = request.url.origin == origin.origin && auth.protects(config);
|
|
if (scoped) {
|
|
request.followRedirects = false;
|
|
request.headers.addAll(await auth.headers(config));
|
|
}
|
|
if (canSend != null && !canSend!()) {
|
|
throw StateError('Relay request expired');
|
|
}
|
|
final response = await _inner.send(request);
|
|
if (scoped && (response.statusCode == 401 || response.statusCode == 403)) {
|
|
auth.rejected(config, request.headers['Nostr-Federated-Identity']);
|
|
}
|
|
return response;
|
|
}
|
|
|
|
@override
|
|
void close() => _inner.close();
|
|
}
|