-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.go
61 lines (49 loc) · 1.38 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
59
60
61
package lnurlauth
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/fiatjaf/go-lnurl"
qrcode "github.com/skip2/go-qrcode"
)
// Authenticate authenticates a request,
// returns the user's LNUrl auth key if successful,
// returns an error otherwise.
func Authenticate(r *http.Request) (string, string, error) {
var (
k1, sig, key string
)
k1 = r.URL.Query().Get("k1")
if k1 == "" {
return "", "", errors.New("missing required key: k1")
}
sig = r.URL.Query().Get("sig")
if sig == "" {
return "", "", errors.New("missing required key: sig")
}
key = r.URL.Query().Get("key")
if key == "" {
return "", "", errors.New("missing required key: sig")
}
ok, err := lnurl.VerifySignature(k1, sig, key)
if err != nil {
return "", "", err
} else if !ok {
return "", "", errors.New("invalid signature")
}
return key, k1, nil
}
// GenerateQrCode generates a qr code from a full LNUrl.
func GenerateQrCode(lnurl string) ([]byte, error) {
return qrcode.Encode(fmt.Sprintf("lightning:%s", lnurl), qrcode.Highest, 256)
}
// GenerateLnUrl generates a LNUrl string using a callback url
func GenerateLnUrl(callbackUrl string) (string, string, error) {
generatedUrl := fmt.Sprintf("%s?k1=%s&tag=login", callbackUrl, lnurl.RandomK1())
encoded, err := lnurl.LNURLEncode(generatedUrl)
if err != nil {
return encoded, "", err
}
return strings.ToLower(encoded), generatedUrl, nil
}