From 024cad670bbe1cdefaf184395a4acf0a21fe4ccd Mon Sep 17 00:00:00 2001 From: Collin Guarino Date: Sat, 27 Feb 2016 13:09:20 -0500 Subject: Major fix for Bridge.CreateUser and change to user management design. --- bridge.go | 18 +++++++++++++----- bridge_test.go | 3 ++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/bridge.go b/bridge.go index 6645ee9..d9c0c5c 100644 --- a/bridge.go +++ b/bridge.go @@ -221,19 +221,27 @@ func (bridge *Bridge) Login(username string) error { return nil } -// Bridge.CreateUser posts to ./api on the bridge to create a new whitelisted user. -func (bridge *Bridge) CreateUser(deviceType string) error { +// Bridge.CreateUser adds a new user token on the whitelist. +// The token is the first return value in this function which must +// be used with `Bridge.Login`. You cannot use a plaintext username +// like the argument provided in this function. +// This was done by Philips Hue for security reasons. +func (bridge *Bridge) CreateUser(deviceType string) (string, error) { params := map[string]string{"devicetype": deviceType} body, _, err := bridge.Post("/api", params) if err != nil { - log.Fatal("Error: Unable to create user. ", err) - return err + log.Fatal("Error: Failed to create user. ", err) + return "", err } content := string(body) username := content[strings.LastIndex(content, ":\"")+2 : strings.LastIndex(content, "\"")] + userOut := fmt.Sprintf( + "Created user token '%s'. Use Bridge.Login with this token from now on.", + username) + log.Println(userOut) bridge.Username = username - return nil + return username, nil } // Bridge.DeleteUser deletes a user given its USER KEY, not the string name. diff --git a/bridge_test.go b/bridge_test.go index e46c32a..b98d63e 100644 --- a/bridge_test.go +++ b/bridge_test.go @@ -14,7 +14,8 @@ import ( func TestCreateUser(t *testing.T) { bridge, _ := NewBridge("192.168.1.128") - bridge.CreateUser("test") + username, _ := bridge.CreateUser("test") + bridge.Login(username) //bridge.DeleteUser(bridge.Username) } -- cgit v1.2.3