Skip to content

Commit

Permalink
Added to auth command, used to retrieve the hue-application-key
Browse files Browse the repository at this point in the history
  • Loading branch information
thibauult committed Oct 31, 2023
1 parent 89f1aa4 commit 3c311ab
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ help:
.PHONY: generate
generate: ## Generates the openhue.gen.go client from the latest https://github.com/openhue/openhue-api specification
@$(GO) generate
@echo "${GREEN}${BOLD}./openhue/openhue.gen.go successfully generated 🚀${RESET}"
@echo "\n${GREEN}${BOLD}./openhue/openhue.gen.go successfully generated 🚀${RESET}"

.PHONY: build
build: ## Generates the openhue-cli executables in the ./dist folder
Expand All @@ -26,10 +26,10 @@ build: ## Generates the openhue-cli executables in the ./dist folder
.PHONY: tidy
tidy: ## Tidy makes sure go.mod matches the source code in the module
@$(GO) mod tidy
@echo "${GREEN}${BOLD}go.mod successfully cleaned 🧽${RESET}"
@echo "\n${GREEN}${BOLD}go.mod successfully cleaned 🧽${RESET}"

.PHONY: clean
clean: ##
@rm -rf ./dist
@$(GO) clean
@echo "${GREEN}${BOLD}Project successfully cleaned 🧹${RESET} (removed ./bin folder + go clean)"
@echo "\n${GREEN}${BOLD}Project successfully cleaned 🧹${RESET} (removed ./dist folder + go clean)"
26 changes: 19 additions & 7 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"fmt"
"github.com/spf13/cobra"
"openhue-cli/openhue"
"os"
)

var (
bridge string
deviceType string
generateClientKey bool
)
Expand All @@ -17,25 +19,35 @@ var authCmd = &cobra.Command{
Use: "auth",
GroupID: "init",
Short: "Authenticate",
Long: `Authenticate to retrieve the HUE application key. Requires to go and press the button on the bridge`,
Long: `Authenticate to retrieve the Hue Application Key. Requires to go and press the button on the bridge`,
Run: func(cmd *cobra.Command, args []string) {

client := openhue.NewOpenHueClientNoAuth(bridge)

body := new(openhue.AuthenticateJSONRequestBody)
body.Devicetype = &deviceType
body.Generateclientkey = &generateClientKey
resp, err := openhue.Api.AuthenticateWithResponse(context.Background(), *body)
resp, err := client.AuthenticateWithResponse(context.Background(), *body)
cobra.CheckErr(err)

auth := (*resp.JSON200).Success
fmt.Println(auth.Clientkey)
auth := (*resp.JSON200)[0]
if auth.Error != nil {
fmt.Println("\n🖲️", *auth.Error.Description)
} else {
fmt.Println("\nYour hue-application-key ->", *auth.Success.Username)
}
},
}

func init() {
rootCmd.AddCommand(authCmd)

authCmd.Flags().StringVarP(&deviceType, "devicetype", "d", "", "Device identifier (example 'app_name#instance_name')")
authCmd.MarkFlagRequired("devicetype")
authCmd.Flags().StringVarP(&bridge, "bridge", "b", "", "Bridge IP (example '192.168.1.23')")
authCmd.MarkFlagRequired("bridge")

hostname, err := os.Hostname()
cobra.CheckErr(err)
authCmd.Flags().StringVarP(&deviceType, "devicetype", "d", hostname, "Device identifier")

authCmd.Flags().BoolVarP(&generateClientKey, "generateclientkey", "k", true, "")
authCmd.Flags().BoolVarP(&generateClientKey, "generateclientkey", "k", true, "Generate the client key")
}
15 changes: 14 additions & 1 deletion openhue/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Load() *Config {
viper.SetConfigType("yaml")

// List of commands that does not require configuration
ignoredCmds := []string{"setup", "help", "discover"}
ignoredCmds := []string{"setup", "help", "discover", "auth"}

// When trying to run CLI without configuration
if err := viper.ReadInConfig(); err != nil && !slices.Contains(ignoredCmds, os.Args[1]) {
Expand Down Expand Up @@ -66,3 +66,16 @@ func NewOpenHueClient(c *Config) *ClientWithResponses {

return client
}

// NewOpenHueClientNoAuth Creates a new NewClientWithResponses for a given server and no application key.
// This function will also skip SSL verification, as the Philips HUE Bridge exposes a self-signed certificate.
func NewOpenHueClientNoAuth(ip string) *ClientWithResponses {

// skip SSL Verification
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}

client, err := NewClientWithResponses("https://" + ip)
cobra.CheckErr(err)

return client
}
9 changes: 7 additions & 2 deletions openhue/openhue.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3c311ab

Please sign in to comment.