-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverifier.go
67 lines (54 loc) · 1.2 KB
/
verifier.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
62
63
64
65
66
67
package web3auth
import (
"encoding/base64"
"errors"
"strconv"
"strings"
"time"
"github.com/samyfodil/web3auth/key"
ethereum "github.com/taubyte/go-sdk/ethereum/client"
)
type verifier struct {
pk key.Key
}
func newVerifier(pk key.Key) Verifier {
return &verifier{
pk: pk,
}
}
func (v *verifier) Validate(token string) error {
bytes, err := base64.StdEncoding.DecodeString(token)
if err != nil {
return err
}
token = string(bytes)
tokenElms := make(map[string]string)
for _, elm := range strings.Split(token, ";") {
tkns := strings.Split(elm, "=")
if len(tkns) != 2 {
continue
}
attr := tkns[0]
value := tkns[1]
tokenElms[attr] = value
}
tokenInfo := "addr=" + tokenElms["addr"] + ";expire=" + tokenElms["expire"]
message := []byte("\x19Ethereum Signed Message:\n" + strconv.Itoa(len(tokenInfo)) + tokenInfo)
sig, err := parseSignature(tokenElms["sign"])
if err != nil {
return err
}
err = ethereum.VerifySignature(message, v.pk.Bytes(), sig)
if err != nil {
return err
}
// parse expire
expire, err := strconv.ParseInt(tokenElms["expire"], 10, 64)
if err != nil {
return err
}
if expire < time.Now().Unix() {
return errors.New("Token expired")
}
return nil
}