-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtotp_test.go
291 lines (232 loc) · 6.9 KB
/
totp_test.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package otpgo
import (
"encoding/json"
"testing"
"time"
"github.com/jltorresm/otpgo/config"
)
func TestTOTP_Generate(t *testing.T) {
t.Run("Normal Generation", testTOTPNormalGeneration)
t.Run("Bad Key", testTOTPBadKey)
t.Run("Default Params", testTOTPDefaultParams)
t.Run("Autogenerated Key", testTOTPAutogeneratedKey)
t.Run("Lower Case Key", testTOTPLowerCaseKey)
}
func testTOTPNormalGeneration(t *testing.T) {
key := "73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUQ"
totp := &TOTP{
Key: key,
Period: TOTPDefaultPeriod,
Algorithm: config.HmacSHA256,
Length: config.Length6,
}
expectedOTP, err := getExpectedTOTP(key, totp.getCounter(time.Now().Unix()), config.Length6, config.HmacSHA256)
if err != nil {
t.Errorf("unexpected error: %s", err)
t.FailNow()
}
actualOTP, err := totp.Generate()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if actualOTP != expectedOTP {
t.Errorf("wrong totp\nexpected: %s\n actual: %s", expectedOTP, actualOTP)
}
}
func testTOTPBadKey(t *testing.T) {
totp := &TOTP{
Key: "invalid-base-32",
Period: TOTPDefaultPeriod,
Algorithm: config.HmacSHA256,
Length: config.Length6,
}
_, err := totp.Generate()
expectedErr := ErrorInvalidKey{msg: "illegal base32 data at input byte 7"}
if err != expectedErr {
t.Errorf("unexpected error: %s", err)
}
}
func testTOTPDefaultParams(t *testing.T) {
key := "73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUQ"
expectedPeriod := TOTPDefaultPeriod
expectedDelay := TOTPDefaultDelay
expectedAlg := config.HmacSHA1
expectedLength := config.Length6
totp := &TOTP{Key: key}
otp, err := totp.Generate()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if totp.Period != expectedPeriod {
t.Errorf("unexpected period\nexpected: %d\n actual: %d", expectedPeriod, totp.Period)
}
if totp.Delay != expectedDelay {
t.Errorf("unexpected delay\nexpected: %d\n actual: %d", expectedDelay, totp.Delay)
}
if totp.Algorithm != expectedAlg {
t.Errorf("unexpected hash algorithm\nexpected: %d (SHA1)\n actual: %d", expectedAlg, totp.Algorithm)
}
if totp.Length != expectedLength {
t.Errorf("unexpected length\nexpected: %d\n actual: %d", expectedLength, totp.Length)
}
// Make sure to generate the counter after the defaults have been loaded
counter := totp.getCounter(time.Now().Unix())
expectedOTP, err := getExpectedTOTP(key, counter, expectedLength, expectedAlg)
if err != nil {
t.Errorf("unexpected error: %s", err)
t.FailNow()
}
if otp != expectedOTP {
t.Errorf("unexpected totp\nexpected: %s\n actual: %s", expectedOTP, otp)
}
}
func testTOTPAutogeneratedKey(t *testing.T) {
totp := &TOTP{}
_, err := totp.Generate()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if totp.Key == "" {
t.Error("expected Key to be populated")
}
}
func testTOTPLowerCaseKey(t *testing.T) {
totp := &TOTP{Key: "73qk7d3a3piz6nuqqbf4bnfyqbrvuhuq"}
_, err := totp.Generate()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if totp.Key == "" {
t.Error("expected Key to be populated")
}
}
func TestTOTP_Validate(t *testing.T) {
t.Run("Success", testTOTPValidateSuccess)
t.Run("Failure", testTOTPValidateFailure)
t.Run("Delayed Validation", testTOTPValidateDelayed)
t.Run("Missing Key", testTOTPValidateMissingKey)
}
func testTOTPValidateSuccess(t *testing.T) {
t.Parallel()
totp := &TOTP{
Key: "73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUQ",
Period: TOTPDefaultPeriod,
Delay: TOTPDefaultDelay,
Algorithm: config.HmacSHA512,
Length: config.Length8,
}
expectedOTP, err := getExpectedTOTP(totp.Key, totp.getCounter(time.Now().Unix()), totp.Length, totp.Algorithm)
if err != nil {
t.Errorf("unexpected error: %s", err)
t.FailNow()
}
isValid, err := totp.Validate(expectedOTP)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !isValid {
t.Errorf("invalid token\nexpected %s to be valid", expectedOTP)
}
}
func testTOTPValidateFailure(t *testing.T) {
t.Parallel()
invalidOTP := "111111"
totp := &TOTP{Key: "73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUQ", Length: config.Length8}
isValid, err := totp.Validate(invalidOTP)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if isValid {
t.Errorf("unexpected valid token\nexpected %s to be invalid", invalidOTP)
}
}
func testTOTPValidateDelayed(t *testing.T) {
t.Parallel()
totp := &TOTP{
Key: "73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUQ",
Period: 10,
Delay: 2,
Algorithm: config.HmacSHA256,
Length: config.Length7,
}
cases := []struct {
label string
modifier int
shouldBeValid bool
}{
{"On Time", 0, true},
{"One Step Delay", -1, true},
{"One Step Forward", 1, true},
{"Two Step Delay", -2, true},
{"Two Step Forward", +2, true},
{"Three Step Delay", -3, false},
{"Three Step Forward", 3, false},
}
for _, c := range cases {
t.Run(c.label, func(t *testing.T) {
counter := totp.getCounter(time.Now().Unix() + int64(totp.Period*c.modifier))
expectedOTP, err := getExpectedTOTP(totp.Key, counter, totp.Length, totp.Algorithm)
if err != nil {
t.Errorf("unexpected error: %s", err)
t.FailNow()
}
isValid, err := totp.Validate(expectedOTP)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if isValid != c.shouldBeValid {
t.Errorf("unexpected result from Validate()\nexpected %s to be valid", c.label)
}
})
}
}
func testTOTPValidateMissingKey(t *testing.T) {
totp := &TOTP{}
isValid, err := totp.Validate("irrelevant")
if err == nil {
t.Error("expected error")
t.FailNow()
}
if err.Error() != "missing secret key for validation" {
t.Errorf("unexpected error: %s", err)
}
if isValid {
t.Errorf("token should be invalid")
}
}
func TestTOTP_KeyUri(t *testing.T) {
totp := TOTP{Key: "JOC773H4BTUR5U6M422M2AT7S4MTQ7BLR75Y252JK3A"}
_, _ = totp.Generate()
accountName := "jò[email protected]"
issuer := "Acme Inc"
expectedUri := "otpauth://totp/Acme%20Inc:j%C3%[email protected]?algorithm=SHA1&digits=6&issuer=Acme+Inc&period=30&secret=JOC773H4BTUR5U6M422M2AT7S4MTQ7BLR75Y252JK3A"
uri := totp.KeyUri(accountName, issuer)
if expectedUri != uri.String() {
t.Errorf("unexpected key URI\nexpected: %s\n actual: %s", expectedUri, uri.String())
}
}
func TestTOTPJson(t *testing.T) {
h := TOTP{
Key: "73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUQ",
Period: 30,
Delay: 1,
Algorithm: config.HmacSHA256,
Length: config.Length7,
}
expectedJson := `{"key":"73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUQ","period":30,"delay":1,"algorithm":"SHA256","length":7}`
j, err := json.Marshal(h)
if err != nil {
t.Errorf("unexpected error: %s", err)
t.FailNow()
}
if expectedJson != string(j) {
t.Errorf("unexpected json:\nexpected: %s\n actual: %s", expectedJson, j)
}
}
func getExpectedTOTP(key string, counter uint64, length config.Length, algorithm config.HmacAlgorithm) (string, error) {
expectedOTP, err := generateOTP(key, counter, length, algorithm)
if err != nil {
return "", err
}
return expectedOTP, nil
}