Skip to content

Commit

Permalink
pass AUTH information to functions via context.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Jun 26, 2023
1 parent dc594ee commit 0a56247
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
13 changes: 13 additions & 0 deletions extra.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 0a56247

Please sign in to comment.