aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCollin Guarino <collin.guarino@gmail.com>2016-02-27 13:09:20 -0500
committerCollin Guarino <collin.guarino@gmail.com>2016-02-27 13:09:20 -0500
commit024cad670bbe1cdefaf184395a4acf0a21fe4ccd (patch)
tree06c50f93bcd0ff81c0a0c625991a00b232d8427f
parent5effa9fc725fa60c64e34742b2637cdd458544d2 (diff)
Major fix for Bridge.CreateUser and change to user management design.
-rw-r--r--bridge.go18
-rw-r--r--bridge_test.go3
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)
}