-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (144 loc) · 3.44 KB
/
main.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
package main
import (
"fmt"
"os"
"strings"
"yko/ykoath"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/progress"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/ebfe/scard"
"golang.design/x/clipboard"
"golang.org/x/term"
)
type model struct {
state tui_state
key *ykoath.YKO
docStyle lipgloss.Style
list list.Model
listName string
progress progress.Model
progressPerc float64
help help.Model
code string
}
type executeNextUpdate struct{}
func nextStep() executeNextUpdate {
return executeNextUpdate{}
}
func main() {
if err := clipboard.Init(); err != nil {
log.Fatal("Cannot init the clipboard", "error", err)
}
ctx, card, err := setupYubiKey()
if err != nil {
log.Fatal("Failed to setup YubiKey", "error", err)
}
defer func() {
if err := card.Disconnect(scard.ResetCard); err != nil {
log.Warn("Failed to disconnect YubiKey", "error", err)
}
if err := ctx.Release(); err != nil {
log.Warn("Failed to release scard context", "error", err)
}
}()
yKey := ykoath.New(card)
if err := yKey.Select(); err != nil {
log.Fatal("Failed to select", "error", err)
}
if yKey.AuthRequired() {
fmt.Print("Password: ")
pass, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Fatal(err)
}
fmt.Println()
if err := yKey.Validate(pass); err != nil {
log.Fatal(err)
}
}
keyList, err := yKey.List()
if err != nil {
log.Fatal("Failed to list", "error", err)
}
a := make([]list.Item, len(keyList))
for i := 0; i < len(keyList); i++ {
a[i] = accounts{acc: keyList[i].Name}
}
m := model{
state: LIST_ACCOUNTS,
key: yKey,
docStyle: lipgloss.NewStyle().Margin(1, 2),
list: list.New(a, list.NewDefaultDelegate(), 0, 0),
listName: "YubiKey Accounts",
progress: progress.New(),
progressPerc: 0,
help: help.New(),
code: "",
}
m.list.Title = m.listName
m.progress.ShowPercentage = false
p := tea.NewProgram(m, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
log.Fatal("Failed to run the TUI", "error", err)
}
}
func setupYubiKey() (*scard.Context, *scard.Card, error) {
ctx, err := scard.EstablishContext()
if err != nil {
return nil, nil, err
}
readers, err := ctx.ListReaders()
if err != nil {
return nil, nil, err
}
for _, reader := range readers {
if strings.Contains(reader, "Yubico YubiKey") {
card, err := ctx.Connect(reader, scard.ShareExclusive, scard.ProtocolAny)
if err != nil {
return nil, nil, err
}
return ctx, card, nil
}
}
return nil, nil, fmt.Errorf("No YoubiKey was found")
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch m.state {
case LIST_ACCOUNTS:
return UpdateList(msg, m)
case TOUCH_KEY:
switch msg.(type) {
case executeNextUpdate:
code, err := m.key.Calculate(m.list.SelectedItem().(accounts).acc)
if err != nil {
log.Fatal("Failed to get code", "error", err)
}
m.code = code
m.state = SHOW_CODE
m.progressPerc = 0
return m, tickCmd()
}
return m, nil
case SHOW_CODE:
return UpdateCode(msg, m)
}
return nil, nil
}
func (m model) View() string {
switch m.state {
case LIST_ACCOUNTS:
return ViewList(m)
case TOUCH_KEY:
return "Touch your YubiKey!!!"
case SHOW_CODE:
return ViewCode(m)
}
return ""
}