-
Notifications
You must be signed in to change notification settings - Fork 17
/
auth.go
58 lines (48 loc) · 1.54 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package lnurl
import (
"encoding/hex"
"errors"
"net/url"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
)
type LNURLAuthParams struct {
Tag string `json:"tag"`
K1 string `json:"k1"`
Callback string `json:"callback"`
CallbackURL *url.URL `json:"-"`
Host string `json:"host"`
}
func (_ LNURLAuthParams) LNURLKind() string { return "lnurl-auth" }
// VerifySignature takes the hex-encoded parameters passed to an lnurl-login endpoint and verifies
// the signature against the key and challenge.
func VerifySignature(k1, sig, key string) (ok bool, err error) {
bk1, err1 := hex.DecodeString(k1)
bsig, err2 := hex.DecodeString(sig)
bkey, err3 := hex.DecodeString(key)
if err1 != nil || err2 != nil || err3 != nil {
return false, errors.New("Failed to decode hex.")
}
pubkey, err := btcec.ParsePubKey(bkey)
if err != nil {
return false, errors.New("Failed to parse pubkey: " + err.Error())
}
signature, err := ecdsa.ParseDERSignature(bsig)
if err != nil {
return false, errors.New("Failed to parse signature: " + err.Error())
}
return signature.Verify(bk1, pubkey), nil
}
func HandleAuth(rawurl string, parsed *url.URL, query url.Values) (LNURLParams, error) {
k1 := query.Get("k1")
if _, err := hex.DecodeString(k1); err != nil || len(k1) != 64 {
return nil, errors.New("k1 is not a valid 32-byte hex-encoded string.")
}
return LNURLAuthParams{
Tag: "login",
K1: k1,
Callback: rawurl,
CallbackURL: parsed,
Host: parsed.Host,
}, nil
}