-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
251 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package openhue | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// Authenticator defines a service that allows retrieving the Hue API Key | ||
type Authenticator interface { | ||
// Authenticate performs a single authentication request to retrieve an API key. | ||
// It will return ("", true, err != nil) if the link button has not been pressed. | ||
Authenticate() (key string, press bool, err error) | ||
} | ||
|
||
type authenticatorImpl struct { | ||
client *ClientWithResponses | ||
deviceType string | ||
generateClientKey bool | ||
} | ||
|
||
type authOpt func(b *authenticatorImpl) | ||
|
||
func NewAuthenticator(bridgeIP string, opts ...authOpt) (Authenticator, error) { | ||
client, err := newClient(bridgeIP, "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
authenticator := &authenticatorImpl{client: client, generateClientKey: true} | ||
|
||
for _, o := range opts { | ||
o(authenticator) | ||
} | ||
|
||
if len(authenticator.deviceType) == 0 { | ||
hostName, err := os.Hostname() | ||
if err != nil { | ||
return nil, err | ||
} | ||
authenticator.deviceType = hostName | ||
} | ||
|
||
return authenticator, nil | ||
} | ||
|
||
func (a *authenticatorImpl) Authenticate() (string, bool, error) { | ||
|
||
body := AuthenticateJSONRequestBody{ | ||
Devicetype: &a.deviceType, | ||
Generateclientkey: &a.generateClientKey, | ||
} | ||
|
||
resp, err := a.client.AuthenticateWithResponse(context.Background(), body) | ||
if err != nil { | ||
return "", false, err | ||
} | ||
|
||
if resp.JSON200 == nil { | ||
return "", false, fmt.Errorf("unable to reach the Bridge, verify that the IP is correct") | ||
} | ||
|
||
auth := (*resp.JSON200)[0] | ||
if auth.Error != nil { | ||
return "", true, errors.New(*auth.Error.Description) | ||
} | ||
|
||
return *auth.Success.Username, false, nil | ||
} | ||
|
||
func WithDeviceType(deviceType string) authOpt { | ||
return func(b *authenticatorImpl) { | ||
b.deviceType = deviceType | ||
} | ||
} | ||
|
||
func WithGenerateClientKey(generateClientKey bool) authOpt { | ||
return func(b *authenticatorImpl) { | ||
b.generateClientKey = generateClientKey | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
module github.com/openhue/openhue-go | ||
|
||
go 1.22 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/grandcat/zeroconf v1.0.0 | ||
github.com/oapi-codegen/runtime v1.1.1 | ||
github.com/stretchr/testify v1.9.0 | ||
gopkg.in/yaml.v3 v3.0.1 | ||
) | ||
|
||
require ( | ||
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect | ||
github.com/cenkalti/backoff v2.2.1+incompatible // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/google/uuid v1.6.0 // indirect | ||
github.com/grandcat/zeroconf v1.0.0 // indirect | ||
github.com/miekg/dns v1.1.61 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/stretchr/objx v0.5.2 // indirect | ||
golang.org/x/crypto v0.25.0 // indirect | ||
golang.org/x/mod v0.19.0 // indirect | ||
golang.org/x/net v0.27.0 // indirect | ||
golang.org/x/sync v0.7.0 // indirect | ||
golang.org/x/sys v0.22.0 // indirect | ||
golang.org/x/tools v0.23.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/openhue/openhue-go" | ||
"time" | ||
) | ||
|
||
func main() { | ||
|
||
bridge, err := openhue.NewBridgeDiscovery(openhue.WithTimeout(1 * time.Second)).Discover() | ||
openhue.CheckErr(err) | ||
|
||
authenticator, err := openhue.NewAuthenticator(bridge.IpAddress) | ||
openhue.CheckErr(err) | ||
|
||
fmt.Println("Press the link button") | ||
|
||
var key string | ||
for len(key) == 0 { | ||
|
||
// try to authenticate | ||
apiKey, retry, err := authenticator.Authenticate() | ||
|
||
if err != nil && retry { | ||
// link button not pressed | ||
fmt.Printf(".") | ||
time.Sleep(500 * time.Millisecond) | ||
} else if err != nil && !retry { | ||
// there is a real error | ||
openhue.CheckErr(err) | ||
} else { | ||
key = apiKey | ||
} | ||
} | ||
|
||
fmt.Println("\n", key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
module github.com/openhue/openhue-go/playground | ||
|
||
go 1.22 | ||
go 1.21 | ||
|
||
replace github.com/openhue/openhue-go => .. | ||
|
||
require ( | ||
github.com/openhue/openhue-go v0.1.0 | ||
) | ||
require github.com/openhue/openhue-go v0.1.0 | ||
|
||
require ( | ||
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect | ||
github.com/cenkalti/backoff v2.2.1+incompatible // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/google/uuid v1.6.0 // indirect | ||
github.com/grandcat/zeroconf v1.0.0 // indirect | ||
github.com/miekg/dns v1.1.61 // indirect | ||
github.com/oapi-codegen/runtime v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/stretchr/objx v0.5.2 // indirect | ||
github.com/stretchr/testify v1.9.0 // indirect | ||
golang.org/x/mod v0.19.0 // indirect | ||
golang.org/x/net v0.27.0 // indirect | ||
golang.org/x/sync v0.7.0 // indirect | ||
golang.org/x/sys v0.22.0 // indirect | ||
golang.org/x/tools v0.23.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
Oops, something went wrong.