mirror of
https://github.com/block/buzz.git
synced 2026-11-24 18:21:59 +00:00
Step one of [mobile diagnostic
reporting](https://github.com/block/buzz/blob/main/docs/mobile-diagnostic-reporting.md),
second piece: the explicit error reports the spec lists, through the
reporter added in [#9613 diagnostic reporting
module](https://github.com/block/buzz/pull/9613). A build without a
destination (every build from this repository alone) still reports
nothing.
### What's in it
Each report carries its `operation` tag, the error's type and stack
trace, and no error text (the filter replaces it).
- **Event subscriber (`eventSubscriber`):** a subscriber that throws
while the relay session delivers a buffered batch is reported, and the
rest of the batch is still delivered. In a build without a destination
the error still surfaces as uncaught, so it fails the test that hit it
and prints in development, as before. Before, one throw dropped the rest
of the batch, and those events never came back because their delivery
keys were already recorded.
- **Relay frames (`relayFrame`):** a frame the socket can't decode, or
whose body isn't a JSON array, is reported instead of silently skipped.
- **Relay queries (`relayQuery`):** an HTTP query body that can't be
decoded, including the read-state snapshot.
- **Invalid-request rejections (`relayRejection`):** an `OK false` or
`CLOSED` whose reason starts with `invalid:`, reported as a fixed
`RelayInvalidRequest` with no relay text, including an `OK` with no
pending publish (a typing indicator, or a reply after a timeout).
Refusals the app handles as expected outcomes (an archived channel, an
Unarchive of a channel that is no longer archived, an open channel name
that is taken, an agent whose `policy:` refuses being added, a sole
owner's Leave refused as the last owner, a guest's Add Members refused
because only members can add people, an event whose timestamp is outside
the relay's window, so a wrong device clock does not report every
publish) are left out. The long-lived relay session, the short-lived
sockets (hosted admission, the self-hosted probe, one-shot publishes
such as agent registration and push tombstones) and the account
assertion requests made for a community's key (relay access and push
stop assertions) report frames and rejections the same way, except while
reports name a member other than the key they sign with. A removed or
inactive community's tombstone, or the probe of a community being
joined, then reports nothing rather than naming the wrong member. With
no member set (first run, or after the last community is removed) it
reports under the install identifier.
- **Account responses (`accountResponse`):** a Builderlab account
response that isn't a JSON object, or a 2xx exchange or `/v1/auth/me`
response missing its credential, subject or expiry. Non-2xx statuses are
expected outcomes and aren't reported.
- **Push gateway responses (`pushGatewayResponse`):** a malformed push
capability descriptor from the relay. A relay that leaves
`supported_extensions` out or sends none has no push and isn't reported,
and a fetch that finishes after a community switch has begun isn't
reported either, since it would name the next member. Push gateway
replies aren't reported yet.
- **Stored data (`storedData`):** a saved community list that can't be
read back.
- **Member on switch:** switching communities now looks the new member
up in storage, not in the provider's list, which can still be the
previous one while it reloads after sign-in. It reads storage before
tearing anything down, so a failed read leaves the switch unchanged and
retryable.
- **Filter:** also drops a report's additional data (`extra`). Nothing
sets it today.
- The spec's status line now says what is implemented.
- Event delivery moves from `relay_session.dart` into a part file,
unchanged apart from the subscriber guard, so the session stays under
its size limit.
### Regressions
None expected: each change only adds a report where an error was already
caught or skipped. The one behavior change is that a throwing event
subscriber no longer costs the rest of its batch.
### Validation
Added tests for each report site (relay session, socket frames, one-shot
publishes, read-state snapshot, closed-reason policy, account client,
push descriptor and capability provider, community storage), for the
relay session (member changed mid-request), one-shot sockets and
assertion requests reporting nothing while another member is set and
reporting with none set, for the member on switch and a switch whose
read fails then retries, and for the filter dropping additional data and
leaving a message-less event without one.
Full-app run on an iPhone 17 Pro simulator at b4f05ad (debug build, no
reporting destination): signed-in launch and channel list, scrolling
history in several channels, a thread, sending a message and a reaction,
search, switching communities and back, background and foreground with
live updates afterwards, and kill and relaunch with saved communities
restored. No regressions and no exceptions from the changed code.
---------
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
671 lines
24 KiB
Dart
671 lines
24 KiB
Dart
import 'dart:async';
|
||
import 'dart:convert';
|
||
import 'dart:math' show max;
|
||
|
||
import 'package:flutter/foundation.dart';
|
||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||
import 'package:http/http.dart' as http;
|
||
import 'package:buzz/shared/http/app_http_client.dart';
|
||
|
||
import '../diagnostics/diagnostic_reporter.dart';
|
||
import '../relay/relay_provider.dart';
|
||
import 'builderlab_account.dart';
|
||
import 'account_identity_store.dart';
|
||
import 'hosted_environment.dart';
|
||
import 'nip_fi_discovery.dart';
|
||
import 'organization_environment.dart';
|
||
import 'organization_key_backup.dart';
|
||
import '../push/push_bridge.dart';
|
||
|
||
final builderlabAccountProvider = Provider<BuilderlabAccountClient>((ref) {
|
||
final account = BuilderlabAccountClient(
|
||
diagnostics: ref.read(diagnosticReporterProvider),
|
||
// Hosted communities that enforce NIP-FI need the extension to mint too.
|
||
// A build without hosted relay auth still clears a copy an earlier build
|
||
// shared, so the extension cannot keep minting after sign-out.
|
||
onSessionChanged: (credential) => shareHostedSessionWithNotifications(
|
||
origin: HostedEnvironment.origin,
|
||
credential: HostedRelayAuth.configured && HostedEnvironment.enabled
|
||
? credential
|
||
: null,
|
||
relayDomains: HostedEnvironment.authDomains,
|
||
communityDomain: HostedEnvironment.communitySuffix,
|
||
),
|
||
);
|
||
ref.onDispose(account.dispose);
|
||
return account;
|
||
});
|
||
|
||
/// The organization's own account session, separate from the hosted account.
|
||
/// Null unless this build configures [OrganizationEnvironment].
|
||
final organizationAccountProvider = Provider<BuilderlabAccountClient?>((ref) {
|
||
final origin = OrganizationEnvironment.origin;
|
||
if (origin == null) return null;
|
||
final account = BuilderlabAccountClient(
|
||
accountOrigin: origin,
|
||
diagnostics: ref.read(diagnosticReporterProvider),
|
||
// The notification extension mints its own assertions for these relays.
|
||
onSessionChanged: (credential) => shareOrganizationSessionWithNotifications(
|
||
origin: origin,
|
||
credential: credential,
|
||
relayHosts: OrganizationEnvironment.relayHosts,
|
||
),
|
||
);
|
||
ref.onDispose(account.dispose);
|
||
return account;
|
||
});
|
||
|
||
/// Outlives relay-auth rebuilds, so its settled keys and in-flight attempts
|
||
/// are never duplicated. Null unless this build configures an organization.
|
||
final organizationKeyBackupProvider = Provider<OrganizationKeyBackup?>((ref) {
|
||
final organization = ref.watch(organizationAccountProvider);
|
||
return organization == null ? null : OrganizationKeyBackup(organization);
|
||
});
|
||
|
||
final hostedRelayAuthProvider = Provider<HostedRelayAuth>((ref) {
|
||
final auth = HostedRelayAuth(
|
||
ref.watch(builderlabAccountProvider),
|
||
organization: ref.watch(organizationAccountProvider),
|
||
organizationBackup: ref.watch(organizationKeyBackupProvider),
|
||
);
|
||
ref.onDispose(auth.dispose);
|
||
return auth;
|
||
});
|
||
|
||
/// Each transport owns its client lifetime; configuration and auth are shared.
|
||
final hostedHttpClientFactoryProvider =
|
||
Provider<http.Client Function({HttpTraffic traffic})>((ref) {
|
||
final auth = ref.watch(hostedRelayAuthProvider);
|
||
final config = ref.watch(relayConfigProvider);
|
||
return ({HttpTraffic traffic = HttpTraffic.api}) =>
|
||
HostedRelayHttpClient(auth, config, inner: newAppHttpClient(traffic));
|
||
});
|
||
|
||
/// The build's one Builderlab session, with a short-lived assertion for the
|
||
/// current relay/key. A public build's hosted communities use [account] when
|
||
/// [enabled] and their relay's NIP-FI discovery requires evidence; an internal
|
||
/// build's organization relays always use [organization]. Enable alongside
|
||
/// backend issuance; self-hosted communities stay usable.
|
||
class HostedRelayAuth extends ChangeNotifier {
|
||
HostedRelayAuth(
|
||
this.account, {
|
||
this.organization,
|
||
this.organizationBackup,
|
||
this.organizationRelay = OrganizationEnvironment.protects,
|
||
this.enabled = configured,
|
||
DateTime Function()? now,
|
||
Duration Function()? elapsed,
|
||
NipFiRequirementFetch? discover,
|
||
}) : _now = now ?? DateTime.now,
|
||
// Deadlines run on a monotonic clock. A test that injects only [now]
|
||
// drives both clocks with it.
|
||
_elapsed =
|
||
elapsed ??
|
||
(now == null
|
||
? _monotonic
|
||
: () => Duration(microseconds: now().microsecondsSinceEpoch)),
|
||
_discover = discover ?? _fetchRequirement;
|
||
|
||
static final _stopwatch = Stopwatch()..start();
|
||
static Duration _monotonic() => _stopwatch.elapsed;
|
||
|
||
static Future<bool> _fetchRequirement(Uri origin) async {
|
||
final client = newAppHttpClient();
|
||
try {
|
||
return await fetchNipFiRequirement(client, origin);
|
||
} finally {
|
||
client.close();
|
||
}
|
||
}
|
||
|
||
/// Whether this build turns hosted relay auth on.
|
||
static const configured = bool.fromEnvironment('BUZZ_HOSTED_RELAY_AUTH');
|
||
|
||
final BuilderlabAccountClient account;
|
||
final BuilderlabAccountClient? organization;
|
||
|
||
/// Backs up each organization relay key once its assertion has bound it.
|
||
final OrganizationKeyBackup? organizationBackup;
|
||
final bool Function(Uri relay) organizationRelay;
|
||
final bool enabled;
|
||
final DateTime Function() _now;
|
||
final Duration Function() _elapsed;
|
||
final NipFiRequirementFetch _discover;
|
||
// NIP-FI discovery per hosted relay origin: the read in flight, shared by
|
||
// concurrent callers, and the last result. The issuer refuses hosts that have
|
||
// not enabled NIP-FI, so those connect without evidence.
|
||
final _discovering = <String, Future<bool>>{};
|
||
final _required = <String, bool>{};
|
||
// One assertion per relay and key, so issuing for an inactive community (for
|
||
// example a background revocation) never evicts the active session's
|
||
// credentials that native media paths read synchronously.
|
||
final _cached =
|
||
<
|
||
({String relay, String pubkey}),
|
||
({String token, CachedReceipt receipt, BuilderlabAccount account})
|
||
>{};
|
||
// Per account client, so a stalled issuer never delays an unrelated one.
|
||
// Waiters share account restoration, but only a recorded issuance for their
|
||
// own relay and key can fail on their behalf.
|
||
final _pending =
|
||
<
|
||
BuilderlabAccountClient,
|
||
({Future<void> operation, String relay, String pubkey, bool background})
|
||
>{};
|
||
bool _disposed = false;
|
||
// Orders issuance and refusals, so a stale result never replaces a newer
|
||
// failure for another relay or key.
|
||
int _sequence = 0;
|
||
int _failureSequence = 0;
|
||
String? failedRelay;
|
||
String? failedPubkey;
|
||
AccountFailure? failure;
|
||
|
||
/// How many minutes the device clock looked off when the issuer refused
|
||
/// the current [failure], from that refusal's `Date`; null when not far
|
||
/// enough to explain it, or once the account session that was refused
|
||
/// has changed.
|
||
int? get failureClockHint =>
|
||
identical(_hintSession, _hintAccount?.account) ? _clockHint : null;
|
||
int? _clockHint;
|
||
BuilderlabAccountClient? _hintAccount;
|
||
BuilderlabAccount? _hintSession;
|
||
|
||
/// Issuance attempts per acquisition before a token that arrives with no
|
||
/// local lifetime left is reported instead of retried.
|
||
static const _acquireAttempts = 3;
|
||
|
||
bool protects(RelayConfig config) => _accountFor(config) != null;
|
||
|
||
/// Whether this relay uses the organization's session, not the hosted one.
|
||
bool organizationProtects(RelayConfig config) =>
|
||
organization != null && identical(_accountFor(config), organization);
|
||
|
||
BuilderlabAccountClient? _accountFor(RelayConfig config) {
|
||
final relay = Uri.parse(config.baseUrl);
|
||
if (organization != null && organizationRelay(relay)) {
|
||
return organization;
|
||
}
|
||
if (enabled &&
|
||
HostedEnvironment.requiresAuth(relay.host) &&
|
||
HostedEnvironment.supportsRelay(relay)) {
|
||
return account;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// 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;
|
||
final client = _accountFor(config);
|
||
if (client == null) return const {};
|
||
if (_hostedOrigin(config, client) case final origin?
|
||
when _required[origin] == false) {
|
||
return const {};
|
||
}
|
||
final pubkey = pubkeyFromNsec(config.nsec);
|
||
if (pubkey == null) return null;
|
||
final cached =
|
||
_cached[(
|
||
relay: Uri.parse(config.wsUrl).replace(path: '').toString(),
|
||
pubkey: pubkey,
|
||
)];
|
||
if (cached == null ||
|
||
!identical(cached.account, client.account) ||
|
||
!cached.receipt.usable(_elapsed(), _now())) {
|
||
return null;
|
||
}
|
||
return {'Nostr-Federated-Identity': 'Bearer ${cached.token}'};
|
||
}
|
||
|
||
/// [headers] for a request made outside [HostedRelayHttpClient], such as a
|
||
/// native download. Scoped like that client: only the relay's own origin
|
||
/// gets the assertion, so it never reaches a third-party host.
|
||
Future<Map<String, String>> headersForUrl(RelayConfig config, Uri url) async {
|
||
if (!_isRelayOrigin(config, url)) return const {};
|
||
return headers(config);
|
||
}
|
||
|
||
/// Synchronous [headersForUrl] for callers that hand headers to native code
|
||
/// up front. Uses the assertion the live relay session already holds; with
|
||
/// none cached the request goes out without it and the relay decides.
|
||
Map<String, String> cachedHeadersForUrl(RelayConfig config, Uri url) {
|
||
if (!_isRelayOrigin(config, url)) return const {};
|
||
return cachedHeaders(config) ?? const {};
|
||
}
|
||
|
||
static bool _isRelayOrigin(RelayConfig config, Uri url) =>
|
||
(url.scheme == 'http' || url.scheme == 'https') &&
|
||
// `Uri.origin` throws for a hostless URI, which a community can supply.
|
||
url.host.isNotEmpty &&
|
||
url.origin == Uri.parse(config.baseUrl).origin;
|
||
|
||
/// The assertion for [config]'s relay and key. A [background] caller (such
|
||
/// as a push revocation for a removed community) never records its failure
|
||
/// or success, so it cannot hide or clear the active session's recovery.
|
||
/// It never backs up the key or binds it to an unbound account, and its
|
||
/// assertion bypasses the shared cache: each attempt gets a fresh one, and
|
||
/// foreground use never inherits an assertion whose key was not backed up.
|
||
/// Without an assertion it gets no headers rather than a failure, so the
|
||
/// relay decides.
|
||
///
|
||
/// A hosted relay gets an assertion only when its NIP-FI discovery requires
|
||
/// one. Every caller opening a new socket passes [fresh] to reread
|
||
/// discovery, so a community that changes enforcement is picked up on its
|
||
/// next connect; HTTP requests reuse the latest discovery.
|
||
Future<Map<String, String>> headers(
|
||
RelayConfig config, {
|
||
bool background = false,
|
||
bool fresh = false,
|
||
}) async {
|
||
final client = _accountFor(config);
|
||
if (client == null) return const {};
|
||
try {
|
||
if (_hostedOrigin(config, client) case final origin?
|
||
when !await _requires(origin, fresh)) {
|
||
return const {};
|
||
}
|
||
return await _headers(config, client, background);
|
||
} on AccountFailure catch (failure) {
|
||
// Cleanup still reaches a relay that does not enforce assertions; an
|
||
// enforcing relay refuses, and the caller's durable retry tries again.
|
||
if (background) return const {};
|
||
// Without an organization session, connect as before and let the relay
|
||
// decide: an enforcing relay refuses, and sign-in is offered.
|
||
if (failure == AccountFailure.signedOut &&
|
||
identical(client, organization)) {
|
||
return const {};
|
||
}
|
||
rethrow;
|
||
}
|
||
}
|
||
|
||
/// The relay origin whose discovery decides, or null for organization relays,
|
||
/// which always use their own issuer.
|
||
String? _hostedOrigin(RelayConfig config, BuilderlabAccountClient client) =>
|
||
identical(client, organization) ? null : Uri.parse(config.baseUrl).origin;
|
||
|
||
Future<bool> _requires(String origin, bool fresh) async {
|
||
final known = _required[origin];
|
||
if (!fresh && known != null) return known;
|
||
final pending = _discovering[origin] ??= _discover(Uri.parse(origin));
|
||
try {
|
||
return _required[origin] = await pending;
|
||
} on Exception catch (error) {
|
||
// The last answer stays for requests. Without one, or for a new
|
||
// connection, retry with backoff rather than guessing.
|
||
debugPrint('NIP-FI discovery failed for $origin: $error');
|
||
throw AccountFailure.unavailable;
|
||
} finally {
|
||
if (identical(_discovering[origin], pending)) {
|
||
_discovering.remove(origin);
|
||
}
|
||
}
|
||
}
|
||
|
||
Future<Map<String, String>> _headers(
|
||
RelayConfig config,
|
||
BuilderlabAccountClient client,
|
||
bool background,
|
||
) async {
|
||
final relay = Uri.parse(config.wsUrl).replace(path: '').toString();
|
||
final pubkey = pubkeyFromNsec(config.nsec);
|
||
if (pubkey == null) throw AccountFailure.denied;
|
||
while (!_disposed) {
|
||
final cached = background
|
||
? null
|
||
: _cached[(relay: relay, pubkey: pubkey)];
|
||
if (cached != null &&
|
||
identical(cached.account, client.account) &&
|
||
cached.receipt.usable(_elapsed(), _now())) {
|
||
return {'Nostr-Federated-Identity': 'Bearer ${cached.token}'};
|
||
}
|
||
final pending = _pending[client];
|
||
if (pending != null) {
|
||
// Different communities share account restoration, never assertions.
|
||
// A background failure is never recorded, so it is never inherited.
|
||
try {
|
||
await pending.operation;
|
||
} on AccountFailure {
|
||
if (!pending.background &&
|
||
pending.relay == relay &&
|
||
pending.pubkey == pubkey &&
|
||
failedRelay == config.baseUrl &&
|
||
failedPubkey == pubkey) {
|
||
rethrow;
|
||
}
|
||
}
|
||
continue;
|
||
}
|
||
final operation = _issue(config, client, relay, pubkey, background);
|
||
_pending[client] = (
|
||
operation: operation,
|
||
relay: relay,
|
||
pubkey: pubkey,
|
||
background: background,
|
||
);
|
||
try {
|
||
final token = await operation;
|
||
if (token != null) {
|
||
return {'Nostr-Federated-Identity': 'Bearer $token'};
|
||
}
|
||
} finally {
|
||
if (identical(_pending[client]?.operation, operation)) {
|
||
_pending.remove(client);
|
||
}
|
||
}
|
||
}
|
||
throw AccountFailure.unavailable;
|
||
}
|
||
|
||
/// The issued token, or null when the account session changed meanwhile.
|
||
Future<String?> _issue(
|
||
RelayConfig config,
|
||
BuilderlabAccountClient account,
|
||
String relay,
|
||
String pubkey,
|
||
bool background,
|
||
) => BuilderlabAccountClient.observingRefusals(
|
||
(clockHint) =>
|
||
_acquire(config, account, relay, pubkey, background, clockHint),
|
||
);
|
||
|
||
// Only a refusal to this acquisition's own requests supplies [clockHint].
|
||
Future<String?> _acquire(
|
||
RelayConfig config,
|
||
BuilderlabAccountClient account,
|
||
String relay,
|
||
String pubkey,
|
||
bool background,
|
||
int? Function() clockHint,
|
||
) async {
|
||
final started = ++_sequence;
|
||
try {
|
||
if (account.account == null && await account.restore() == null) {
|
||
// A push stop pending at sign-out still gets a few attempts.
|
||
if (!background) throw AccountFailure.signedOut;
|
||
final value = await account.pushStopAssertion(relay, config.nsec!);
|
||
if (_disposed) throw AccountFailure.unavailable;
|
||
return _token(value, pubkey).token;
|
||
}
|
||
final session = account.account!;
|
||
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 && background) {
|
||
// Issuance can create a binding, and a background caller's key may be
|
||
// one the user discarded. Only foreground use chooses the identity.
|
||
throw AccountFailure.denied;
|
||
} else if (identity == null && identical(account, organization)) {
|
||
// The organization's issuer binds the proven key on first issuance;
|
||
// that binding is how a member's existing key joins the organization.
|
||
} else if (identity == null) {
|
||
// Issuance can create a binding. Reconnect must not silently give a
|
||
// different, unbound hosted account this community's saved identity.
|
||
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;
|
||
}
|
||
String? token;
|
||
late Duration receipt;
|
||
late Duration deadline;
|
||
for (var attempt = 0; token == null; attempt++) {
|
||
if (attempt == _acquireAttempts) throw AccountFailure.unavailable;
|
||
// Sampled before the request, so time in flight counts against the
|
||
// token's life.
|
||
final requestStart = _elapsed();
|
||
final value = await account.relayAssertion(
|
||
relay,
|
||
config.nsec!,
|
||
expectedSubject: session.subject,
|
||
);
|
||
final issued = _token(value, pubkey);
|
||
final iat = issuedAt(issued.token);
|
||
if (iat == null) throw AccountFailure.invalidResponse;
|
||
receipt = _elapsed();
|
||
deadline = localDeadline(
|
||
requestStart: requestStart,
|
||
receipt: receipt,
|
||
wallAtReceipt: _now().millisecondsSinceEpoch ~/ 1000,
|
||
exp: issued.expiry,
|
||
iat: iat,
|
||
);
|
||
// A token with no local lifetime left is never sent: ask again.
|
||
if (deadline > receipt) token = issued.token;
|
||
if (_disposed) throw AccountFailure.unavailable;
|
||
}
|
||
final remaining = (deadline - receipt).inSeconds;
|
||
if (!identical(session, account.account)) {
|
||
return null;
|
||
}
|
||
// A background caller may be removing this community: it must not clear
|
||
// recovery, back up a discarded key or seed the shared cache.
|
||
if (background) return token;
|
||
final wall = _now();
|
||
_cached.removeWhere((_, entry) => !entry.receipt.usable(receipt, wall));
|
||
final refreshAt =
|
||
deadline - Duration(seconds: remaining > 60 ? 30 : remaining ~/ 2);
|
||
_cached[(relay: relay, pubkey: pubkey)] = (
|
||
token: token,
|
||
receipt: CachedReceipt(receipt, wall, refreshAt - receipt),
|
||
account: session,
|
||
);
|
||
if (_clockHint != null &&
|
||
failedRelay == config.baseUrl &&
|
||
failedPubkey == pubkey) {
|
||
_clockHint = null;
|
||
notifyListeners();
|
||
}
|
||
// Issuance alone cannot prove admission after a relay refusal.
|
||
if (failure != AccountFailure.denied) connected(config);
|
||
if (identical(account, organization)) {
|
||
unawaited(organizationBackup?.afterAssertion(config.nsec!, pubkey));
|
||
}
|
||
return token;
|
||
} on AccountFailure catch (error) {
|
||
// Issuers run in parallel, so a failure that started before another
|
||
// relay's unresolved failure was recorded must not replace it.
|
||
final otherScope =
|
||
failure != null &&
|
||
(failedRelay != config.baseUrl || failedPubkey != pubkey);
|
||
if (!background &&
|
||
!_disposed &&
|
||
!(otherScope && _failureSequence > started)) {
|
||
_record(
|
||
config.baseUrl,
|
||
pubkey,
|
||
error,
|
||
started,
|
||
clockHint: error == AccountFailure.denied ? clockHint() : null,
|
||
account: account,
|
||
);
|
||
}
|
||
rethrow;
|
||
}
|
||
}
|
||
|
||
/// The `iat` claim of a compact JWS, or null when it has none or it is
|
||
/// unreadable. A NumericDate, so it may be fractional.
|
||
@visibleForTesting
|
||
static num? issuedAt(String token) {
|
||
final parts = token.split('.');
|
||
if (parts.length != 3) return null;
|
||
try {
|
||
final claims = jsonDecode(
|
||
utf8.decode(base64Url.decode(base64Url.normalize(parts[1]))),
|
||
);
|
||
final iat = claims is Map ? claims['iat'] : null;
|
||
return iat is num && iat.isFinite ? iat : null;
|
||
} on FormatException {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// An issued assertion for [pubkey], with its expiry in Unix seconds.
|
||
({String token, int expiry}) _token(
|
||
Map<String, dynamic> value,
|
||
String pubkey,
|
||
) {
|
||
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 ||
|
||
expiry <= _now().millisecondsSinceEpoch ~/ 1000) {
|
||
throw AccountFailure.invalidResponse;
|
||
}
|
||
return (token: token, expiry: expiry);
|
||
}
|
||
|
||
/// 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;
|
||
_clockHint = null;
|
||
notifyListeners();
|
||
}
|
||
|
||
void rejected(
|
||
RelayConfig config,
|
||
String? usedToken, {
|
||
bool terminal = false,
|
||
}) {
|
||
if (!protects(config) || usedToken == null) return;
|
||
_cached.removeWhere((_, entry) => 'Bearer ${entry.token}' == usedToken);
|
||
if (terminal && !_disposed) {
|
||
_record(
|
||
config.baseUrl,
|
||
pubkeyFromNsec(config.nsec),
|
||
AccountFailure.denied,
|
||
++_sequence,
|
||
);
|
||
}
|
||
}
|
||
|
||
void _record(
|
||
String relay,
|
||
String? pubkey,
|
||
AccountFailure error,
|
||
int sequence, {
|
||
int? clockHint,
|
||
BuilderlabAccountClient? account,
|
||
}) {
|
||
failedRelay = relay;
|
||
failedPubkey = pubkey;
|
||
failure = error;
|
||
_failureSequence = sequence;
|
||
_clockHint = clockHint;
|
||
_hintAccount = account;
|
||
_hintSession = account?.account;
|
||
notifyListeners();
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
_disposed = true;
|
||
_cached.clear();
|
||
_discovering.clear();
|
||
_required.clear();
|
||
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 ?? newAppHttpClient();
|
||
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();
|
||
}
|
||
|
||
/// When a cached assertion arrived, on the monotonic and the wall clock, and
|
||
/// how long it may be reused.
|
||
@visibleForTesting
|
||
class CachedReceipt {
|
||
const CachedReceipt(this.monotonic, this.wall, this.budget);
|
||
final Duration monotonic;
|
||
final DateTime wall;
|
||
final Duration budget;
|
||
|
||
/// Still reusable at [now] and [wallNow]. The monotonic clock stops while
|
||
/// the device sleeps, so the wall clock bounds it too; a wall clock set
|
||
/// back counts as no time passed.
|
||
bool usable(Duration now, DateTime wallNow) {
|
||
final byWall = wallNow.difference(wall);
|
||
final elapsed = now - monotonic;
|
||
return (byWall > elapsed ? byWall : elapsed) < budget;
|
||
}
|
||
}
|
||
|
||
/// The local monotonic deadline of an assertion requested at [requestStart]
|
||
/// and received at [receipt], with the wall clock at [wallAtReceipt] (Unix
|
||
/// seconds): the issuer's lifetime (`exp − iat`) counted from before the
|
||
/// request was sent, capped by [exp] against the local wall clock. The first
|
||
/// bound keeps a skewed phone clock from stretching the token; the second
|
||
/// keeps a short session-capped token from outliving its `exp`. A deadline
|
||
/// at or before [receipt] means no lifetime is left.
|
||
@visibleForTesting
|
||
Duration localDeadline({
|
||
required Duration requestStart,
|
||
required Duration receipt,
|
||
required int wallAtReceipt,
|
||
required int exp,
|
||
required num iat,
|
||
}) {
|
||
final byExp = receipt + Duration(seconds: max(0, exp - wallAtReceipt));
|
||
final byLifetime =
|
||
requestStart + Duration(seconds: max(0, (exp - iat).floor()));
|
||
return byLifetime < byExp ? byLifetime : byExp;
|
||
}
|