forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setapicreds.go
93 lines (88 loc) · 2.13 KB
/
setapicreds.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
package cmd
import (
"bufio"
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/OpenBazaar/openbazaar-go/repo"
"github.com/ipfs/go-ipfs/repo/fsrepo"
"golang.org/x/crypto/ssh/terminal"
"io/ioutil"
"os"
"path"
"strings"
"syscall"
)
type SetAPICreds struct {
DataDir string `short:"d" long:"datadir" description:"specify the data directory to be used"`
Testnet bool `short:"t" long:"testnet" description:"config file is for testnet node"`
}
func (x *SetAPICreds) Execute(args []string) error {
// Set repo path
repoPath, err := repo.GetRepoPath(x.Testnet)
if err != nil {
return err
}
if x.DataDir != "" {
repoPath = x.DataDir
}
r, err := fsrepo.Open(repoPath)
if err != nil {
log.Error(err)
return err
}
configFile, err := ioutil.ReadFile(path.Join(repoPath, "config"))
if err != nil {
return err
}
apiCfg, err := repo.GetAPIConfig(configFile)
if err != nil {
log.Error(err)
return err
}
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter username: ")
username, _ := reader.ReadString('\n')
var pw string
for {
fmt.Print("Enter a veerrrry strong password: ")
bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin))
fmt.Println("")
resp := string(bytePassword)
if len(resp) < 8 {
fmt.Println("You call that a password? Try again.")
} else if resp != "" {
pw = resp
break
} else {
fmt.Println("Seriously, enter a password.")
}
}
for {
fmt.Print("Confirm your password: ")
bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin))
fmt.Println("")
resp := string(bytePassword)
if resp == pw {
break
} else {
fmt.Println("Quit effin around. Try again.")
}
}
pw = strings.Replace(pw, "'", "''", -1)
if strings.Contains(username, "\r\n") {
apiCfg.Username = strings.Replace(username, "\r\n", "", -1)
} else if strings.Contains(username, "\n") {
apiCfg.Username = strings.Replace(username, "\n", "", -1)
}
apiCfg.Authenticated = true
h := sha256.Sum256([]byte(pw))
apiCfg.Password = hex.EncodeToString(h[:])
if len(apiCfg.AllowedIPs) == 0 {
apiCfg.AllowedIPs = []string{}
}
if err := r.SetConfigKey("JSON-API", apiCfg); err != nil {
return err
}
return nil
}