From 1cb13782e2854aa5c48469eff314dc44c26f14c0 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Tue, 28 May 2019 20:19:32 +0000 Subject: [PATCH] fixed client code example. WTF was wrong with the other code I made? Signed-off-by: Jeff Carr --- .gitignore | 1 + goclient/.client.go.swp | Bin 20480 -> 0 bytes goclient/client.go | 201 ++++------------------------------------ 3 files changed, 21 insertions(+), 181 deletions(-) delete mode 100644 goclient/.client.go.swp diff --git a/.gitignore b/.gitignore index 4e0833e..ff31c01 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.swp server/server client/client example-protobuf/example-protobuf diff --git a/goclient/.client.go.swp b/goclient/.client.go.swp deleted file mode 100644 index 57af4bffc1a83bbac07cc396e021b6256e868a45..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 20480 zcmeI3dyFKeO`NQ*0*!#zwb4S&;0z?A6xpN zYmD`(VR)_^$m6~KHYO$}CXXF029_V#PQ6%lTg7U}58RgJ`NeMAb8E$}9T-nYhNZe& z^1W)QZU^#E5|=JiP0uUU9N)L9zFS?lBvrL(TTW04FU7jMS1q#lnFKNk>?nawur_|| zEyf*3CMP7#ZMj?3Tfgt@j>)qZnFKNkWD>|EkVznuKqi4q0+|Fd34HfSAZTA@yq{(r zh?=$;UEefty*j$@MA!EXyuT{CpNp>lIq*KZ%`TY)G6`f7$Rvfod!aLyQYYpS`@FZ-&BXA$w1~} z&%iIkGQ1uB3n$P=dXAt0T^!-hUQ?FmsavO&V>i_$V`SB>Z5H&iscB`qoxpAumn^e3 z(`@EF%WoGSB*CaLa@iOe@vNZZIcm*p`WClMw_ZHs*-p@O@^!0t+-bvgf&8o_20iBvF=Fk)jhnvo1Px zk<{V6qGJvp7rV})B1x5X5s#Uk+1jUNG zZaHcB?yZOWYkyU@zh$mlw6`nm&kb$g#khI84GUIx$-3C#z*JuDE1PYrpgLZ&AVZ6q zCWps%+0(kzSEJ0Wd`v$j8hv(Y4!P%A78UBHmY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&kD#S}30*?ti+xfFki_ zD9wB_57{;j7PGHNLb@=rh}R~R)xVWW_U~vwF8!g@Qr9r*VboNj4yh{2WRTO9;<4^A z8DGVd94L_=3}>D*@Ck)-D+vblBbpq0qe;iLzfB#V94ApkhW4SdQLH73r6h%t)aN+d zme|V4x^iG95h*@YUMdr_leO`WE-x;yHkO1tIT%C%PY`}==5trtrI6C??~os1Kk^}7 zDba5&A^l4Uvsw9 zkERUEl{fpyG%-;{^IdX2=$irzL;ptX!a!s@KEH@Xkj0O{xX>=z(1xb1Uq3T3IeX<@ zdFa$KcBe8kH}^<%4o%I^EXX2BbW+><*v`saP4>OBin9B4%vKn1Pg~$^Z9tT{A)iWn z(;mNGo>U+yukBKi9Ch`=Jkm9lriU-Zyv0^BRD!(NgVEmSdS$~b apOB8*f_rdi_r;m)?yA31bsn_F#{U79!)WOM diff --git a/goclient/client.go b/goclient/client.go index ab72627..32498b8 100644 --- a/goclient/client.go +++ b/goclient/client.go @@ -1,55 +1,12 @@ 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() @@ -57,11 +14,11 @@ func main() { 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")) @@ -73,11 +30,12 @@ func main() { 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) - } - -// should check a user can login here +// 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) { @@ -94,98 +52,26 @@ func processLoginEvent(ge *pb.Event) *pb.Event { 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" +func checkLogin(ge *pb.Event) bool { + if (ge.Account == nil) { + log.Println("checkLogin() pb.Event.Account == nil ERROR") + return false + } - 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") + 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(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 + log.Println("checkLogin() LOGIN FAILED. ATTEMPT updateToken(ge)") + return updateToken(ge) } func updateToken(ge *pb.Event) bool { @@ -194,7 +80,8 @@ func updateToken(ge *pb.Event) bool { } url := ge.Account.URL + "/auth/login" + "?email=" + ge.Account.Email + "&password=" + ge.Account.Password - json := newFetchBody(ge, "POST", url) + // json := newFetchBody(ge, "POST", url) + json := POSTbody(ge, url) jwt := gjson.Get(json, "jwt") if (jwt.String() == "") { @@ -210,54 +97,6 @@ func updateToken(ge *pb.Event) bool { 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)