package main import "log" import "net/http" import "io/ioutil" import "github.com/tidwall/gjson" import pb "git.wit.com/wit/witProtobuf" func main() { ge := pb.MakeOkResponse() c := pb.MakeDefaultConfig() ge.Account = c.Accounts[0] ge.Account.Email = "jcarr@wit.com" ge.Account.Password = "yowzayowza" ge.Account.URL = "http://stackapi:4000/" // ge.Account.Token = "eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJ4IjozLCJyIjoiIiwiY3NyZiI6Ijloa0VYa2M0YURxTlVqSElGU2VJYUJoOCIsImV4cCI6MTU1OTY3OTAxMywiaXNzIjoid2l0Iiwic3ViIjoiamNhcnJAd2l0LmNvbSJ9.eCfhg3_VYBZh1ve69p0Op4U9L2T9CAZY5qPtx7vNswe3TB-y1nz2X2vw4AzPwfy4fACKeQGkJmWGedt1NKQP-WaZNVtGpgX9g0yvloCob2OMQwsLhq3X7e83weiK_Jlk" ge.Account.Token = "badtoken" junkEvent := processLoginEvent(ge) log.Println("processAddEvent() junkEvent =", junkEvent) log.Println("processAddEvent() START ge.Account =", ge.Account) tmp := string(GETbody(ge, "http://stackapi:4000/me")) log.Println("processAddEvent() recieved json=", tmp) tmp = string(GETbody(ge, "http://stackapi:4000/clusters")) log.Println("processAddEvent() recieved json=", tmp) url := ge.Account.URL + "vms/jcarr.com?count=1&cpu=2&ram=512&disk=25" tmp = string(POSTbody(ge, url)) log.Println("processAddEvent() recieved json=", tmp) } // 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 "" } 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) }