-
Notifications
You must be signed in to change notification settings - Fork 0
/
mfa.go
245 lines (217 loc) · 5.46 KB
/
mfa.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
package mfa
import (
"fmt"
"io/ioutil"
"os"
"time"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
)
type Color string
const (
SecretFileNameEnv = "MFA_SECRET_FILE"
Red Color = "\033[31m"
Green Color = "\033[32m"
Yellow Color = "\033[33m"
Blue Color = "\033[34m"
Purple Color = "\033[35m"
Cyan Color = "\033[36m"
Gray Color = "\033[37m"
White Color = "\033[97m"
)
//otp library doesn't provide a map to translate algorithm names
var algMap map[string]otp.Algorithm = map[string]otp.Algorithm{
"SHA1": otp.AlgorithmSHA1,
"SHA256": otp.AlgorithmSHA256,
"SHA512": otp.AlgorithmSHA512,
"MD5": otp.AlgorithmMD5,
}
var TerminalColors map[string]Color = map[string]Color{
"red": Red,
"green": Green,
"yellow": Yellow,
"blue": Blue,
"purple": Purple,
"cyan": Cyan,
"gray": Gray,
"white": White,
}
//Writer is an interface which an MFADevce will write to
type Writer interface {
Write(p []byte) error
Warn(p []byte) error
Error(p []byte) error
}
//Terminsl is a Writer for writing out OTP codes & error/warning messages
type Terminal struct {
Pattern string
Prefix string
DefaultColor Color
WarningColor Color
ErrorColor Color
}
//NewTerminal creates a new Terminal using functional options to set the output colors
func NewTerminal(options ...func(*Terminal)) *Terminal {
term := &Terminal{
Pattern: "\r%s%s [%s] %s",
Prefix: "default",
DefaultColor: Green,
WarningColor: Cyan,
ErrorColor: Red,
}
for _, o := range options {
o(term)
}
return term
}
//Prefix is a functional option for setting a prefix to help identify an OTP (if you have multiple running)
func Prefix(pre string) func(*Terminal) {
return func(t *Terminal) {
if pre != "" {
t.Prefix = pre
}
}
}
//DefaultColor is a functional option for setting the terminal default text color
func DefaultColor(c Color) func(*Terminal) {
return func(t *Terminal) {
if c != "" {
t.DefaultColor = c
}
}
}
//WarningColor is a functional option for setting the terminal warning text color
func WarningColor(c Color) func(*Terminal) {
return func(t *Terminal) {
if c != "" {
t.WarningColor = c
}
}
}
//ErrorColor is a functional option for setting the terminal error text color
func ErrorColor(c Color) func(*Terminal) {
return func(t *Terminal) {
if c != "" {
t.ErrorColor = c
}
}
}
//Write writes in the DefaultColor of the Terminal
func (t *Terminal) Write(p []byte) error {
fmt.Fprintf(os.Stdout, t.Pattern, t.DefaultColor, t.Prefix, time.Now().Format(time.RFC3339), p)
return nil
}
//Warn writes in the WarningColor of the Terminal
func (t *Terminal) Warn(p []byte) error {
fmt.Fprintf(os.Stdout, t.Pattern, t.WarningColor, t.Prefix, time.Now().Format(time.RFC3339), p)
return nil
}
//Error writes in the ErrorColor of the Terminal
func (t *Terminal) Error(p []byte) error {
fmt.Fprintf(os.Stderr, t.Pattern, t.ErrorColor, t.Prefix, time.Now().Format(time.RFC3339), p)
return nil
}
type MFADevice struct {
Secret []byte
Writer Writer
UpdateFrequency time.Duration
TOTPOptions totp.ValidateOpts
}
//NewMFADevice creates a new MFADevice using functional options
func NewMFADevice(options ...func(*MFADevice)) *MFADevice {
//By default use a Terminal
term := NewTerminal()
device := &MFADevice{
TOTPOptions: totp.ValidateOpts{},
Writer: term,
UpdateFrequency: time.Second,
}
for _, o := range options {
o(device)
}
return device
}
//Run runs the MFADevice
func (d *MFADevice) Run() {
ticker := time.NewTicker(d.UpdateFrequency)
for {
t := <-ticker.C
out, err := totp.GenerateCodeCustom(string(d.Secret), t, d.TOTPOptions)
if err != nil {
panic(err)
}
if t.Second() >= 55 || (t.Second() < 30 && t.Second() >= 25) {
err = d.Writer.Warn([]byte(out))
if err != nil {
panic(err)
}
} else {
err = d.Writer.Write([]byte(out))
if err != nil {
panic(err)
}
}
}
}
//Secret is a functional option to set a secret on an MFADevice
func Secret(secret string) func(*MFADevice) {
return func(d *MFADevice) {
if secret != "" {
d.Secret = []byte(secret)
}
}
}
//SecretFromFile is a functional option to tell an MFADevice to read the secret from a file
func SecretFromFile(file *os.File) func(*MFADevice) {
return func(d *MFADevice) {
if file != nil {
stat, err := file.Stat()
if err != nil {
panic(err)
}
if stat.Mode().Perm() > 0o700 {
d.Writer.Warn([]byte(fmt.Sprintf("WARNING - secret file %q is not secure\n", file.Name())))
}
secret, err := ioutil.ReadAll(file)
if err != nil {
panic(err)
}
d.Secret = secret
}
}
}
//Output is a functional option to tell an MFADevice which Writer to output OTP codes to
func Output(w Writer) func(*MFADevice) {
return func(d *MFADevice) {
if w != nil {
d.Writer = w
}
}
}
//UpdateFrequency is a functional option to tell an MFADevice to read the secret from a file
func UpdateFrequency(p time.Duration) func(*MFADevice) {
return func(d *MFADevice) {
if p != 0 {
d.UpdateFrequency = p
}
}
}
func RefreshPeriod(per uint) func(*MFADevice) {
return func(d *MFADevice) {
if per != 0 {
d.TOTPOptions.Period = per
}
}
}
func Digits(dig int) func(*MFADevice) {
return func(d *MFADevice) {
d.TOTPOptions.Digits = otp.Digits(dig)
}
}
func Algorithm(alg string) func(*MFADevice) {
return func(d *MFADevice) {
if alg != "" {
d.TOTPOptions.Algorithm = algMap[alg]
}
}
}