fix: nostr auth

This commit is contained in:
Amarpreet Minhas 2024-08-15 19:38:40 -04:00
parent 2fdd3da9d0
commit 42cf28fe1b

View file

@ -31,6 +31,7 @@ type nostrUser struct {
type nostrWellKnown struct {
Names map[string]string `json:"names"`
Relays map[string][]string `json:"relays,omitempty"`
NIP46 map[string][]string `json:"nip46,omitempty"`
}
func GetNostrAddr(w http.ResponseWriter, r *http.Request) {
@ -39,6 +40,9 @@ func GetNostrAddr(w http.ResponseWriter, r *http.Request) {
// get query string for username
r.ParseForm()
name := strings.ToLower(r.FormValue("name"))
if name == "_" {
name = ""
}
// normalize domain
domain, _, err := net.SplitHostPort(r.Host)
@ -76,8 +80,12 @@ func GetNostrAddr(w http.ResponseWriter, r *http.Request) {
// map of names
n := make(map[string]string)
for _, name := range names {
if name == "" {
n["_"] = user.Pubkey
} else {
n[name] = user.Pubkey
}
}
// map of relays
s := make(map[string][]string)
@ -89,9 +97,14 @@ func GetNostrAddr(w http.ResponseWriter, r *http.Request) {
}
}
// map of nip46
t := make(map[string][]string)
t[user.Pubkey] = []string{"wss://relay.devvul.com"}
ret := nostrWellKnown{
Names: n,
Relays: s,
NIP46: t,
}
j, err := json.Marshal(ret)
@ -101,7 +114,7 @@ func GetNostrAddr(w http.ResponseWriter, r *http.Request) {
return
}
l.Debug().Msgf("returning nip05 for %s@%s", name, domain)
l.Debug().Msgf("returning nip05 for %s@%s: %s", name, domain, user.Pubkey)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(j)
@ -134,7 +147,7 @@ func NewRelay(version string) *khatru.Relay {
relay.DeleteEvent = append(relay.DeleteEvent, RelayDb.DeleteEvent)
// apply policies
policies.ApplySaneDefaults(relay)
//policies.ApplySaneDefaults(relay)
relay.RejectEvent = append(
relay.RejectEvent,
@ -152,10 +165,10 @@ func NewRelay(version string) *khatru.Relay {
func RejectUnregisteredNpubs(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
l := gologger.Get(config.GetConfig().LogLevel).With().Str("context", "nostr-reject-unregistered").Logger()
authenticatedUser := khatru.GetAuthed(ctx)
if authenticatedUser == "" {
return true, fmt.Sprintf("auth-required: user (%s) not authed", event.PubKey)
// always allow auth messages
if event.Kind == 22242 {
return false, ""
}
// reject nip-04 messages to users who aren't registered
@ -185,7 +198,7 @@ func RejectUnregisteredNpubs(ctx context.Context, event *nostr.Event) (reject bo
err := DB.QueryRow("SELECT id FROM users WHERE pubkey=$1", event.PubKey).Scan(&uid)
if err != nil {
l.Debug().Msgf("kind: %v, pubkey: %s, error: %s", event.Kind, event.PubKey, err.Error())
return true, fmt.Sprintf("pubkey %s is not registered to any users", event.PubKey)
return true, fmt.Sprintf("restricted: pubkey %s is not registered to any users", event.PubKey)
}
return false, ""
}