Skip to content

Commit

Permalink
tlock: fix: Send update message to viewport
Browse files Browse the repository at this point in the history
* Sends the update message to the viewport, which was missed.
  • Loading branch information
eklairs committed May 12, 2024
1 parent aa7038f commit c35f4ae
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 202 deletions.
108 changes: 55 additions & 53 deletions tlock/models/dashboard/tokens/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ type AddTokenScreen struct {

// Initializes a new screen of AddTokenScreen
func InitializeAddTokenScreen(folder tlockvault.Folder, vault *tlockvault.Vault) AddTokenScreen {
// Initialize form
form := BuildForm(map[string]string{})
// Initialize form
form := BuildForm(map[string]string{})

// Return
return AddTokenScreen{
form: form,
vault: vault,
folder: folder,
viewport: IntoViewport(addTokenAscii, addTokenDesc, form),
}
form: form,
vault: vault,
folder: folder,
viewport: IntoViewport(addTokenAscii, addTokenDesc, form),
}
}

// Init
Expand All @@ -98,56 +98,58 @@ func (screen AddTokenScreen) Init() tea.Cmd {

// Update
func (screen AddTokenScreen) Update(msg tea.Msg, manager *modelmanager.ModelManager) (modelmanager.Screen, tea.Cmd) {
// Commands
cmds := make([]tea.Cmd, 0)

// Match
switch msgType := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msgType, addTokenKeys.GoBack):
manager.PopScreen()
}

case form.FormSubmittedMsg:
// Get token
token := TokenFromFormData(msgType.Data)

// Make statusbar message
statusBarMessage := fmt.Sprintf("Successfully added token for %s", token.Account)

if token.Account == "" {
statusBarMessage = fmt.Sprintf("Successfully added token (no account name)")
}

// Require refresh of folders and tokens list
cmds = append(
cmds,
func() tea.Msg { return tlockmessages.RefreshFoldersMsg{} },
func() tea.Msg { return tlockmessages.RefreshTokensMsg{} },
func() tea.Msg { return components.StatusBarMsg{Message: statusBarMessage} },
)

// Add
screen.vault.AddTokenFromToken(screen.folder.Name, token)

// Break
manager.PopScreen()
}

// Let the form handle its update
cmds = append(cmds, screen.form.Update(msg, screen.vault))

// Update the viewport
DisableBasedOnType(&screen.form)
UpdateViewport(addTokenAscii, addTokenDesc, &screen.viewport, screen.form)

// Return
// Commands
cmds := make([]tea.Cmd, 0)

// Match
switch msgType := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msgType, addTokenKeys.GoBack):
manager.PopScreen()
}

case form.FormSubmittedMsg:
// Get token
token := TokenFromFormData(msgType.Data)

// Make statusbar message
statusBarMessage := fmt.Sprintf("Successfully added token for %s", token.Account)

if token.Account == "" {
statusBarMessage = fmt.Sprintf("Successfully added token (no account name)")
}

// Require refresh of folders and tokens list
cmds = append(
cmds,
func() tea.Msg { return tlockmessages.RefreshFoldersMsg{} },
func() tea.Msg { return tlockmessages.RefreshTokensMsg{} },
func() tea.Msg { return components.StatusBarMsg{Message: statusBarMessage} },
)

// Add
screen.vault.AddTokenFromToken(screen.folder.Name, token)

// Break
manager.PopScreen()
}

// Let the form handle its update
cmds = append(cmds, screen.form.Update(msg, screen.vault))

// Update the viewport
DisableBasedOnType(&screen.form)
UpdateViewport(addTokenAscii, addTokenDesc, &screen.viewport, screen.form)

// Send the update message to the viewport
screen.viewport, _ = screen.viewport.Update(msg)

// Return
return screen, tea.Batch(cmds...)
}

// View
func (screen AddTokenScreen) View() string {
return screen.viewport.View()
}

124 changes: 62 additions & 62 deletions tlock/models/dashboard/tokens/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,41 @@ import (

// Adds a validator to input to only accept int
func onlyInt(textinput textinput.Model) textinput.Model {
return utils.ValidatorInteger(textinput)
return utils.ValidatorInteger(textinput)
}

// Validator for period
func periodValidator(_ *tlockvault.Vault, period string) error {
if num, err := strconv.ParseInt(period, 10, 64); err == nil {
if num < 1 {
return errors.New("Period cannot be < 1")
}
}
if num, err := strconv.ParseInt(period, 10, 64); err == nil {
if num < 1 {
return errors.New("Period cannot be < 1")
}
}

return nil
return nil
}

// Validator for digits
func digitValidator(_ *tlockvault.Vault, digits string) error {
if num, err := strconv.ParseInt(digits, 10, 64); err == nil {
if num < 1 {
return errors.New("Digits cannot be < 1")
}

if num > 10 {
return errors.New("Digits cannot be > 10")
}
}
if num, err := strconv.ParseInt(digits, 10, 64); err == nil {
if num < 1 {
return errors.New("Digits cannot be < 1")
}

if num > 10 {
return errors.New("Digits cannot be > 10")
}
}

return nil
return nil
}

// Secret validator
func secretValidator(vault *tlockvault.Vault, secret string) error {
// Validate
_, err := vault.ValidateToken(secret)
// Validate
_, err := vault.ValidateToken(secret)

// Return
// Return
return err
}

Expand All @@ -62,33 +62,33 @@ func BuildForm(values map[string]string) tlockform.Form {
// Initialize form
form := tlockform.New()

// Sets the value of the input box
v := func (input textinput.Model, key string) textinput.Model {
if value, ok := values[key]; ok {
input.SetValue(value)
}
// Sets the value of the input box
v := func(input textinput.Model, key string) textinput.Model {
if value, ok := values[key]; ok {
input.SetValue(value)
}

return input
}
return input
}

// Add items
form.AddInput("account", "Account Name", "Name of the account, like John Doe", v(components.InitializeInputBox("Account name goes here..."), "account"), []tlockform.Validator{})
form.AddInput("issuer", "Issuer", "Name of the issuer, like GitHub", v(components.InitializeInputBox("Issuer name goes here..."), "issuer"), []tlockform.Validator{})
form.AddInput("secret", "Secret", "The secret provided by the issuer", v(components.InitializeInputBox("The secret goes here..."), "secret"), []tlockform.Validator{ secretValidator })
form.AddInput("secret", "Secret", "The secret provided by the issuer", v(components.InitializeInputBox("The secret goes here..."), "secret"), []tlockform.Validator{secretValidator})
form.AddOption("type", "Type", "Type of the token", []string{"TOTP", "HOTP"})
form.AddOption("hash", "Hash", "Hashing algorithm for the token", []string{"SHA1", "SHA256", "SHA512"})
form.AddInput("period", "Period", "Time to refresh the token", v(onlyInt(components.InitializeInputBoxCustomWidth("Time in seconds...", 24)), "period"), []tlockform.Validator{ periodValidator })
form.AddInput("period", "Period", "Time to refresh the token", v(onlyInt(components.InitializeInputBoxCustomWidth("Time in seconds...", 24)), "period"), []tlockform.Validator{periodValidator})
form.AddInput("counter", "Initial counter", "Initial counter for HOTP token", v(onlyInt(components.InitializeInputBoxCustomWidth("Initial counter...", 24)), "counter"), []tlockform.Validator{})
form.AddInput("digits", "Digits", "Number of digits", v(onlyInt(components.InitializeInputBoxCustomWidth("Number of digits goes here...", 24)), "digits"), []tlockform.Validator{ digitValidator })

// Set default values
form.Default = map[string]string {
"account": "",
"issuer": "",
"period": "30",
"counter": "0",
"digits": "6",
}
form.AddInput("digits", "Digits", "Number of digits", v(onlyInt(components.InitializeInputBoxCustomWidth("Number of digits goes here...", 24)), "digits"), []tlockform.Validator{digitValidator})

// Set default values
form.Default = map[string]string{
"account": "",
"issuer": "",
"period": "30",
"counter": "0",
"digits": "6",
}

// Disable the counter box
form.Disable("counter")
Expand Down Expand Up @@ -147,7 +147,7 @@ func IntoViewport(ascii, description string, form tlockform.Form) viewport.Model
content := RenderForm(ascii, description, form)

// Initialize
viewport := viewport.New(85, min(height, lipgloss.Height(content)))
viewport := utils.DisableViewportKeys(viewport.New(85, min(height, lipgloss.Height(content))))
viewport.SetContent(content)

// Return
Expand Down Expand Up @@ -183,35 +183,35 @@ func DisableBasedOnType(form *tlockform.Form) {

// Converts string to token type
func toTokenType(tokentype string) tlockvault.TokenType {
if tokentype == "HOTP" {
return tlockvault.TokenTypeHOTP
}
if tokentype == "HOTP" {
return tlockvault.TokenTypeHOTP
}

return tlockvault.TokenTypeTOTP
return tlockvault.TokenTypeTOTP
}

// Converts string to opt algorithm
func toOtpAlgorithm(algo string) otp.Algorithm {
switch algo {
case "SHA256":
return otp.AlgorithmSHA256
case "SHA512":
return otp.AlgorithmSHA512
default:
return otp.AlgorithmSHA1
}
switch algo {
case "SHA256":
return otp.AlgorithmSHA256
case "SHA512":
return otp.AlgorithmSHA512
default:
return otp.AlgorithmSHA1
}
}

// Create a token from form data
func TokenFromFormData(data map[string]string) tlockvault.Token {
return tlockvault.Token{
Issuer: data["issuer"],
Account: data["account"],
Secret: data["secret"],
Type: toTokenType(data["type"]),
InitialCounter: utils.ToInt(data["counter"]),
Period: utils.ToInt(data["period"]),
Digits: utils.ToInt(data["digits"]),
HashingAlgorithm: toOtpAlgorithm(data["hash"]),
}
return tlockvault.Token{
Issuer: data["issuer"],
Account: data["account"],
Secret: data["secret"],
Type: toTokenType(data["type"]),
InitialCounter: utils.ToInt(data["counter"]),
Period: utils.ToInt(data["period"]),
Digits: utils.ToInt(data["digits"]),
HashingAlgorithm: toOtpAlgorithm(data["hash"]),
}
}
Loading

0 comments on commit c35f4ae

Please sign in to comment.