well-goknown/nostr/policies.go

44 lines
1.4 KiB
Go
Raw Normal View History

2024-08-17 19:18:55 +00:00
package nostr
import (
"context"
"fmt"
"git.devvul.com/asara/gologger"
"git.devvul.com/asara/well-goknown/config"
"github.com/fiatjaf/khatru"
"github.com/nbd-wtf/go-nostr"
)
func RejectUnregisteredNpubs(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
var err error
l := gologger.Get(config.GetConfig().LogLevel).With().Str("context", "nostr-reject-unregistered").Logger()
2024-08-31 13:49:47 +00:00
// always allow seals, lightning ephemeral messages, auth messages
if event.Kind == 13 || event.Kind == 21000 || event.Kind == 22242 || event.Kind == 30078 {
2024-08-17 19:18:55 +00:00
return false, ""
}
// ensure pubkey has authenticated
authenticatedUser := khatru.GetAuthed(ctx)
if authenticatedUser == "" {
2024-08-31 13:49:47 +00:00
l.Debug().Msgf("kind: %v, pubkey not authed: %s", event.Kind, event.PubKey)
2024-08-17 19:18:55 +00:00
return true, fmt.Sprintf("auth-required: interacting with this relay requires authentication")
}
npubs := []string{authenticatedUser}
2024-08-17 19:42:14 +00:00
// add recipients of dms (nip04)/private dms (nip17)/gift wraps (nip59) to npubs list
if event.Kind == 4 || event.Kind == 14 || event.Kind == 1059 {
2024-08-17 19:18:55 +00:00
for _, npub := range event.Tags.GetAll([]string{"p"}) {
npubs = append(npubs, npub.Value())
}
}
// check if npubs are registered
if authz := checknPubsInDb(npubs); authz == false {
l.Debug().Msgf("kind: %v, pubkey: %s, error: %s", event.Kind, event.PubKey, err.Error())
return true, fmt.Sprintf("restricted: pubkey %s is not registered to any users", authenticatedUser)
}
return false, ""
}