@@ -8,6 +8,7 @@ import '../relay/relay_provider.dart';
88import 'builderlab_account.dart' ;
99import 'account_identity_store.dart' ;
1010import 'hosted_environment.dart' ;
11+ import 'nip_fi_discovery.dart' ;
1112import 'organization_environment.dart' ;
1213import 'organization_key_backup.dart' ;
1314import '../push/push_bridge.dart' ;
@@ -61,9 +62,10 @@ final hostedHttpClientFactoryProvider = Provider<http.Client Function()>((ref) {
6162});
6263
6364/// One account session per realm, with a short-lived assertion for the current
64- /// relay/key. Hosted communities use [account] when [enabled] ; the build's
65- /// organization relays use [organization] . Enable alongside backend issuance;
66- /// legacy and self-hosted builds stay usable.
65+ /// relay/key. Hosted communities use [account] when [enabled] and their relay's
66+ /// NIP-FI discovery requires evidence; the build's organization relays always
67+ /// use [organization] . Enable alongside backend issuance; legacy and
68+ /// self-hosted builds stay usable.
6769class HostedRelayAuth extends ChangeNotifier {
6870 HostedRelayAuth (
6971 this .account, {
@@ -72,7 +74,18 @@ class HostedRelayAuth extends ChangeNotifier {
7274 this .organizationRelay = OrganizationEnvironment .protects,
7375 this .enabled = const bool .fromEnvironment ('BUZZ_HOSTED_RELAY_AUTH' ),
7476 DateTime Function ()? now,
75- }) : _now = now ?? DateTime .now;
77+ NipFiRequirementFetch ? discover,
78+ }) : _now = now ?? DateTime .now,
79+ _discover = discover ?? _fetchRequirement;
80+
81+ static Future <bool > _fetchRequirement (Uri origin) async {
82+ final client = http.Client ();
83+ try {
84+ return await fetchNipFiRequirement (client, origin);
85+ } finally {
86+ client.close ();
87+ }
88+ }
7689
7790 final BuilderlabAccountClient account;
7891 final BuilderlabAccountClient ? organization;
@@ -82,6 +95,12 @@ class HostedRelayAuth extends ChangeNotifier {
8295 final bool Function (Uri relay) organizationRelay;
8396 final bool enabled;
8497 final DateTime Function () _now;
98+ final NipFiRequirementFetch _discover;
99+ // NIP-FI discovery per hosted relay origin: the read in flight, shared by
100+ // concurrent callers, and the last result. The issuer refuses hosts that have
101+ // not enabled NIP-FI, so those connect without evidence.
102+ final _discovering = < String , Future <bool >> {};
103+ final _required = < String , bool > {};
85104 // One assertion per relay and key, so issuing for an inactive community (for
86105 // example a background revocation) never evicts the active session's
87106 // credentials that native media paths read synchronously.
@@ -130,7 +149,12 @@ class HostedRelayAuth extends ChangeNotifier {
130149 /// recovery state. Null means protected metadata must wait for active auth.
131150 Map <String , String >? cachedHeaders (RelayConfig config) {
132151 if (_disposed) return null ;
133- if (! protects (config)) return const {};
152+ final client = _accountFor (config);
153+ if (client == null ) return const {};
154+ if (_hostedOrigin (config, client) case final origin?
155+ when _required[origin] == false ) {
156+ return const {};
157+ }
134158 final pubkey = pubkeyFromNsec (config.nsec);
135159 if (pubkey == null ) return null ;
136160 final cached =
@@ -139,7 +163,7 @@ class HostedRelayAuth extends ChangeNotifier {
139163 pubkey: pubkey,
140164 )];
141165 if (cached == null ||
142- ! identical (cached.account, _accountFor (config) ? .account) ||
166+ ! identical (cached.account, client .account) ||
143167 ! cached.expiry.isAfter (_now ())) {
144168 return null ;
145169 }
@@ -176,13 +200,23 @@ class HostedRelayAuth extends ChangeNotifier {
176200 /// foreground use never inherits an assertion whose key was not backed up.
177201 /// Without an assertion it gets no headers rather than a failure, so the
178202 /// relay decides.
203+ ///
204+ /// A hosted relay gets an assertion only when its NIP-FI discovery requires
205+ /// one. Every caller opening a new socket passes [fresh] to reread
206+ /// discovery, so a community that changes enforcement is picked up on its
207+ /// next connect; HTTP requests reuse the latest discovery.
179208 Future <Map <String , String >> headers (
180209 RelayConfig config, {
181210 bool background = false ,
211+ bool fresh = false ,
182212 }) async {
183213 final client = _accountFor (config);
184214 if (client == null ) return const {};
185215 try {
216+ if (_hostedOrigin (config, client) case final origin?
217+ when ! await _requires (origin, fresh)) {
218+ return const {};
219+ }
186220 return await _headers (config, client, background);
187221 } on AccountFailure catch (failure) {
188222 // Cleanup still reaches a relay that does not enforce assertions; an
@@ -198,6 +232,29 @@ class HostedRelayAuth extends ChangeNotifier {
198232 }
199233 }
200234
235+ /// The relay origin whose discovery decides, or null for organization relays,
236+ /// which always use their own issuer.
237+ String ? _hostedOrigin (RelayConfig config, BuilderlabAccountClient client) =>
238+ identical (client, organization) ? null : Uri .parse (config.baseUrl).origin;
239+
240+ Future <bool > _requires (String origin, bool fresh) async {
241+ final known = _required[origin];
242+ if (! fresh && known != null ) return known;
243+ final pending = _discovering[origin] ?? = _discover (Uri .parse (origin));
244+ try {
245+ return _required[origin] = await pending;
246+ } on Exception catch (error) {
247+ // The last answer stays for requests. Without one, or for a new
248+ // connection, retry with backoff rather than guessing.
249+ debugPrint ('NIP-FI discovery failed for $origin : $error ' );
250+ throw AccountFailure .unavailable;
251+ } finally {
252+ if (identical (_discovering[origin], pending)) {
253+ _discovering.remove (origin);
254+ }
255+ }
256+ }
257+
201258 Future <Map <String , String >> _headers (
202259 RelayConfig config,
203260 BuilderlabAccountClient client,
@@ -403,6 +460,8 @@ class HostedRelayAuth extends ChangeNotifier {
403460 void dispose () {
404461 _disposed = true ;
405462 _cached.clear ();
463+ _discovering.clear ();
464+ _required.clear ();
406465 super .dispose ();
407466 }
408467}
0 commit comments