-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement nip-42 AUTH event validation.
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
9775016
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool!