From 2d3a0a900a835572e942409ba34118b3902f62e7 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 26 May 2019 08:04:12 +0000 Subject: [PATCH] lots of cleanups to token handling. protobuf fixes Signed-off-by: Jeff Carr --- gorilla-server/apiJSON.go | 11 +++++++++-- gorilla-server/main.go | 40 +++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/gorilla-server/apiJSON.go b/gorilla-server/apiJSON.go index 075d5ca..fd63b16 100644 --- a/gorilla-server/apiJSON.go +++ b/gorilla-server/apiJSON.go @@ -43,9 +43,16 @@ func fetchJSON(GETorPOST string, token string, a string) []byte { } func getToken(ge *pb.Event) string { - if ge == nil {return ""} + if ge == nil { + log.Println("getToken() has ge = nil") + return "" + } + if ge.Account == nil { + log.Println("getToken() has ge.Account = nil") + return "" + } - url := ge.Account.URL + "/auth/login" + "?email=" + ge.Email + "&password=" + ge.Password + url := ge.Account.URL + "/auth/login" + "?email=" + ge.Account.Email + "&password=" + ge.Account.Password log.Println("URL =", url) var jsonStr = []byte(`{}`) diff --git a/gorilla-server/main.go b/gorilla-server/main.go index 9942ed7..2a20d21 100644 --- a/gorilla-server/main.go +++ b/gorilla-server/main.go @@ -26,7 +26,7 @@ type network struct { func checkLogin(ge *pb.Event) string { if (origLoginCheck(ge) == true) { log.Println("origLoginCheck() LOGIN WORKED") - return ge.Token + return ge.Account.Token } log.Println("origLoginCheck() LOGIN FAILED. ATTEMPT getToken(ge)") log.Println("origLoginCheck() LOGIN FAILED. ATTEMPT getToken(ge)") @@ -226,13 +226,13 @@ func main() { func processGetEvent(ge *pb.Event) *pb.Event { log.Println("processGetEvent() START") - log.Println("processGetEvent() ge.Token =", ge.Token) - log.Println("processGetEvent() ge.Username =", ge.Username) - log.Println("processGetEvent() ge.Email =", ge.Email) + log.Println("processGetEvent() ge.Account.Token =", ge.Account.Token) + log.Println("processGetEvent() ge.Account.Username =", ge.Account.Username) + log.Println("processGetEvent() ge.Account.Email =", ge.Account.Email) e := pb.Event{} - networks := fetchNetworks(ge.Token) + networks := fetchNetworks(ge.Account.Token) for i, network := range(networks) { if i >= MAX_NETS { break @@ -244,7 +244,7 @@ func processGetEvent(ge *pb.Event) *pb.Event { e.Networks = append(e.Networks, &enetwork) - vms := fetchVMs(ge.Token, network.Subdomain) + vms := fetchVMs(ge.Account.Token, network.Subdomain) for j, vm := range(vms) { if j >= MAX_VMS { break @@ -266,7 +266,7 @@ func processGetEvent(ge *pb.Event) *pb.Event { func processLoginEvent(ge *pb.Event) *pb.Event { log.Println("processLoginEvent() START") - log.Println("processLoginEvent() ge.Token =", ge.Token) + log.Println("processLoginEvent() ge.Account.Token =", ge.Account.Token) log.Println("processLoginEvent() NEED TO LOGIN HERE OR CHECK TOKEN WORKS") @@ -276,29 +276,29 @@ func processLoginEvent(ge *pb.Event) *pb.Event { e.Comment = "LOGIN FAILED" return e } + log.Println("processLoginEvent() LOGIN WORKED") log.Println("processLoginEvent() LOGIN WORKED") log.Println("processLoginEvent() LOGIN WORKED") e := pb.MakeOkResponse() - e.Token = token + e.Account = ge.Account + if (e.Account.Token != token) { + log.Println("processLoginEvent() GENERATED NEW TOKEN old =", e.Account.Token) + log.Println("processLoginEvent() GENERATED NEW TOKEN new =", token) + } + e.Account.Token = token e.Comment = "LOGIN WORKED" return e } func processAddEvent(ge *pb.Event) *pb.Event { log.Println("processAddEvent() START") - log.Println("processAddLoginEvent() ge.Token =", ge.Token) + log.Println("processAddLoginEvent() ge.Account.Token =", ge.Account.Token) log.Println("processAddEvent() END") return nil } /* - var login string - login = "auth/login?email=jcarr@wit.com&password=yowzayowza" - raw = fetchPOST("badtoken", login) - log.Println("raw =", string(raw)) -*/ - func jsonAddEvent(ge *pb.Event) bool { var url string var httpType string @@ -306,7 +306,6 @@ func jsonAddEvent(ge *pb.Event) bool { if ge.Type == pb.Event_LOGIN { url = API + "me" } else if ge.Type == pb.Event_ADD { - // stackapi:4000/vms/jcarr.com?count=1&cpu=2&ram=512&disk=25 url = API + "vms/jcarr.com?count=1&cpu=2&ram=512&disk=25" httpType = "POST" } else if ge.Type == pb.Event_MIGRATE { @@ -352,6 +351,7 @@ func jsonAddEvent(ge *pb.Event) bool { } return false } +*/ // This is the 'orig' code // TODO: phase this out for more common code @@ -367,7 +367,7 @@ func origLoginCheck(ge *pb.Event) bool { log.Println("can't make new req") return false } - req.Header.Set("X-Wit-Auth", ge.Token) + req.Header.Set("X-Wit-Auth", ge.Account.Token) client := &http.Client{} resp, err := client.Do(req) if err != nil { @@ -379,7 +379,7 @@ func origLoginCheck(ge *pb.Event) bool { log.Println("can't read resp") return false } - log.Printf("RESPONSE =", body) + log.Printf("RESPONSE =", string(body)) if body == nil { log.Println("RESPONSE is nil") return false @@ -389,8 +389,8 @@ func origLoginCheck(ge *pb.Event) bool { log.Println("body len = ", len) email := gjson.Get(string(body), "email") log.Println("email = ", email.String()) - log.Println("ge.Username = ", ge.Username) - if (email.String() == ge.Username) { + log.Println("ge.Account.Username = ", ge.Account.Username) + if (email.String() == ge.Account.Username) { log.Println("\tTHIS TOTALLY WORKED MAN") return true }