From 5b17786273dac6d227c8c7f2c9f1bdfa74dd1894 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 25 Dec 2023 09:30:13 -0300 Subject: [PATCH] bring back RequestAuth(ctx), now as a global. --- README.md | 2 +- handlers.go | 4 ++-- utils.go | 5 +++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bcf166a..4b88d5d 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ func main() { relay.OnConnect = append(relay.OnConnect, func(ctx context.Context) { // request NIP-42 AUTH from everybody - relay.RequestAuth(ctx) + khatru.RequestAuth(ctx) }, ) relay.OnAuth = append(relay.OnAuth, diff --git a/handlers.go b/handlers.go index 13d27eb..c36c6d7 100644 --- a/handlers.go +++ b/handlers.go @@ -150,7 +150,7 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) { } else { reason = writeErr.Error() if strings.HasPrefix(reason, "auth-required:") { - ws.WriteJSON(nostr.AuthEnvelope{Challenge: &ws.Challenge}) + RequestAuth(ctx) } } ws.WriteJSON(nostr.OKEnvelope{EventID: env.Event.ID, OK: ok, Reason: reason}) @@ -181,7 +181,7 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) { // fail everything if any filter is rejected reason := err.Error() if strings.HasPrefix(reason, "auth-required:") { - ws.WriteJSON(nostr.AuthEnvelope{Challenge: &ws.Challenge}) + RequestAuth(ctx) } ws.WriteJSON(nostr.ClosedEnvelope{SubscriptionID: env.SubscriptionID, Reason: reason}) cancelReqCtx(errors.New("filter rejected")) diff --git a/utils.go b/utils.go index 2bbab86..7681ac0 100644 --- a/utils.go +++ b/utils.go @@ -12,6 +12,11 @@ const ( subscriptionIdKey ) +func RequestAuth(ctx context.Context) { + ws := GetConnection(ctx) + ws.WriteJSON(nostr.AuthEnvelope{Challenge: &ws.Challenge}) +} + func GetConnection(ctx context.Context) *WebSocket { return ctx.Value(wsKey).(*WebSocket) }