bring back RequestAuth(ctx), now as a global.

This commit is contained in:
fiatjaf 2023-12-25 09:30:13 -03:00
parent 77600dc05c
commit 5b17786273
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
3 changed files with 8 additions and 3 deletions

View File

@ -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,

View File

@ -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"))

View File

@ -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)
}