package main // import "io" import "log" import "net/http" import "io/ioutil" import "bytes" // import "github.com/golang/protobuf/proto" // import "github.com/gorilla/websocket" // import "github.com/davecgh/go-spew/spew" // import "encoding/json" import "github.com/tidwall/gjson" import pb "git.wit.com/wit/witProtobuf" func checkLogin(ge *pb.Event) bool { if (ge.Account == nil) { log.Println("checkLogin() pb.Event.Account == nil ERROR") return false } if (origLoginCheck(ge) == true) { log.Println("checkLogin() LOGIN WORKED") return true } log.Println("checkLogin() LOGIN FAILED. ATTEMPT updateToken(ge)") return updateToken(ge) } // https://www.reddit.com/r/golang/comments/boznnz/an_alternative_to_gorilla_sessions_jeff_manage/ /* var upgrader = websocket.Upgrader{ ReadBufferSize: 4096, WriteBufferSize: 4096, EnableCompression: true, CheckOrigin: func(r *http.Request) bool { return true }, } func sendProtobuf(conn *websocket.Conn, msg *pb.Event) { log.Println("sendProtobuf() START") data, _ := proto.Marshal(msg) err := conn.WriteMessage(websocket.BinaryMessage, data) if err != nil { log.Println("sendProtobuf() write:", err) } log.Println("sendProtobuf() END") } */ func main() { ge := pb.MakeOkResponse() c := pb.MakeDefaultConfig() ge.Account = c.Accounts[0] ge.Account.Username = "jcarr@wit.com" ge.Account.Password = "yowzayowza" // ge.Account.Token = "eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJ4IjozLCJyIjoiIiwiY3NyZiI6InRBY1p2eXVJbk1YdWUxV0RSbDFIeDI5YSIsImV4cCI6MTU1OTI3MDQwMCwiaXNzIjoid2l0Iiwic3ViIjoiamNhcnJAd2l0LmNvbSJ9.bqXX_6yrUHQGYh3SEmW8ydSa9Xfqx-HIKutTN_GirwhC_VrVX1xJBcgYfjdKGegvwY7Td1vO3rs40Iz7ifcptrtdzJnDX62d_1JJPKBHUQUfnTLr2qoTgaljElFM0Q_e" ge.Account.Token = "badtoken" checkLogin(ge) 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) // stackapi:4000/vms/jcarr.com?count=1&cpu=2&ram=512&disk=25 url := ge.Account.URL + "vms/jcarr.com?count=1&cpu=2&ram=512&disk=25" json := newFetchBody(ge, "POST", url) log.Println("processAddEvent() recieved json=", json) } // should check a user can login here 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 } // This is the 'orig' code // TODO: phase this out for more common code func origLoginCheck(ge *pb.Event) bool { var url string var httpType string httpType = "GET" url = ge.Account.URL + "me" log.Println("origLoginCheck() ", httpType, url) req, err := http.NewRequest(httpType, url, nil) if err != nil { ge.Comment = "origLoginCheck() FAILED can't make new req" log.Println(ge.Comment) return false } req.Header.Set("X-Wit-Auth", ge.Account.Token) client := &http.Client{} resp, err := client.Do(req) if err != nil { ge.Comment = "origLoginCheck() FAILED Can't exec the req to list networks " + err.Error() log.Println(ge.Comment) return false } body, err := ioutil.ReadAll(resp.Body) if err != nil { ge.Comment = "origLoginCheck() FAILED can't read resp" log.Println(ge.Comment) return false } log.Printf("origLoginCheck() ", "RESPONSE =", string(body)) if body == nil { ge.Comment = "origLoginCheck() FAILED RESPONSE is nil" log.Println(ge.Comment) return false } else { len := len(body) log.Println("origLoginCheck() ", "body = ", string(body)) log.Println("origLoginCheck() ", "body len = ", len) email := gjson.Get(string(body), "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 } } ge.Comment = "origLoginCheck() FAILED " log.Println(ge.Comment) return false } func fetchGET(token string, a string) []byte { return fetchJSON("GET", token, a) } func fetchPOST(token string, a string) []byte { return fetchJSON("POST", token, a) } func fetchJSON(GETorPOST string, token string, a string) []byte { log.Println("GOT HERE: fetchJSON(): ", GETorPOST + ": " + a) req, err := http.NewRequest(token, a, nil) if err != nil { log.Fatal("can't make new req") } // always set X-Wit-Auth even on token request if (token == "") { token = "badtoken" } req.Header.Set("X-Wit-Auth", token) // needed to allow 'CORS' to work req.Header.Set("Access-Control-Allow-Origin", "*") req.Header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") req.Header.Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") log.Println("GOT HERE: fetchJSON(): token =", token) client := &http.Client{} resp, err := client.Do(req) if err != nil { log.Println("Can't exec the req to list networks: %s", err) } body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal("can't read resp") } log.Println("GOT HERE: fetchJSON() body =", string(body)) return body } 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) 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 } // url := ge.Account.URL + "/auth/login" + "?email=" + ge.Account.Email + "&password=" + ge.Account.Password func newFetchBody(ge *pb.Event, POSTorGET string, url string) string { if ge == nil { log.Println("newFetchBody() has ge = nil") ge.Comment = "newFetchBody() has ge = nil" return "" } if ge.Account == nil { log.Println("newFetchBody() has ge.Account = nil") ge.Comment = "newFetchBody() has ge.Account = nil" return "" } log.Println("newFetchBody()", POSTorGET, "URL =", url) var jsonStr = []byte(`{}`) req, err := http.NewRequest(POSTorGET, url, bytes.NewBuffer(jsonStr)) // req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Wit-Auth", "tokenrequest") if (POSTorGET == "POST") { log.Println("newFetchBody()", POSTorGET, "ADDING CORS headers") // needed to allow 'CORS' to work req.Header.Set("Access-Control-Allow-Origin", "*") req.Header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") req.Header.Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11") } client := &http.Client{} resp, err := client.Do(req) if err != nil { log.Println(err) ge.Comment = err.Error() return "" } defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) log.Println("newFetchBody() response Body:", string(body)) log.Println("newFetchBody() response Status:", resp.Status) log.Println("newFetchBody() response Headers:", resp.Header) return string(body) } 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) }