From ad166618a63d88f5eba02519b1e353ca583d18d9 Mon Sep 17 00:00:00 2001 From: thibauult Date: Tue, 9 Jul 2024 10:01:18 +0200 Subject: [PATCH] Bridge Authentication logic moved to the openhue-go project --- cmd/setup/setup.go | 34 ++++++---------------------------- go.mod | 6 +++++- go.sum | 2 ++ 3 files changed, 13 insertions(+), 29 deletions(-) diff --git a/cmd/setup/setup.go b/cmd/setup/setup.go index 65cd321..b3ca42f 100644 --- a/cmd/setup/setup.go +++ b/cmd/setup/setup.go @@ -1,8 +1,6 @@ package setup import ( - "context" - "errors" "fmt" oh "github.com/openhue/openhue-go" log "github.com/sirupsen/logrus" @@ -51,11 +49,15 @@ func startSetup(io openhue.IOStreams, o *CmdSetupOptions) { return } - client := openhue.NewOpenHueClientNoAuth(ip) + authenticator, err := oh.NewAuthenticator(ip, oh.WithDeviceType(o.deviceType), oh.WithGenerateClientKey(o.generateClientKey)) + if err != nil { + io.ErrPrintln(err.Error()) + return + } io.Println("[..] Please push the button on your Hue Bridge") for { - key, retry, err := tryAuth(client, o.toAuthenticateBody()) + key, retry, err := authenticator.Authenticate() if err != nil && retry { // this is an expected error, we just wait for the user to push the button io.Printf(".") @@ -95,30 +97,6 @@ func getBridgeIPAddress(io openhue.IOStreams, o *CmdSetupOptions) (string, error return b.IpAddress, nil } -func (o *CmdSetupOptions) toAuthenticateBody() oh.AuthenticateJSONRequestBody { - body := oh.AuthenticateJSONRequestBody{} - body.Devicetype = &o.deviceType - body.Generateclientkey = &o.generateClientKey - return body -} - -func tryAuth(client *oh.ClientWithResponses, body oh.AuthenticateJSONRequestBody) (string, bool, error) { - - resp, err := client.AuthenticateWithResponse(context.Background(), body) - cobra.CheckErr(err) - - if resp.JSON200 == nil { - return "", false, fmt.Errorf("[KO] Unable to reach the Bridge, verify that the IP is correct. You can verify it via %s", hueBridgeDiscover) - } - - auth := (*resp.JSON200)[0] - if auth.Error != nil { - return "", true, errors.New(*auth.Error.Description) - } - - return *auth.Success.Username, false, nil -} - func saveConfig(bridge string, key string) (string, error) { c := openhue.Config{ Bridge: bridge, diff --git a/go.mod b/go.mod index e12b49b..9759862 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,12 @@ module openhue-cli go 1.22.3 +//replace ( +// "github.com/openhue/openhue-go" => "/home/thibault.pensec/local/perso/openhue-go" +//) + require ( - github.com/openhue/openhue-go v0.2.0 + github.com/openhue/openhue-go v0.3.0 github.com/r3labs/sse/v2 v2.10.0 github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.8.1 diff --git a/go.sum b/go.sum index a4156c4..507d3ff 100644 --- a/go.sum +++ b/go.sum @@ -39,6 +39,8 @@ github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmt github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg= github.com/openhue/openhue-go v0.2.0 h1:6IXyX8/RklJk31HJQGqxctjDXf2SfA1FPmdnTJeMiMU= github.com/openhue/openhue-go v0.2.0/go.mod h1:45yBMI3Un7YAbn2BO9L/RnIQ50gbZjuOB31a2f1kK8g= +github.com/openhue/openhue-go v0.3.0 h1:42/fWpq69vYjGOdfbwjIUoTtNom6M/EieOAI0xPmvCQ= +github.com/openhue/openhue-go v0.3.0/go.mod h1:CJ/AzVsyfJFJ5zhiyAQxeteCfkuzzmTYrNirYewpZN4= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=