137 lines
3.5 KiB
Go
137 lines
3.5 KiB
Go
package main
|
|
|
|
import "log"
|
|
import "net/http"
|
|
import "io/ioutil"
|
|
|
|
import "github.com/tidwall/gjson"
|
|
import pb "git.wit.com/wit/witProtobuf"
|
|
|
|
// check if a user can login here
|
|
// tries generates a new TOKEN if the old one doesn't work
|
|
// fails with the appropriate protobuf response to send
|
|
// to the GUI client
|
|
func processLoginEvent(ge *pb.Event) *pb.Event {
|
|
log.Println("processLoginEvent() LOGIN START")
|
|
if (checkLogin(ge) == false) {
|
|
e := pb.MakeFailResponse()
|
|
e.Comment = "LOGIN FAILED"
|
|
log.Println(e.Comment)
|
|
return e
|
|
}
|
|
|
|
e := pb.MakeOkResponse()
|
|
e.Account = ge.Account
|
|
e.Comment = "processLoginEvent() LOGIN WORKED"
|
|
log.Println(e.Comment)
|
|
return e
|
|
}
|
|
|
|
func checkLogin(ge *pb.Event) bool {
|
|
if (ge.Account == nil) {
|
|
log.Println("checkLogin() pb.Event.Account == nil ERROR")
|
|
return false
|
|
}
|
|
|
|
url := ge.Account.URL + "me"
|
|
json := string(GETbody(ge, url))
|
|
if (json != "") {
|
|
email := gjson.Get(json, "email")
|
|
log.Println("origLoginCheck() ", "email = ", email.String())
|
|
log.Println("origLoginCheck() ", "ge.Account.Username = ", ge.Account.Username)
|
|
if (email.String() == ge.Account.Username) {
|
|
ge.Comment = "origLoginCheck() THIS TOTALLY WORKED MAN"
|
|
}
|
|
log.Println(ge.Comment)
|
|
return true
|
|
}
|
|
log.Println("checkLogin() LOGIN FAILED. ATTEMPT updateToken(ge)")
|
|
return updateToken(ge)
|
|
}
|
|
|
|
func updateToken(ge *pb.Event) bool {
|
|
if ge == nil {
|
|
return false
|
|
}
|
|
|
|
url := ge.Account.URL + "auth/login" + "?email=" + ge.Account.Email + "&password=" + ge.Account.Password
|
|
// json := newFetchBody(ge, "POST", url)
|
|
json := POSTbody(ge, url)
|
|
|
|
jwt := gjson.Get(json, "jwt")
|
|
if (jwt.String() == "") {
|
|
ge.Comment = "updateToken() GOT TOKEN == nil"
|
|
log.Println("updateToken() ge.Comment =", ge.Comment)
|
|
return false
|
|
}
|
|
if (jwt.String() != ge.Account.Token) {
|
|
log.Println("updateToken() GOT NEW TOKEN", jwt)
|
|
ge.Account.Token = jwt.String()
|
|
}
|
|
log.Println("updateToken() END")
|
|
return true
|
|
}
|
|
|
|
func GETbody(ge *pb.Event, URL string) string {
|
|
// req, err := http.NewRequest("GET", ge.Account.URL + "clusters", nil)
|
|
req, err := http.NewRequest("GET", URL, nil)
|
|
if err != nil {
|
|
log.Println("GETbody() can't make new req")
|
|
return ""
|
|
}
|
|
req.Header.Set("X-Wit-Auth", ge.Account.Token)
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
log.Printf("Can't exec the req to list networks: %s", err)
|
|
return ""
|
|
}
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
log.Println("can't read resp")
|
|
return ""
|
|
}
|
|
// log.Printf("GETbody() body =", string(body))
|
|
if body == nil {
|
|
log.Println("networks is nil")
|
|
return ""
|
|
}
|
|
return string(body)
|
|
}
|
|
|
|
func POSTbody(ge *pb.Event, URL string) string {
|
|
// req, err := http.NewRequest("GET", ge.Account.URL + "clusters", nil)
|
|
log.Println("POSTbody() url =", URL)
|
|
req, err := http.NewRequest("POST", URL, nil)
|
|
if err != nil {
|
|
log.Println("POSTbody() can't make new req")
|
|
return ""
|
|
}
|
|
|
|
token := ge.Account.Token
|
|
if (token == "") {
|
|
token = "POSTbodyEmptyToken"
|
|
}
|
|
|
|
log.Println("POSTbody() ge.Account.Token =", token)
|
|
req.Header.Set("X-Wit-Auth", ge.Account.Token)
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
log.Printf("POSTbody() Can't exec the req to list networks: %s", err)
|
|
return ""
|
|
}
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
log.Println("POSTbody() can't read resp")
|
|
return ""
|
|
}
|
|
// log.Printf("GETbody() body =", string(body))
|
|
if body == nil {
|
|
log.Println("POSTbody() body is nil")
|
|
return ""
|
|
}
|
|
log.Println("POSTbody() END body =", string(body))
|
|
return string(body)
|
|
}
|