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 #6

Merged
merged 1 commit into from
Jul 9, 2024
Merged

Bridge Authentication #6

merged 1 commit into from
Jul 9, 2024

Conversation

thibauult
Copy link
Contributor

Bridge authentication has been make simple via the Authenticator interface:

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)
}

In this example, we wait until the link button is pressed on the bridge.
The Authenticator.Authenticate() function returns three values:

  • apiKey string that is not empty when retry = false and err == nil
  • retry bool which indicates that the link button has not been pressed
  • err error which contains the error details

You can consider the authentication has failed whenever the retry value is false and the err is not nil.

playground/authenticate/main.go Dismissed Show dismissed Hide dismissed
@thibauult thibauult merged commit d44637d into main Jul 9, 2024
3 checks passed
@thibauult thibauult deleted the feat/authenticate branch July 9, 2024 07:57
thibauult added a commit to openhue/openhue-cli that referenced this pull request Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant