/
otp.go
169 lines (133 loc) · 3 KB
/
otp.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package otp
import (
"crypto"
"crypto/hmac"
"encoding/base32"
"encoding/binary"
"math"
"net/url"
"strconv"
"strings"
"time"
)
const (
// 30 second interval
DefaultInterval = 30
OtpAuthScheme = "otpauth"
)
type Counter interface {
Count() int64
}
type TimeCounter struct {
Interval, Step int
}
func (t *TimeCounter) Count() int64 {
return int64(math.Floor(float64(time.Now().Unix()-int64(t.Step)) / float64(t.Interval)))
}
type OTP interface {
Counter() Counter
Hash() crypto.Hash
GenerateCode(key string, count int64, tokenLen int) (string, error)
VerifyCode(code, key string, skew, tokenLen int) bool
}
type HOTP struct {
counter Counter
hashAlgo crypto.Hash
}
func (h *HOTP) Counter() Counter {
return h.counter
}
func (h *HOTP) Hash() crypto.Hash {
return h.hashAlgo
}
func (h *HOTP) GenerateCode(key string, count int64, tokenLen int) (string, error) {
b, err := base32.StdEncoding.DecodeString(key)
if err != nil {
return "", err
}
hmacResult := genKey(h.Hash(), b, count)
code := strconv.Itoa(truncate(hmacResult) % int(math.Pow10(tokenLen)))
// pad the string on the left if necessary
if len(code) < tokenLen {
code = strings.Repeat("0", tokenLen-len(code)) + code
}
return code, nil
}
func (h *HOTP) VerifyCode(code, key string, skew, tokenLen int) bool {
currentCount := h.counter.Count()
currentCode, _ := h.GenerateCode(key, currentCount, tokenLen)
if currentCode == code {
return true
}
for i := 1; i < skew; i++ {
behind, _ := h.GenerateCode(key, currentCount-int64(i), tokenLen)
ahead, _ := h.GenerateCode(key, currentCount+int64(i), tokenLen)
if behind == code || ahead == code {
return true
}
}
return false
}
func truncate(b []byte) int {
offset := int(b[len(b)-1]) & 0xf
return int(b[offset])&0x7f<<24 |
int(b[offset+1])&0xff<<16 |
int(b[offset+2])&0xff<<8 |
int(b[offset+3])&0xff
}
func genKey(hasher crypto.Hash, key []byte, count int64) []byte {
hmacHash := hmac.New(hasher.New, key)
binary.Write(hmacHash, binary.BigEndian, count)
return hmacHash.Sum(nil)
}
func NewTOTP(hash crypto.Hash, interval, step int) OTP {
return &HOTP{
counter: &TimeCounter{
Interval: interval,
Step: step,
},
hashAlgo: hash,
}
}
func DefaultTOTP() OTP {
return NewTOTP(crypto.SHA1, DefaultInterval, 0)
}
type AuthURL struct {
Type string
Label string
Secret string
Issuer string
Algorithm string
Counter int
Digits int
Period int
}
func (o *AuthURL) String() string {
uri := &url.URL{
Scheme: "otpauth",
Host: o.Type,
Path: o.Label,
RawQuery: o.values().Encode(),
}
return uri.String()
}
func (o *AuthURL) values() url.Values {
v := url.Values{}
v.Add("secret", o.Secret)
if o.Issuer != "" {
v.Add("issuer", o.Issuer)
}
if o.Algorithm != "" {
v.Add("algorithm", o.Algorithm)
}
if o.Counter != 0 {
v.Add("counter", strconv.Itoa(o.Counter))
}
if o.Digits != 0 {
v.Add("digits", strconv.Itoa(o.Digits))
}
if o.Period != 0 {
v.Add("period", strconv.Itoa(o.Period))
}
return v
}