fix: nostr auth

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

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) {
@ -47,6 +48,9 @@ func GetNostrAddr(w http.ResponseWriter, r *http.Request) {
}
// get owner id for nip05 request
if name == "_" {
name = ""
}
var uid int
err = DB.QueryRow("SELECT owner_id FROM nip05s WHERE name=$1 AND domain=$2", name, domain).Scan(&uid)
if err != nil {
@ -89,9 +93,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)
@ -134,7 +143,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,11 +161,6 @@ 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)
}
// reject nip-04 messages to users who aren't registered
if event.Kind == 4 {
@ -185,7 +189,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, ""
}