/
auth.go
39 lines (33 loc) · 881 Bytes
/
auth.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
package main
import (
"fmt"
"github.com/howeyc/gopass"
"pk/api"
)
// Reads oauth tokens from disk if exist, else prompt for login
func authorize(force bool) {
conf, err := getRc()
client, err = api.NewPKClient(conf.URL)
if force || err != nil {
var username, password string
fmt.Printf("Email: ")
fmt.Scanln(&username)
fmt.Printf("Password: ")
password = string(gopass.GetPasswd())
err = client.Authorize(username, password) // should return json on unauthorized
if err != nil {
fmt.Println("Authorization error:", err)
return
}
conf.User = username
conf.AccessToken = client.AccessToken
conf.Expiration = client.TokenExpiration
if err := conf.saveRc(); err != nil {
panic(err)
}
} else {
client.AccessToken = conf.AccessToken
client.TokenExpiration = conf.Expiration
}
return
}