-
Notifications
You must be signed in to change notification settings - Fork 1
/
key.go
155 lines (135 loc) · 3.16 KB
/
key.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
// 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 (
"encoding/json"
"fmt"
"log"
"net/url"
"time"
"github.com/99designs/keyring"
otp "github.com/hgfischer/go-otp"
)
type KeyType int8
const (
HOTP_TOKEN KeyType = 0
TOTP_TOKEN KeyType = 1
)
type Key struct {
Name string `json:"name"`
Type KeyType `json:"type,int8"` //nolint
Digits int `json:"digits,int"` //nolint
Interval int `json:"interval,int"` //nolint
Counter int `json:"counter,int"` //nolint
secret SecretString
}
func NewKey(ring keyring.Keyring, name string) Key {
return Key{
Name: name,
Type: TOTP_TOKEN,
Digits: 6,
Interval: 30,
Counter: 1,
secret: newSecretString(name, ring),
}
}
func KeyFromStorage(storage Storage, ring keyring.Keyring, name string) Key {
value, err := storage.GetKey(name)
if err != nil {
fmt.Println(fmt.Errorf("%s", err))
}
key := Key{}
err = json.Unmarshal([]byte(value), &key)
if err != nil {
fmt.Println(fmt.Errorf("%s", err))
}
key.secret = newSecretString(name, ring)
return key
}
func (k Key) String() string {
return k.Name
}
func (k Key) VerboseString() string {
return fmt.Sprintf("%s \t %d digits every %d seconds", k.Name, k.Digits, k.Interval)
}
func (k *Key) GenerateToken() string {
switch k.Type {
case TOTP_TOKEN:
return k.totpToken()
case HOTP_TOKEN:
return k.hotpToken()
default:
panic("Unknown key type. Valid type: TOTP or HOTP")
}
}
func (k *Key) ExpiresIn() int {
currentTime := time.Now()
return k.Interval - (currentTime.Second() % k.Interval)
}
func (k *Key) totpToken() string {
secret, err := k.secret.Value()
if err != nil {
log.Fatal(err)
}
totp := &otp.TOTP{
Secret: string(secret),
Length: uint8(k.Digits),
Period: uint8(k.Interval),
IsBase32Secret: true,
}
token := totp.Get()
return token
}
// Generate a new HOTP token and increament counter
func (k *Key) hotpToken() string {
secret, err := k.secret.Value()
if err != nil {
log.Fatal(err)
}
hotp := &otp.HOTP{
Secret: string(secret),
Counter: uint64(k.Counter),
Length: uint8(k.Digits),
IsBase32Secret: true,
}
token := hotp.Get()
k.Counter++
return token
}
func (k *Key) Secret(secret string) error {
return k.secret.Set([]byte(secret))
}
func (k *Key) Delete() error {
return k.secret.Remove()
}
func (k *Key) Rename(newName string) error {
err := k.secret.Rename(newName)
if err != nil {
return err
}
k.Name = newName
return nil
}
func (k Key) OtpauthURI() (string, error) {
out := url.URL{
Scheme: "otpauth",
Path: k.Name,
}
q := out.Query()
secret, err := k.secret.Value()
if err != nil {
return "", fmt.Errorf("cannot read key secret: %w", err)
}
q.Set("secret", string(secret))
q.Set("digits", fmt.Sprint(k.Digits))
if k.Type == TOTP_TOKEN {
out.Host = "totp"
q.Set("period", fmt.Sprint(k.Interval))
} else if k.Type == HOTP_TOKEN {
out.Host = "hotp"
q.Set("counter", fmt.Sprint(k.Counter))
}
out.RawQuery = q.Encode()
return out.String(), nil
}