-
Notifications
You must be signed in to change notification settings - Fork 2
/
hotp.go
126 lines (101 loc) · 2.74 KB
/
hotp.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package auth
import (
"crypto/hmac"
"crypto/sha1"
"crypto/subtle"
"encoding/base32"
"encoding/binary"
"fmt"
"hash"
"math"
"strconv"
"strings"
"github.com/uhppoted/uhppoted-lib/kvs"
"github.com/uhppoted/uhppoted-mqtt/log"
)
type HOTP struct {
increment uint64
secrets *kvs.KeyValueStore
counters struct {
*kvs.KeyValueStore
filepath string
}
}
const DIGITS = 6
func NewHOTP(increment uint64, secrets string, counters string) (*HOTP, error) {
u := func(value string) (interface{}, error) {
return value, nil
}
v := func(value string) (interface{}, error) {
return strconv.ParseUint(value, 10, 64)
}
hotp := HOTP{
increment: increment,
secrets: kvs.NewKeyValueStore("hotp:secrets", u),
counters: struct {
*kvs.KeyValueStore
filepath string
}{
kvs.NewKeyValueStore("hotp:counters", v),
counters,
},
}
if err := hotp.secrets.LoadFromFile(secrets); err != nil {
return &hotp, err
}
if err := hotp.counters.LoadFromFile(counters); err != nil {
log.Warnf(LOG_TAG, "%v", err)
}
hotp.secrets.Watch(secrets)
return &hotp, nil
}
func (hotp *HOTP) Validate(clientID, otp string) error {
otp = strings.TrimSpace(otp)
if len(otp) != DIGITS {
return fmt.Errorf("%s: invalid OTP '%s' - expected %d digits", clientID, otp, DIGITS)
}
secret, ok := hotp.secrets.Get(clientID)
if !ok {
return fmt.Errorf("%s: no authorisation key", clientID)
}
counter, ok := hotp.counters.Get(clientID)
if !ok {
counter = uint64(1)
}
for i := uint64(0); i < hotp.increment; i++ {
generated, err := generateHOTP(secret.(string), counter.(uint64), DIGITS, sha1.New)
if err != nil {
return err
}
if subtle.ConstantTimeCompare([]byte(generated), []byte(otp)) == 1 {
hotp.counters.Store(clientID, counter.(uint64)+1, hotp.counters.filepath)
return nil
}
counter = counter.(uint64) + 1
}
return fmt.Errorf("%s: invalid OTP %s", clientID, otp)
}
// Ref. https://github.com/pquerna/otp
func generateHOTP(secret string, counter uint64, digits int, algorithm func() hash.Hash) (passcode string, err error) {
secret = strings.TrimSpace(secret)
if n := len(secret) % 8; n != 0 {
secret = secret + strings.Repeat("=", 8-n)
}
bytes, err := base32.StdEncoding.DecodeString(strings.ToUpper(secret))
if err != nil {
return "", err
}
buffer := make([]byte, 8)
mac := hmac.New(algorithm, bytes)
binary.BigEndian.PutUint64(buffer, counter)
mac.Write(buffer)
sum := mac.Sum(nil)
// http://tools.ietf.org/html/rfc4226#section-5.4
offset := sum[len(sum)-1] & 0xf
value := int64(((int(sum[offset]) & 0x7f) << 24) |
((int(sum[offset+1] & 0xff)) << 16) |
((int(sum[offset+2] & 0xff)) << 8) |
(int(sum[offset+3]) & 0xff))
mod := int32(value % int64(math.Pow10(digits)))
return fmt.Sprintf("%06d", mod), nil
}