/
app_totp.go
70 lines (60 loc) · 1.68 KB
/
app_totp.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
package main
import (
"time"
"github.com/bgould/keyboard-firmware/keyboard/console"
totp "github.com/bgould/tinytotp"
)
var (
// defaultTotpAccount string
// defaultTotpKey SecureString
defaultTotpAccount string // = "keyboard tester"
defaultTotpKey SecureString // = "KWNKKXLJYPHGSFCB"
)
type TOTPKey struct {
Name string
Key SecureString
}
type SecureString string
func (s SecureString) String() string {
return "<redacted>"
}
var (
totpKeys = []TOTPKey{
{Name: defaultTotpAccount, Key: defaultTotpKey},
}
totpCommands = console.Commands{
"get": console.CommandHandlerFunc(totpget),
}
)
func totpget(cmd console.CommandInfo) int {
cmd.Stdout.Write([]byte("totp attempt"))
cmd.Stdout.Write([]byte(time.Now().Local().String()))
if code, err := totp.GenerateCode(string(totpKeys[0].Key), time.Now()); err != nil {
cmd.Stdout.Write([]byte("error generating TOTP: " + err.Error()))
return 1
} else {
cmd.Stdout.Write([]byte("generated code: " + code + "\n"))
return 0
}
}
func totptask() {
// TOTP-related functionality
if totpKeys[0].Name != "" && totpKeys[0].Key != "" {
ds.totpCounter = uint64(totp.TimeBasedCounter(time.Now(), totp.DefaultOpts.Period))
if ds.totpCounter != lastTotp {
// TODO: un-hardcode index
ds.totpAccount = totpKeys[0].Name
numbers, err := totp.GenerateCode(string(totpKeys[0].Key), time.Now())
if err != nil {
board.CLI().WriteString("warning: error updating TOTP - " + err.Error())
numbers = "000000"
}
ds.totpNumbers = numbers
lastTotp = ds.totpCounter
}
}
if err := showTime(ds, false); err != nil {
board.CLI().WriteString("warning: error updating display - " + err.Error())
}
displayTask()
}