Skip to content

Commit

Permalink
Add change-pin command + better error handling with invalid PIN
Browse files Browse the repository at this point in the history
  • Loading branch information
Balazs Nadasdi committed Sep 29, 2016
1 parent f3f96a8 commit 2ed1307
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 20 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ This is a simple TOTP _(Time-based One-time Password)_ CLI tool. You can manage
accounts with namespaces and protect your data with a PIN _(aka password)_.

```
❯ ./bin/totp-cli
❯ totp-cli
delete Delete an account or a whole namespace
`totp-cli delete nsname`
`totp-cli delete nsname.accname`
change-pin Change PIN code
generate Generate a specific OTP
`totp-cli generate namespace.account`
help This help message :)
add-token Add new token
`totp-cli add-token`
This command will ask for the namespace, the account and the token
list List all available namespaces or accounts under a namespace
`totp-cli list` => list all namespaces
`totp-cli list myns` => list all accounts under 'myns' namespace
generate Generate a specific OTP
`totp-cli generate namespace.account`
help This help message :)
```

TODO: Documentation :D
42 changes: 30 additions & 12 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,25 @@ type CommandFunction func()
var storage *Storage

var commandHandlers map[string]CommandFunction = map[string]CommandFunction{
"generate": Command_Generate,
"help": Command_Help,
"add-token": Command_AddToken,
"list": Command_List,
"delete": Command_Delete,
"generate": Command_Generate,
"help": Command_Help,
"add-token": Command_AddToken,
"list": Command_List,
"delete": Command_Delete,
"change-pin": Command_ChangePIN,
}

var commandDescriptions map[string]string = map[string]string{
"generate": "Generate a specific OTP%NLWI%`totp-cli generate namespace.account`",
"help": "This help message :)",
"add-token": "Add new token%NLWI%`totp-cli add-token`%NLWI%This command will ask for the namespace, the account and the token",
"list": "List all available namespaces or accounts under a namespace%NLWI%`totp-cli list` => list all namespaces%NLWI%`totp-cli list myns` => list all accounts under 'myns' namespace",
"delete": "Delete an account or a whole namespace%NLWI%`totp-cli delete nsname`%NLWI%`totp-cli delete nsname.accname`",
"generate": "Generate a specific OTP%NLWI%`totp-cli generate namespace.account`",
"help": "This help message :)",
"add-token": "Add new token%NLWI%`totp-cli add-token`%NLWI%This command will ask for the namespace, the account and the token",
"list": "List all available namespaces or accounts under a namespace%NLWI%`totp-cli list` => list all namespaces%NLWI%`totp-cli list myns` => list all accounts under 'myns' namespace",
"delete": "Delete an account or a whole namespace%NLWI%`totp-cli delete nsname`%NLWI%`totp-cli delete nsname.accname`",
"change-pin": "Change PIN code",
}

func prepareStorage() {
pin := AskPIN(32)
pin := AskPIN(32, "")

currentUser, err := user.Current()
check(err)
Expand Down Expand Up @@ -127,7 +129,7 @@ func Command_AddToken() {

account = &Account{Name: accName, Token: token}

namespace.Accounts = []*Account{account}
namespace.Accounts = append(namespace.Accounts, account)

storage.Save()
}
Expand Down Expand Up @@ -183,11 +185,27 @@ func Command_Delete() {
}
}

func Command_ChangePIN() {
prepareStorage()
newPIN := AskPIN(32, "New PIN")
newPINConfirm := AskPIN(32, "Again")

if !CheckPINConfirm(newPIN, newPINConfirm) {
fmt.Println("New PIN and the confirm mismatch!")
return
}

storage.PIN = newPIN
storage.Save()
}

func Command_NotImplementedYet() {
fmt.Println(" -- Not Implemented Yet --")
}

func askForAddTokenDetails() (namespace, account, token string) {
namespace = flag.Arg(1)
account = flag.Arg(2)
for len(namespace) < 1 {
namespace = Ask("Namespace")
}
Expand Down
7 changes: 6 additions & 1 deletion storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
)

type Storage struct {
Expand Down Expand Up @@ -94,7 +96,10 @@ func (s *Storage) parse(decodedData []byte) {
}

err := json.Unmarshal(decodedData, &parsedData)
check(err)
if err != nil {
fmt.Println("Something went wrong. Maybe this PIN is not a valid one.")
os.Exit(1)
}

var namespaces []*Namespace

Expand Down
34 changes: 31 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ import (

func check(err error) {
if err != nil {
panic(err)
fmt.Println(err.Error())
os.Exit(1)
}
}

func AskPIN(length int) []byte {
func AskPIN(length int, prompt string) []byte {
var pin []byte = make([]byte, length, length)
var text string

if prompt == "" {
prompt = "PIN"
}

text = os.Getenv("PIN")

if len(text) < 1 {
os.Stderr.Write([]byte("PIN: "))
prompt = fmt.Sprintf("%s: ", prompt)
os.Stderr.Write([]byte(prompt))
stdin := int(os.Stdin.Fd())
if terminal.IsTerminal(stdin) {
var lineBytes []byte
Expand Down Expand Up @@ -66,3 +72,25 @@ func Confirm(prompt string) bool {

return (text == "y" || text == "yes" || text == "sure")
}

func CheckPINConfirm(pin, confirm []byte) bool {
if pin == nil && confirm == nil {
return true
}

if pin == nil || confirm == nil {
return false
}

if len(pin) != len(confirm) {
return false
}

for i := range pin {
if pin[i] != confirm[i] {
return false
}
}

return true
}

0 comments on commit 2ed1307

Please sign in to comment.