-
Notifications
You must be signed in to change notification settings - Fork 3
/
key.go
150 lines (137 loc) · 3.97 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
package key
import (
"fmt"
"github.com/beeper/beepycli/utils"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbletea"
gloss "github.com/charmbracelet/lipgloss"
)
type valid struct {
file, password bool
}
type Model struct {
password, confirmation, file textinput.Model
status string
valid valid
buttonFocused bool
}
func InitModel() Model {
return Model{
password: utils.TextInput(utils.PasswordPlaceholder, true),
confirmation: utils.TextInput(utils.PasswordPlaceholder, true),
file: utils.TextInput("/home/user/element-keys.txt", false),
}
}
func (m Model) KeyPath() string {
return m.file.Value()
}
func (m Model) KeyPassword() string {
return m.password.Value()
}
func (m Model) Init() tea.Cmd {
return getTextFileInWd
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if key, ok := msg.(tea.KeyMsg); ok {
switch key.Type {
case tea.KeyCtrlC:
return m, tea.Quit
case tea.KeyEnter, tea.KeyTab:
if m.buttonFocused {
if m.valid.file && m.valid.password {
return m, utils.NextPhase
}
} else if m.file.Focused() {
m.buttonFocused = true
m.file.Blur()
} else if m.confirmation.Focused() {
m.confirmation.Blur()
m.file.Focus()
} else if m.password.Focused() {
m.password.Blur()
m.confirmation.Focus()
} else {
m.password.Focus()
}
case tea.KeyShiftTab:
if m.buttonFocused {
m.buttonFocused = false
m.file.Focus()
} else if m.file.Focused() {
m.file.Blur()
m.confirmation.Focus()
} else if m.confirmation.Focused() {
m.confirmation.Blur()
m.password.Focus()
} else if m.password.Focused() {
m.password.Blur()
} else {
return m, utils.PrevPhase
}
default:
var cmd tea.Cmd
if m.password.Focused() {
m.password, cmd = m.password.Update(msg)
cmd = tea.Batch(cmd, validatePassword(m.password.Value(), m.confirmation.Value()))
} else if m.confirmation.Focused() {
m.confirmation, cmd = m.confirmation.Update(msg)
cmd = tea.Batch(cmd, validatePassword(m.password.Value(), m.confirmation.Value()))
} else if m.file.Focused() {
m.file, cmd = m.file.Update(msg)
return m, tea.Batch(cmd, prevalidate)
}
return m, cmd
}
} else if _, ok := msg.(fileLoadingMsg); ok {
m.status = loadingValidation.Render()
m.valid.file = false
return m, validateKey(m.file.Value())
} else if _, ok := msg.(fileOkMsg); ok {
m.status = okValidation.Render()
m.valid.file = true
} else if _, ok := msg.(fileErrMsg); ok {
m.status = errValidation.Render()
m.valid.file = false
} else if validation, ok := msg.(passwordValidationMsg); ok {
m.valid.password = bool(validation)
} else if file, ok := msg.(keyFileMsg); ok {
m.file.Placeholder = string(file)
m.file.SetValue(string(file))
m.file.CursorEnd()
return m, prevalidate
}
return m, nil
}
func (m Model) View() string {
italic := gloss.NewStyle().Italic(true)
validation := ""
if len(m.confirmation.Value()) > 0 {
if m.valid.password {
validation = fmt.Sprintf(" %s", okValidation.Render())
} else {
validation = fmt.Sprintf(" %s", errValidation.Render())
}
}
return fmt.Sprintf(
"%s\n"+
"You're going to need some keys for this: in Beeper Desktop, navigate\n"+
"to %s and scroll down to the\n"+
"%s heading. Then click the %s button.\n"+
"The fields below will ask you for the password you gave your keys,\n"+
"and their location on your computer.\n\n"+
"Password: %s%s\n"+
"Confirm Password: %s%s\n"+
"Path to Keys: %s %s\n\n"+
"Whole lotta cryptography gonna be happening here real soon 🔐!\n\n"+
"%s",
utils.Title().Render("Configure end-to-end encryption for your Beepy"),
italic.Render("Gear > Settings > Security & Privacy"),
italic.Render("Cryptography"),
italic.Render("Export E2E room keys"),
m.password.View(), validation,
m.confirmation.View(), validation,
m.file.View(),
m.status,
utils.Button(m.buttonFocused).Render("Next"),
)
}