-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
129 lines (106 loc) · 2.81 KB
/
auth.go
File metadata and controls
129 lines (106 loc) · 2.81 KB
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 main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"strings"
"github.com/automattic/go/jaguar"
)
// runSetup prompts the user for the necessary info to
// configure and run. It can be triggered directly using
// --init or will get triggered if testSetup fails
func runSetup() {
var user, pass string
// prompt user for site
conf.SiteURL = promptForURL("Enter URL for site: ")
// prompt for username
fmt.Print("Enter username: ")
user = readLine()
// prompt for password
fmt.Print("Enter password: ")
pass = readLine()
// make JWT call to fetch token
url := strings.Join([]string{conf.SiteURL, "wp-json", "jwt-auth/v1/token"}, "/")
j := jaguar.New()
j.Url(url)
j.Params.Add("username", user)
j.Params.Add("password", pass)
resp, err := j.Method("POST").Send()
if err != nil {
log.Fatal("API error authentication", err)
}
if resp.StatusCode == 403 {
log.Fatal("Error authenticating, try again.")
}
if resp.StatusCode == 404 {
log.Fatal("Auth API not found. JWT Auth plugin installed and activated?")
}
if err := json.Unmarshal(resp.Bytes, &conf); err != nil {
log.Fatal("Error parsing JSON response", string(resp.Bytes), err)
}
if conf.Token == "" {
log.Fatal("No authentication token.", resp.StatusCode, string(resp.Bytes))
}
// write out config
jsonConf, err := json.Marshal(conf)
if err != nil {
log.Warn("JSON Encoding Error", err)
} else {
err = ioutil.WriteFile("wpsync.json", jsonConf, 0644)
if err != nil {
log.Warn("Error writing wpsync.json", err)
} else {
log.Debug("wpsync.json written")
}
}
}
// testSetup confirms everything is configured and working
// includes the local directories, blog config, and auth
func testSetup() bool {
if conf.SiteURL == "" {
log.Warn("Site URL not set")
return false
}
if conf.Token == "" {
log.Warn("Authentication token not set")
return false
}
j := getApiFetcher("jwt-auth/v1/token/validate")
resp, err := j.Method("POST").Send()
if err != nil {
log.Warn("Error in Auth validation API", err)
}
if resp.StatusCode == 403 {
log.Warn("Authentication error. Try running --init ", string(resp.Bytes))
return false
}
return true
}
func promptForURL(prompt string) string {
var input string
fmt.Print(prompt)
input = readLine()
input = strings.TrimSuffix(input, "/")
_, err := url.ParseRequestURI(input)
if err != nil {
log.Warn("Error with URL. Be sure to include http:// prefix ,", err)
return promptForURL(prompt)
}
return input
}
func readLine() string {
// fmt.Scanln produces "unexpected newline" and exits
bufr, err := bufio.NewReader(os.Stdin).ReadBytes('\n')
if err != nil {
log.Fatal("What happened?", err)
}
if bufr[len(bufr)-2] == byte(13) {
//windows input, cut 2 last bytes
bufr = bufr[:len(bufr)-2]
return string(bufr)
}
return string(bufr[:len(bufr)-1])
}