/
login.go
167 lines (134 loc) · 3.46 KB
/
login.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"bufio"
"fmt"
"net/http"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/howeyc/gopass"
)
const (
gojazzDataDir = ".gojazz"
credentialsFile = "credentials.txt"
)
func loginOp() {
usr, err := user.Current()
if err != nil {
panic(err)
}
credentialsFilePath := filepath.Join(usr.HomeDir, gojazzDataDir, credentialsFile)
// Remove existing credentials first
err = os.RemoveAll(credentialsFilePath)
if err != nil {
panic(err)
}
userId, password, err := getCredentials()
if err != nil {
panic(err)
}
// Test the credentials by retrieving a page
client, err := NewClient(userId, password)
if err != nil {
panic(err)
}
request, err := http.NewRequest("GET", "https://hub.jazz.net/invitations", nil)
if err != nil {
panic(err)
}
_, err = client.Do(request)
if err != nil {
jazzErr, ok := err.(*JazzError)
if ok && jazzErr.StatusCode == 401 {
fmt.Printf("Not logged in, check your credentials and try again.\n")
return
}
panic(err)
}
fmt.Printf("Logged in\n")
err = storeCredentials(userId, password)
if err != nil {
panic(err)
}
}
func isLoggedIn() bool {
usr, err := user.Current()
if err != nil {
return false
}
gojazzDir := filepath.Join(usr.HomeDir, gojazzDataDir)
credentialFilePath := filepath.Join(gojazzDir, credentialsFile)
s, _ := os.Stat(credentialFilePath)
return s != nil
}
func getCredentials() (string, string, error) {
// Is there a credentials file in the user's home directory?
usr, err := user.Current()
if err != nil {
return "", "", err
}
gojazzDir := filepath.Join(usr.HomeDir, gojazzDataDir)
credentialFilePath := filepath.Join(gojazzDir, credentialsFile)
f, err := os.Open(credentialFilePath)
if err != nil {
fmt.Printf("We need your credentials for this operation.")
fmt.Printf("You can avoid this prompt next time by using the 'gojazz login' command.\n")
fmt.Println()
// No credentials file, we need to prompt
fmt.Printf("User ID: ")
reader := bufio.NewReader(os.Stdin)
userId, _ := reader.ReadString('\n')
userId = strings.TrimSpace(userId)
fmt.Printf("Password: ")
password := string(gopass.GetPasswd())
return userId, password, nil
}
reader := bufio.NewReader(f)
userId, err := reader.ReadString('\n')
if err != nil {
return "", "", err
}
userId = strings.TrimSpace(userId)
password, _ := reader.ReadString('\n')
password = strings.TrimSpace(password)
return userId, password, nil
}
func storeCredentials(userId string, password string) error {
// Is there a credentials file in the user's home directory?
usr, err := user.Current()
if err != nil {
return err
}
gojazzDir := filepath.Join(usr.HomeDir, gojazzDataDir)
credentialFilePath := filepath.Join(gojazzDir, credentialsFile)
s, _ := os.Stat(gojazzDir)
if s == nil {
err = os.Mkdir(gojazzDir, 0700)
if err != nil {
return err
}
}
credentialFile, err := os.Create(credentialFilePath)
if err != nil {
return err
}
credentialFile.Close()
err = os.Chmod(credentialFilePath, 0600)
if err != nil {
return err
}
// Open the file again with appropriate permissions
credentialFile, err = os.OpenFile(credentialFilePath, os.O_WRONLY, 0600)
if err != nil {
return err
}
_, err = credentialFile.WriteString(userId + "\n")
_, err = credentialFile.WriteString(password)
if err != nil {
return err
}
credentialFile.Close()
fmt.Printf("You credentials have been stored in %v and protected using operating system permissions.\n", credentialFilePath)
return nil
}