-
Notifications
You must be signed in to change notification settings - Fork 1
/
account.go
129 lines (114 loc) · 3.08 KB
/
account.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
package cmd
import (
"bufio"
"errors"
"fmt"
"github.com/spf13/cobra"
"golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/ssh/terminal"
"io"
"log"
"os"
"strings"
"syscall"
)
var (
accountCmd = &cobra.Command{
Use: "account",
Short: "Add/Delete/Print account",
}
addAccountCmd = &cobra.Command{
Use: "add",
Short: "Create new account",
Run: addAccount,
}
deleteAccountCmd = &cobra.Command{
Use: "delete",
Short: "Delete existing account",
Args: cobra.NoArgs,
Run: deleteAccount,
}
printUsernamesCmd = &cobra.Command{
Use: "print",
Short: "Show all usernames",
Run: getAccounts,
}
)
type credentials struct {
username string
password []byte
}
func init() {
accountCmd.AddCommand(addAccountCmd)
accountCmd.AddCommand(deleteAccountCmd)
accountCmd.AddCommand(printUsernamesCmd)
rootCmd.AddCommand(accountCmd)
}
func addAccount(cmd *cobra.Command, args []string) {
pr := terminalPasswordReader{}
credentials, err := getCredentials(pr, os.Stdin)
if err != nil {
log.Fatalln(err)
}
hashedPassword, err := bcrypt.GenerateFromPassword(credentials.password, 10)
if err != nil {
log.Fatalf("Failed hashing password, error msg: %v", err)
}
err = AccountDB.CreateAccount(credentials.username, hashedPassword)
if err != nil {
log.Fatalf("Failed creating new account, error msg: %v", err)
}
}
func deleteAccount(cmd *cobra.Command, args []string) {
pr := terminalPasswordReader{}
credentials, err := getCredentials(pr, os.Stdin)
if err != nil {
log.Fatalln(err)
}
account, err := AccountDB.GetAccount(credentials.username)
if err != nil {
log.Fatalf("Could not delete account for user: %v", credentials.username)
}
if err := bcrypt.CompareHashAndPassword([]byte(account.Password), credentials.password); err != nil {
log.Fatalln("Username and password don't match")
}
AccountDB.DeleteAccount(credentials.username)
fmt.Printf("Account for user: %v deleted", credentials.username)
}
func getAccounts(cmd *cobra.Command, args []string) {
usernames := AccountDB.GetUsernames()
if len(usernames) == 0 {
fmt.Println("DB is empty")
return
}
fmt.Println("Current Usernames in DB:")
for _, username := range usernames {
fmt.Println("> " + username)
}
}
type passwordReader interface {
ReadPassword(fd int) ([]byte, error)
}
type terminalPasswordReader struct{}
func (pr terminalPasswordReader) ReadPassword(fd int) ([]byte, error) {
return terminal.ReadPassword(fd)
}
func getCredentials(pr passwordReader, input io.Reader) (*credentials, error) {
reader := bufio.NewReader(input)
fmt.Print("Enter Username: ")
username, _ := reader.ReadString('\n')
username = strings.TrimSpace(username)
if len(username) == 0 {
return &credentials{}, errors.New("Empty username")
}
fmt.Print("Enter Password: ")
bytePassword, err := pr.ReadPassword(int(syscall.Stdin))
if err != nil {
return &credentials{}, fmt.Errorf("Failed reading password, error msg: %v", err)
}
password := string(bytePassword)
if len(password) < 5 {
return &credentials{}, errors.New("Password must be at least 5 chars")
}
return &credentials{username, bytePassword}, nil
}