Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bridge Authentication logic moved to the openhue-go project #61

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 6 additions & 28 deletions cmd/setup/setup.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package setup

import (
"context"
"errors"
"fmt"
oh "github.com/openhue/openhue-go"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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(".")
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
Loading