fixed client code example.
WTF was wrong with the other code I made? Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
34351697e3
commit
1cb13782e2
|
@ -1,3 +1,4 @@
|
||||||
|
*.swp
|
||||||
server/server
|
server/server
|
||||||
client/client
|
client/client
|
||||||
example-protobuf/example-protobuf
|
example-protobuf/example-protobuf
|
||||||
|
|
Binary file not shown.
|
@ -1,55 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
// import "io"
|
|
||||||
import "log"
|
import "log"
|
||||||
import "net/http"
|
import "net/http"
|
||||||
import "io/ioutil"
|
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 "github.com/tidwall/gjson"
|
||||||
import pb "git.wit.com/wit/witProtobuf"
|
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() {
|
func main() {
|
||||||
ge := pb.MakeOkResponse()
|
ge := pb.MakeOkResponse()
|
||||||
c := pb.MakeDefaultConfig()
|
c := pb.MakeDefaultConfig()
|
||||||
|
@ -57,11 +14,11 @@ func main() {
|
||||||
ge.Account.Email = "jcarr@wit.com"
|
ge.Account.Email = "jcarr@wit.com"
|
||||||
ge.Account.Password = "yowzayowza"
|
ge.Account.Password = "yowzayowza"
|
||||||
ge.Account.URL = "http://stackapi:4000/"
|
ge.Account.URL = "http://stackapi:4000/"
|
||||||
|
// ge.Account.Token = "eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJ4IjozLCJyIjoiIiwiY3NyZiI6Ijloa0VYa2M0YURxTlVqSElGU2VJYUJoOCIsImV4cCI6MTU1OTY3OTAxMywiaXNzIjoid2l0Iiwic3ViIjoiamNhcnJAd2l0LmNvbSJ9.eCfhg3_VYBZh1ve69p0Op4U9L2T9CAZY5qPtx7vNswe3TB-y1nz2X2vw4AzPwfy4fACKeQGkJmWGedt1NKQP-WaZNVtGpgX9g0yvloCob2OMQwsLhq3X7e83weiK_Jlk"
|
||||||
ge.Account.Token = "badtoken"
|
ge.Account.Token = "badtoken"
|
||||||
|
|
||||||
junkEvent := processLoginEvent(ge)
|
junkEvent := processLoginEvent(ge)
|
||||||
log.Println("processAddEvent() junkEvent =", junkEvent)
|
log.Println("processAddEvent() junkEvent =", junkEvent)
|
||||||
|
|
||||||
log.Println("processAddEvent() START ge.Account =", ge.Account)
|
log.Println("processAddEvent() START ge.Account =", ge.Account)
|
||||||
|
|
||||||
tmp := string(GETbody(ge, "http://stackapi:4000/me"))
|
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"
|
url := ge.Account.URL + "vms/jcarr.com?count=1&cpu=2&ram=512&disk=25"
|
||||||
tmp = string(POSTbody(ge, url))
|
tmp = string(POSTbody(ge, url))
|
||||||
log.Println("processAddEvent() recieved json=", tmp)
|
log.Println("processAddEvent() recieved json=", tmp)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if a user can login here
|
||||||
// should check 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 {
|
func processLoginEvent(ge *pb.Event) *pb.Event {
|
||||||
log.Println("processLoginEvent() LOGIN START")
|
log.Println("processLoginEvent() LOGIN START")
|
||||||
if (checkLogin(ge) == false) {
|
if (checkLogin(ge) == false) {
|
||||||
|
@ -94,98 +52,26 @@ func processLoginEvent(ge *pb.Event) *pb.Event {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is the 'orig' code
|
func checkLogin(ge *pb.Event) bool {
|
||||||
// TODO: phase this out for more common code
|
if (ge.Account == nil) {
|
||||||
func origLoginCheck(ge *pb.Event) bool {
|
log.Println("checkLogin() pb.Event.Account == nil ERROR")
|
||||||
var url string
|
return false
|
||||||
var httpType string
|
}
|
||||||
httpType = "GET"
|
|
||||||
url = ge.Account.URL + "me"
|
|
||||||
|
|
||||||
log.Println("origLoginCheck() ", httpType, url)
|
url := ge.Account.URL + "me"
|
||||||
req, err := http.NewRequest(httpType, url, nil)
|
json := string(GETbody(ge, url))
|
||||||
if err != nil {
|
if (json != "") {
|
||||||
ge.Comment = "origLoginCheck() FAILED can't make new req"
|
email := gjson.Get(json, "email")
|
||||||
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() ", "email = ", email.String())
|
||||||
log.Println("origLoginCheck() ", "ge.Account.Username = ", ge.Account.Username)
|
log.Println("origLoginCheck() ", "ge.Account.Username = ", ge.Account.Username)
|
||||||
if (email.String() == ge.Account.Username) {
|
if (email.String() == ge.Account.Username) {
|
||||||
ge.Comment = "origLoginCheck() THIS TOTALLY WORKED MAN"
|
ge.Comment = "origLoginCheck() THIS TOTALLY WORKED MAN"
|
||||||
|
}
|
||||||
log.Println(ge.Comment)
|
log.Println(ge.Comment)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
log.Println("checkLogin() LOGIN FAILED. ATTEMPT updateToken(ge)")
|
||||||
ge.Comment = "origLoginCheck() FAILED "
|
return updateToken(ge)
|
||||||
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 {
|
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
|
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")
|
jwt := gjson.Get(json, "jwt")
|
||||||
if (jwt.String() == "") {
|
if (jwt.String() == "") {
|
||||||
|
@ -210,54 +97,6 @@ func updateToken(ge *pb.Event) bool {
|
||||||
return true
|
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 {
|
func GETbody(ge *pb.Event, URL string) string {
|
||||||
// req, err := http.NewRequest("GET", ge.Account.URL + "clusters", nil)
|
// req, err := http.NewRequest("GET", ge.Account.URL + "clusters", nil)
|
||||||
req, err := http.NewRequest("GET", URL, nil)
|
req, err := http.NewRequest("GET", URL, nil)
|
||||||
|
|
Loading…
Reference in New Issue