Skip to content

Commit

Permalink
implement nip-42 AUTH event validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Jan 16, 2023
1 parent a37ffac commit 9775016
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions nip42/nip42.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package nip42

import (
"net/url"
"time"

"github.com/nbd-wtf/go-nostr"
)

func ValidateAuthEvent(event *nostr.Event, challenge string, relayURL string) (pubkey string, ok bool) {
if ok, _ := event.CheckSignature(); ok == false {
return "", false
}
if event.Kind != 22242 {
return "", false
}

now := time.Now()
if event.CreatedAt.After(now.Add(10*time.Minute)) || event.CreatedAt.Before(now.Add(-10*time.Minute)) {
return "", false
}

if event.Tags.GetFirst([]string{"challenge", challenge}) == nil {
return "", false
}

expected, err1 := url.Parse(relayURL)
found, err2 := url.Parse(event.Tags.GetFirst([]string{"relay", ""}).Value())
if err1 != nil || err2 != nil {
return "", false
} else {
if expected.Scheme != found.Scheme ||
expected.Host != found.Host ||
expected.Path != found.Path {
return "", false
}
}

return event.PubKey, true
}

1 comment on commit 9775016

@barkyq
Copy link
Contributor

@barkyq barkyq commented on 9775016 Jan 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool!

Please sign in to comment.