@@ -334,34 +334,56 @@ impl InnerLocalRelay {
334
334
}
335
335
}
336
336
337
- // Check NIP42
338
- if let Some ( nip42) = & self . nip42 {
339
- // TODO: check if public key allowed
337
+ // Check if it's configured to require NIP42 authentication for writing
338
+ let require_nip42_auth: bool = match & self . nip42 {
339
+ Some ( nip42) => nip42. mode . is_write ( ) ,
340
+ None => false ,
341
+ } ;
340
342
341
- // Check mode and if it's authenticated
342
- if nip42. mode . is_write ( ) && !session. nip42 . is_authenticated ( ) {
343
- // Generate and send AUTH challenge
344
- send_msg (
345
- ws_tx,
346
- RelayMessage :: Auth {
347
- challenge : Cow :: Owned ( session. nip42 . generate_challenge ( ) ) ,
348
- } ,
349
- )
350
- . await ?;
343
+ // Check if it's a protected event
344
+ let is_protected: bool = event. is_protected ( ) ;
351
345
352
- // Return error
353
- return send_msg (
346
+ // Check if authentication is required
347
+ if ( require_nip42_auth || is_protected) && !session. nip42 . is_authenticated ( ) {
348
+ // Generate and send AUTH challenge
349
+ send_msg (
350
+ ws_tx,
351
+ RelayMessage :: Auth {
352
+ challenge : Cow :: Owned ( session. nip42 . generate_challenge ( ) ) ,
353
+ } ,
354
+ ) . await ?;
355
+
356
+ // Return error
357
+ return send_msg (
358
+ ws_tx,
359
+ RelayMessage :: Ok {
360
+ event_id : event. id ,
361
+ status : false ,
362
+ message : Cow :: Owned ( format ! (
363
+ "{}: you must auth" ,
364
+ MachineReadablePrefix :: AuthRequired
365
+ ) ) ,
366
+ } ,
367
+ ) . await ;
368
+ }
369
+
370
+ if is_protected {
371
+ if let Some ( authenticated_public_key) = & session. nip42 . public_key {
372
+ // Block if the event author not matches the authenticated public key
373
+ if event. pubkey != * authenticated_public_key {
374
+ return send_msg (
354
375
ws_tx,
355
376
RelayMessage :: Ok {
356
377
event_id : event. id ,
357
378
status : false ,
358
379
message : Cow :: Owned ( format ! (
359
- "{}: you must auth " ,
360
- MachineReadablePrefix :: AuthRequired
380
+ "{}: this event may only be published by its author " ,
381
+ MachineReadablePrefix :: Blocked
361
382
) ) ,
362
383
} ,
363
384
)
364
- . await ;
385
+ . await ;
386
+ }
365
387
}
366
388
}
367
389
0 commit comments