Merge pull request #130 from fiatjaf/fix-auth

fix auth
This commit is contained in:
mattn
2025-03-28 22:51:57 +09:00
committed by GitHub
2 changed files with 8 additions and 4 deletions

View File

@@ -5,9 +5,12 @@ 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 {
value := ctx.Value(AUTH_CONTEXT_KEY)
if value == nil {
return "", false
}
return authedPubkey.(string), true
if ws, ok := value.(*WebSocket); ok {
return ws.authed, true
}
return "", false
}

View File

@@ -293,7 +293,6 @@ func (s *Server) doAuth(ctx context.Context, ws *WebSocket, request []json.RawMe
}
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 {
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: "error: failed to authenticate"})
@@ -324,6 +323,8 @@ func (s *Server) handleMessage(ctx context.Context, ws *WebSocket, message []byt
var typ string
json.Unmarshal(request[0], &typ)
ctx = context.WithValue(ctx, AUTH_CONTEXT_KEY, ws)
switch typ {
case "EVENT":
notice = s.doEvent(ctx, ws, request, store)