diff options
author | Collin Guarino <collin.guarino@gmail.com> | 2016-02-27 12:55:14 -0500 |
---|---|---|
committer | Collin Guarino <collin.guarino@gmail.com> | 2016-02-27 12:55:14 -0500 |
commit | 5effa9fc725fa60c64e34742b2637cdd458544d2 (patch) | |
tree | 6cb7efd870d6e3e73c882b007a36bbb1d4af99c3 | |
parent | e02dd76720b845ba8ffbfd69cb44e71a0e31f97f (diff) |
Fixed Bridge.Login to verify authentication instead of crashing later.
-rw-r--r-- | bridge.go | 26 | ||||
-rw-r--r-- | bridge_test.go | 9 |
2 files changed, 23 insertions, 12 deletions
@@ -53,9 +53,10 @@ type BridgeInfo struct { // bridge.Get sends an http GET to the bridge func (bridge *Bridge) Get(path string) ([]byte, io.Reader, error) { - resp, err := http.Get("http://" + bridge.IPAddress + path) + uri := fmt.Sprintf("http://" + bridge.IPAddress + path) + resp, err := http.Get(uri) if err != nil { - err = errors.New("Unable to access bridge.") + err = errors.New("Error: Unable to access bridge. ") log.Println(err) return []byte{}, nil, err } @@ -134,7 +135,7 @@ func HandleResponse(resp *http.Response) ([]byte, io.Reader, error) { return []byte{}, nil, err } reader := bytes.NewReader(body) - if strings.Contains(string(body), "error") { + if strings.Contains(string(body), "\"error\"") { errString := string(body) errNum := errString[strings.Index(errString, "type\":")+6 : strings.Index(errString, ",\"address")] @@ -155,7 +156,7 @@ func FindBridge() (Bridge, error) { bridge := Bridge { IPAddress: "www.meethue.com" } body, _, err := bridge.Get("/api/nupnp") if err != nil { - err = errors.New("Unable to locate bridge.") + err = errors.New("Error: Unable to locate bridge.") log.Fatal(err) return Bridge{}, err } @@ -207,15 +208,16 @@ func (bridge *Bridge) GetInfo() error { return nil } -// Bridge.Login assigns a username to access the bridge with and -// will create the username key if it does not exist. +// Bridge.Login verifies that the username token has bridge access +// and only assigns the bridge its Username value if verification is successful. func (bridge *Bridge) Login(username string) error { + uri := fmt.Sprintf("/api/%s", username) + _, _, err := bridge.Get(uri) + if err != nil { + log.Fatal(err) + return err + } bridge.Username = username - // err := bridge.CreateUser(username) - // if err != nil { - // log.Fatal("Error: Unable to login as user ", username, " ", err) - // return err - // } return nil } @@ -292,7 +294,7 @@ func (bridge *Bridge) GetLightByIndex(index int) (Light, error) { light := Light{} err = json.Unmarshal(body, &light) if err != nil { - trace("", err) + return Light{}, errors.New("Error: Unable to unmarshal light data. ") } light.Index = index light.Bridge = bridge diff --git a/bridge_test.go b/bridge_test.go index 3fcaa81..e46c32a 100644 --- a/bridge_test.go +++ b/bridge_test.go @@ -22,3 +22,12 @@ func TestFindBridge(t *testing.T) { bridge, _ := FindBridge() fmt.Println(bridge.IPAddress) } + +func TestBridgeLogin(t *testing.T) { + bridge, err := FindBridge() + if err != nil { + fmt.Println("Error on TestBridgeLogin") + } + bridge.Login("427de8bd6d49f149c8398e4fc08f") + +} |