-
Notifications
You must be signed in to change notification settings - Fork 1
/
key_test.go
124 lines (108 loc) · 3.14 KB
/
key_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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"fmt"
"net/url"
"os"
"testing"
"time"
"github.com/99designs/keyring"
otp "github.com/hgfischer/go-otp"
)
// func TestUnmarshalJSON(t *testing.T) {
// jsonString := "{\"name\": \"Test\", \"digits\": 8, \"interval\": 30}"
// data := Key{}
// err := json.Unmarshal([]byte(jsonString), &data)
// if err != nil {
// fmt.Errorf("%s", err)
// }
// test := Key{
// Name: "Test",
// Digits: 8,
// Interval: 30,
// }
// if !reflect.DeepEqual(data, test) {
// t.Errorf("struct is not as expected: %+v %+v", data, test)
// }
// }
func _generateTotp(secret string, digits int) string {
now := time.Now()
interval := 30
timeInterval := uint64(time.Duration(interval) * time.Second)
counter := uint64(now.UnixNano()) / timeInterval
token := &otp.HOTP{
Secret: string(secret),
Length: uint8(digits),
Counter: counter,
IsBase32Secret: true,
}
return token.Get()
}
// openTestKeyring replaces openKeyring function to use a file backend instead
// This should allow running tests on all platforms without any OS specific
// dependency.
func openTestKeyring() (keyring.Keyring, error) {
path, err := os.Getwd()
if err != nil {
return nil, err
}
config := keyring.Config{
AllowedBackends: []keyring.BackendType{
keyring.FileBackend,
},
ServiceName: "2ami",
FilePasswordFunc: func(prompt string) (string, error) {
return "password", nil
},
FileDir: path,
}
ring, err := keyring.Open(config)
if err != nil {
return nil, fmt.Errorf("can't open keyring: %w", err)
}
return ring, nil
}
func TestKeyGenerateTotp(t *testing.T) {
var generatedToken string
ring, _ := openTestKeyring()
key := NewKey(ring, "test")
secretValue := "ORSXG5A="
_ = key.Secret(secretValue)
token := _generateTotp(secretValue, key.Digits)
generatedToken = key.GenerateToken()
if generatedToken != token {
t.Errorf("Wrong token. Expected %s Actual %s", token, generatedToken)
}
key = NewKey(ring, "anothertest")
secretValue = "MFXG65DIMVZHIZLTOQFA===="
_ = key.Secret(secretValue)
token = _generateTotp(secretValue, key.Digits)
generatedToken = key.GenerateToken()
if generatedToken != token {
t.Errorf("Wrong token. Expected %s Actual %s", token, generatedToken)
}
key = NewKey(ring, "thisisatest2")
secretValue = "ORUGS43JON2GK43UGI======"
_ = key.Secret(secretValue)
token = _generateTotp(secretValue, key.Digits)
generatedToken = key.GenerateToken()
if generatedToken != token {
t.Errorf("Wrong token. Expected %s Actual %s", token, generatedToken)
}
}
func TestKeyGenerateotpauthURI(t *testing.T) {
ring, _ := openTestKeyring()
key := NewKey(ring, "test")
secretValue := "ORSXG5A="
_ = key.Secret(secretValue)
want := fmt.Sprintf("otpauth://totp/test?digits=6&period=30&secret=%s", url.QueryEscape(secretValue))
got, err := key.OtpauthURI()
if err != nil {
t.Errorf("error occurred: %s", err.Error())
}
if got != want {
t.Errorf("wrong oauthURI. Expected %s Actual %s", want, got)
}
}