pass AUTH information to functions via context.

This commit is contained in:
fiatjaf 2023-06-26 20:05:06 -03:00
parent dc594ee32b
commit 0a5624737d
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
2 changed files with 14 additions and 0 deletions

13
extra.go Normal file
View File

@ -0,0 +1,13 @@
package relayer
import "context"
const AUTH_CONTEXT_KEY = iota
func GetAuthStatus(ctx context.Context) (pubkey string, ok bool) {
authedPubkey := ctx.Value(AUTH_CONTEXT_KEY)
if authedPubkey == nil {
return "", false
}
return authedPubkey.(string), true
}

View File

@ -332,6 +332,7 @@ func (s *Server) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
}
if pubkey, ok := nip42.ValidateAuthEvent(&evt, ws.challenge, auther.ServiceURL()); ok {
ws.authed = pubkey
ctx = context.WithValue(ctx, AUTH_CONTEXT_KEY, pubkey)
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: true})
} else {
reason := "error: failed to authenticate"