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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
package auth
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/kyren223/eko/internal/client/ui"
authfield "github.com/kyren223/eko/internal/client/ui/auth/field"
"github.com/kyren223/eko/internal/client/ui/choicepopup"
"github.com/kyren223/eko/pkg/assert"
)
const (
usernameField = iota
privateKeyField
passphraseField
passphraseConfirmField
)
var (
grayStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240"))
focusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("198"))
cursorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("198"))
noStyle = lipgloss.NewStyle()
errorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#F16265"))
fieldStyle = lipgloss.NewStyle().
PaddingLeft(1).
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("#007E8A"))
focusedSignupButton = focusedStyle.Render("[ sign-up ]")
focusedSigninButton = focusedStyle.Render("[ sign-in ]")
blurredSignupButton = fmt.Sprintf("[ %s ]", grayStyle.Render("sign-up"))
blurredSigninButton = fmt.Sprintf("[ %s ]", grayStyle.Render("sign-in"))
headerStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#54D7A9"))
titleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#5874FF"))
signupTitle = titleStyle.Render(`
____ _ ____ _ _ _ _ ___
[__ | | __ |\ | __ | | |__]
___] | |__] | \| |__| |
`)
signinTitle = titleStyle.Render(`
____ _ ____ _ _ _ _ _
[__ | | __ |\ | __ | |\ |
___] | |__] | \| | | \|
`)
revealIcon = lipgloss.NewStyle().PaddingLeft(1).Render(" ")
concealIcon = lipgloss.NewStyle().PaddingLeft(1).Render(" ")
popupStyle = lipgloss.NewStyle().Border(lipgloss.ThickBorder())
choiceSelectedStyle = lipgloss.NewStyle().Background(focusedStyle.GetForeground()).Padding(0, 1).Margin(0, 1)
choiceUnselectedStyle = lipgloss.NewStyle().Background(grayStyle.GetForeground()).Padding(0, 1).Margin(0, 1)
)
type Model struct {
width int
height int
focusIndex int
fields []authfield.Model
signup bool
popup *choicepopup.Model
}
func New() Model {
m := Model{
fields: make([]authfield.Model, 4),
}
for i := range m.fields {
field := authfield.New(48)
field.Input.Cursor.Style = cursorStyle
field.Style = fieldStyle
field.ErrorStyle = errorStyle
field.FocusedStyle = focusedStyle
field.BlurredStyle = noStyle
switch i {
case usernameField:
field.Header = headerStyle.Render("Username")
field.Input.Placeholder = "Username"
field.Input.CharLimit = 48
field.Input.Validate = func(username string) error {
if len(username) == 0 {
return errors.New("Required")
}
return nil
}
case privateKeyField:
field.Header = headerStyle.Render("Private Key")
field.Input.Placeholder = "Path to Private Key"
field.Input.CharLimit = 100
field.Input.Validate = func(privKey string) error {
if len(privKey) == 0 {
return errors.New("Required")
}
return nil
}
case passphraseField:
field.Header = headerStyle.Render("Passphrase (Optional)")
field.Input.Placeholder = "Passphrase"
field.SetRevealIcon(revealIcon)
field.SetConcealIcon(concealIcon)
field.SetRevealed(false)
field.Input.EchoCharacter = '*'
case passphraseConfirmField:
field.Header = headerStyle.Render("Passphrase Confirm")
field.Input.Placeholder = "Repeated Passphrase"
field.SetRevealIcon(revealIcon)
field.SetConcealIcon(concealIcon)
field.SetRevealed(false)
field.Input.EchoCharacter = '*'
}
m.fields[i] = field
}
m.SetSignup(false)
return m
}
func (m Model) Init() tea.Cmd {
return nil
}
func (m Model) View() string {
var builder strings.Builder
builder.WriteRune('\n')
for i, field := range m.fields {
if field.Visisble {
builder.WriteString(m.fields[i].View())
} else {
builder.WriteString("\n\n")
}
if i < len(m.fields)-1 {
builder.WriteRune('\n')
}
}
var button *string
if m.signup {
if m.focusIndex == len(m.fields) {
button = &focusedSignupButton
} else {
button = &blurredSignupButton
}
} else {
if m.focusIndex == len(m.fields) {
button = &focusedSigninButton
} else {
button = &blurredSigninButton
}
}
fmt.Fprintf(&builder, "\n\n%s", *button)
var title string
if m.signup {
title = signupTitle
} else {
title = signinTitle
}
// Tiny offset so odd numbers will have the extra char on the right, ie: 12 <thing> 13
content := lipgloss.JoinVertical(lipgloss.Center-0.01, title, builder.String())
width := lipgloss.Width(content)
vp := viewport.New(64, 23)
vp.SetContent(content)
vp.Style = vp.Style.
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("#007E8A")).
// NOTE: Without -1 it wraps/truncates, not sure why
Padding(0, (vp.Width-width)/2-1, 1)
result := lipgloss.Place(
m.width, m.height,
lipgloss.Center, lipgloss.Center,
vp.View(),
)
if m.popup != nil {
popup := m.popup.View()
x := (m.width - lipgloss.Width(popup)) / 2
y := (m.height - lipgloss.Height(popup)) / 2
result = ui.PlaceOverlay(x, y, popup, result)
}
return result
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width, m.height = msg.Width, msg.Height
return m, nil
case tea.KeyMsg:
key := msg.Type
switch key {
case tea.KeyCtrlC:
return m, tea.Quit
case tea.KeyCtrlT:
if m.popup == nil && (m.focusIndex == passphraseField || m.focusIndex == passphraseConfirmField) {
m.fields[m.focusIndex].SetRevealed(!m.fields[m.focusIndex].Revealed())
}
return m, nil
case tea.KeyEnter:
if m.popup != nil {
_, choice := m.popup.Select()
if choice == "sign-up" {
m.popup = nil
return m, m.SetSignup(true)
}
if choice == "sign-in" {
m.popup = nil
return m, m.SetSignup(false)
}
if choice == "overwrite" {
// TODO: how should I handle this
// I need to somehow use this to notify that the file
// should be overwritten, or maybe I should remove this option?
// And make sure the user manually deletes/renames/moves the file
// So nobody can claim that this deleted their SSH keys
// (or more likely: I won't accidentally delete my SSH keys)
m.popup = nil
return m, nil
}
if choice == "cancel" {
m.popup = nil
return m, nil
}
assert.Never("unexpected choice", "choice", choice)
}
pressedButton := key == tea.KeyEnter && m.focusIndex == len(m.fields)
if pressedButton {
return m, m.ButtonPressed(msg)
}
case tea.KeyShiftTab, tea.KeyUp:
if m.popup != nil {
m.popup.ScrollLeft()
return m, nil
}
m.CycleBack()
return m, m.updateFocus()
case tea.KeyDown, tea.KeyTab:
if m.popup != nil {
m.popup.ScrollRight()
return m, nil
}
m.CycleForward()
return m, m.updateFocus()
}
}
if m.popup != nil {
return m, nil
}
cmds := make([]tea.Cmd, len(m.fields))
for i := range m.fields {
m.fields[i], cmds[i] = m.fields[i].Update(msg)
}
return m, tea.Batch(cmds...)
}
func (m *Model) CycleBack() {
for i := 0; ; i++ {
m.focusIndex--
if m.focusIndex < 0 {
m.focusIndex = len(m.fields)
}
if m.focusIndex == len(m.fields) || m.fields[m.focusIndex].Visisble {
break
}
assert.Assert(i < 2*len(m.fields), "CycleBack infinite loop", "i", i)
}
}
func (m *Model) CycleForward() {
for i := 0; ; i++ {
m.focusIndex++
if m.focusIndex > len(m.fields) {
m.focusIndex = 0
}
if m.focusIndex == len(m.fields) || m.fields[m.focusIndex].Visisble {
break
}
assert.Assert(i < 2*len(m.fields), "CycleForward infinite loop", "i", i)
}
}
func (m *Model) updateFocus() tea.Cmd {
cmds := make([]tea.Cmd, len(m.fields))
for i := 0; i < len(m.fields); i++ {
if i == m.focusIndex {
cmds[i] = m.fields[i].Focus()
} else {
m.fields[i].Blur()
}
}
return tea.Batch(cmds...)
}
func (m *Model) SetSignup(signup bool) tea.Cmd {
m.signup = signup
for i, field := range m.fields {
visible := m.signup || (i == privateKeyField || i == passphraseField)
field.Visisble = visible
field.Input.Err = nil
m.fields[i] = field
}
m.focusIndex = -1
m.CycleForward()
return m.updateFocus()
}
func (m *Model) ButtonPressed(msg tea.Msg) tea.Cmd {
var cmds []tea.Cmd
for i, field := range m.fields {
if !field.Visisble || field.Input.Validate == nil {
continue
}
field.Input.Err = field.Input.Validate(field.Input.Value())
if field.Input.Err != nil {
var cmd tea.Cmd
m.fields[i], cmd = field.Update(msg)
cmds = append(cmds, cmd)
}
}
if len(cmds) != 0 {
return tea.Batch(cmds...)
}
if m.signup {
return m.Signup()
} else {
return m.signin()
}
}
func (m *Model) Signup() tea.Cmd {
privateKeyFilepath := expandPath(m.fields[privateKeyField].Input.Value())
err := os.MkdirAll(filepath.Dir(privateKeyFilepath), 0o755)
if err != nil {
m.fields[privateKeyField].Input.Err = errors.Unwrap(err)
assert.NotNil(errors.Unwrap(err), "there should always be an error to unwrap", "err", err)
return nil
}
file, err := os.OpenFile(privateKeyFilepath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
if errors.Is(err, os.ErrExist) {
info, e := os.Stat(privateKeyFilepath)
assert.NoError(e, "if file exists it should be fine to stat it")
if info.IsDir() {
m.fields[privateKeyField].Input.Err = errors.New("directory exists")
return nil
}
content := fmt.Sprintf("File '%s' exists.\nDo you want to overwrite or sign-in instead?", privateKeyFilepath)
m.popup = createPopup(content, []string{"sign-in", "overwrite"}, []string{"cancel"})
return nil
}
if err != nil {
m.fields[privateKeyField].Input.Err = errors.Unwrap(err)
assert.NotNil(errors.Unwrap(err), "there should always be an error to unwrap", "err", err)
return nil
}
file.Close()
os.Remove(privateKeyFilepath)
return tea.Quit
}
func (m *Model) signin() tea.Cmd {
privateKeyFilepath := expandPath(m.fields[privateKeyField].Input.Value())
_, err := os.ReadFile(privateKeyFilepath)
if errors.Is(err, os.ErrNotExist) {
content := fmt.Sprintf("File '%s' doesn't exist.\nDo you want to sign-up instead?", privateKeyFilepath)
m.popup = createPopup(content, []string{"sign-up"}, []string{"cancel"})
return nil
}
if err != nil {
m.fields[privateKeyField].Input.Err = errors.Unwrap(err)
assert.NotNil(errors.Unwrap(err), "there should always be an error to unwrap", "err", err)
return nil
}
return tea.Quit
}
func createPopup(content string, leftChoices, rightChoices []string) *choicepopup.Model {
content = lipgloss.NewStyle().Padding(0, 1).
Border(lipgloss.NormalBorder(), false, false, true).
Render(content)
popup := choicepopup.New(lipgloss.Width(content), lipgloss.Height(content)+1)
popup.SetContent(content)
popup.SetChoices(leftChoices, rightChoices)
popup.Cycle = true
popup.Style = popupStyle
popup.SelectedStyle = choiceSelectedStyle
popup.UnselectedStyle = choiceUnselectedStyle
return &popup
}
func expandPath(path string) string {
if strings.HasPrefix(path, "~") {
home, err := os.UserHomeDir()
assert.NoError(err, "home directory should always be defined")
if !strings.HasPrefix(path, "~/") {
return home
}
return filepath.Join(home, path[2:])
}
return path
}
func test() {
// pubKey, privKey, err := ed25519.GenerateKey(nil)
// sshPrivKey, err := ssh.NewSignerFromSigner(privKey)
// ssh.MarshalPrivateKey()
// ssh.MarshalPrivateKey()
// ssh.ParseRawPrivateKey()
// ssh.ParseRawPrivateKeyWithPassphrase()
}
|