diff options
-rw-r--r-- | bridge.go | 18 | ||||
-rw-r--r-- | bridge_test.go | 3 |
2 files changed, 15 insertions, 6 deletions
@@ -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) } |