new flow for auth based on "auth-required: " rejection messages.

This commit is contained in:
fiatjaf
2023-12-06 12:14:58 -03:00
parent 7bfde76ab1
commit 22da06b629
5 changed files with 47 additions and 31 deletions

View File

@@ -60,15 +60,20 @@ func main() {
return false, "" // anyone else can
},
)
relay.OnConnect = append(relay.OnConnect,
func(ctx context.Context) {
// request NIP-42 AUTH from everybody
relay.RequestAuth(ctx)
// you can request auth by rejecting an event or a request with the prefix "auth-required: "
relay.RejectFilter = append(relay.RejectFilter,
func(ctx context.Context, filter nostr.Filter) (reject bool, msg string) {
if pubkey := khatru.GetAuthed(ctx); pubkey != "" {
log.Printf("request from %s\n", pubkey)
return false, ""
}
return true, "auth-required: only authenticated users can read from this relay"
},
)
relay.OnAuth = append(relay.OnAuth,
func(ctx context.Context, pubkey string) {
// and when they auth we just log that for nothing
// and when they auth we can just log that for nothing
log.Println(pubkey + " is authed!")
},
)