-
Notifications
You must be signed in to change notification settings - Fork 13
/
pin.go
75 lines (60 loc) · 1.59 KB
/
pin.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
68
69
70
71
72
73
74
75
package mixin
import (
"context"
"crypto/ed25519"
"encoding/binary"
"encoding/hex"
"fmt"
"regexp"
"time"
)
func (c *Client) VerifyPin(ctx context.Context, pin string) error {
body := map[string]interface{}{}
if len(pin) > 6 {
timestamp := uint64(time.Now().UnixNano())
tipBody := []byte(fmt.Sprintf("%s%032d", TIPVerify, timestamp))
body["timestamp"] = timestamp
if pinBts, err := hex.DecodeString(pin); err == nil {
switch len(pinBts) {
case ed25519.PrivateKeySize:
body["pin_base64"] = c.EncryptPin(hex.EncodeToString(ed25519.Sign(pinBts, tipBody)))
case 32:
var key Key
copy(key[:], pinBts)
body["pin_base64"] = c.EncryptPin(key.Sign(tipBody).String())
}
}
}
if _, ok := body["pin_base64"]; !ok {
body["pin"] = c.EncryptPin(pin)
}
return c.Post(ctx, "/pin/verify", body, nil)
}
func (c *Client) ModifyPin(ctx context.Context, pin, newPin string) error {
body := map[string]interface{}{}
if pin != "" {
body["old_pin"] = c.EncryptPin(pin)
}
if len(newPin) > 6 {
counter := make([]byte, 8)
binary.BigEndian.PutUint64(counter, 1)
newPin = newPin + hex.EncodeToString(counter)
}
body["pin"] = c.EncryptPin(newPin)
return c.Post(ctx, "/pin/update", body, nil)
}
var (
pinRegex = regexp.MustCompile(`^\d{6}$`)
)
// ValidatePinPattern validate the pin with pinRegex
func ValidatePinPattern(pin string) error {
if len(pin) > 6 {
if pinBts, err := hex.DecodeString(pin); err == nil && len(pinBts) == 32 {
return nil
}
}
if !pinRegex.MatchString(pin) {
return fmt.Errorf("pin must match regex pattern %q", pinRegex.String())
}
return nil
}