Skip to content

Commit

Permalink
add: Mask input values (#15)
Browse files Browse the repository at this point in the history
* add: Mask input values

To prevent any shoulder surfing when inputting the authentication
values, mask the input.

Closes #13
  • Loading branch information
jacobbednarz committed Oct 18, 2020
1 parent da03e11 commit 70ee123
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
14 changes: 11 additions & 3 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/terminal"

"github.com/99designs/keyring"
"github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -45,10 +46,13 @@ var addCmd = &cobra.Command{
emailAddress = strings.TrimSpace(emailAddress)

fmt.Print("Authentication value (API key or API token): ")
authValue, _ := reader.ReadString('\n')
authValue = strings.TrimSpace(authValue)
byteAuthValue, err := terminal.ReadPassword(0)
if err != nil {
log.Fatalf("\nunable to read authentication value: %s", err)
}
authValue := string(byteAuthValue)

authType, err := determineAuthType(authValue)
authType, err := determineAuthType(strings.TrimSpace(authValue))
if err != nil {
log.Fatalf("failed to detect authentication type: %s", err)
}
Expand Down Expand Up @@ -78,13 +82,17 @@ var addCmd = &cobra.Command{
Key: fmt.Sprintf("%s-%s", profileName, authType),
Data: []byte(authValue),
})

fmt.Print("\nDone! Credentials have been set and are now ready for use!")
},
}

func determineAuthType(s string) (string, error) {
if apiTokenMatch, _ := regexp.MatchString("[A-Za-z0-9-_]{40}", s); apiTokenMatch {
log.Debug("API token detected")
return "api_token", nil
} else if apiKeyMatch, _ := regexp.MatchString("[0-9a-f]{37}", s); apiKeyMatch {
log.Debug("API key detected")
return "api_key", nil
} else {
return "", errors.New("invalid API token or API key format")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/sirupsen/logrus v1.2.0
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.4.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/tools v0.0.0-20200915201639-f4cefd1cb5ba
google.golang.org/api v0.32.0 // indirect
gopkg.in/ini.v1 v1.61.0
Expand Down

0 comments on commit 70ee123

Please sign in to comment.