Skip to content

Commit

Permalink
Introduce Viper to store token between separate runs
Browse files Browse the repository at this point in the history
  • Loading branch information
halacs committed Dec 20, 2023
1 parent d339fcf commit 091e85f
Show file tree
Hide file tree
Showing 18 changed files with 385 additions and 91 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,21 @@ Available Commands:
get-name Queries the name of the Hörmann BiSecur gateway
groups Manages users defined in your Hörmann BiSecur gateway.
help Help about any command
login
logout
ping Check if your Hörmann BiSecur gateway is reachable or not.
set-state Open or close a door connected to your Hörmann BiSecur gateway.
status Queries the status (open/closed/etc) of your door.
users Manages users defined in your Hörmann BiSecur gateway.
Flags:
--debug debug log level
--debug debug log level (default true)
-h, --help help for halsecur
--host string IP or host name or the Hörmann BiSecure gateway
--mac string MAC address of the Hörmann BiSecur gateway
--password string Valid password belongs to the given username
--port int (default 4000)
--token uint32 Valid authentication token
--username string Valid username
Use "halsecur [command] --help" for more information about a command.
Expand Down
7 changes: 4 additions & 3 deletions cli/cmd/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ func init() {
)

discoverCmd := &cobra.Command{
Use: "discover",
Short: "Discover Hörmann BiSecur gateways on the local network",
Long: ``,
Use: "discover",
Short: "Discover Hörmann BiSecur gateways on the local network",
Long: ``,
PreRun: toggleDebug,
Run: func(cmd *cobra.Command, args []string) {
err := discover(discoveryTime)
if err != nil {
Expand Down
19 changes: 13 additions & 6 deletions cli/cmd/getName.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,31 @@ package cmd
import (
"bisecure/cli"
"bisecure/sdk"
"github.com/spf13/viper"
"os"

"github.com/spf13/cobra"
)

func init() {
getNameCmd := &cobra.Command{
Use: "get-name",
Short: "Queries the name of the Hörmann BiSecur gateway",
Long: ``,
Use: "get-name",
Short: "Queries the name of the Hörmann BiSecur gateway",
Long: ``,
PreRun: toggleDebug,
Run: func(cmd *cobra.Command, args []string) {
deviceMac := viper.GetString(ArgNameDeviceMac)
host := viper.GetString(ArgNameHost)
port := viper.GetInt(ArgNamePort)
token := viper.GetUint32(ArgNameToken)

mac, err := cli.ParesMacString(deviceMac)
if err != nil {
log.Fatalf("%v", err)
os.Exit(1)
}

err = GetName(localMac, mac, host, port)
err = GetName(localMac, mac, host, port, token)
if err != nil {
log.Fatalf("%v", err)
os.Exit(4)
Expand All @@ -31,8 +38,8 @@ func init() {
rootCmd.AddCommand(getNameCmd)
}

func GetName(localMac, mac [6]byte, host string, port int) error {
client := sdk.NewClient(log, localMac, mac, host, port)
func GetName(localMac, mac [6]byte, host string, port int, token uint32) error {
client := sdk.NewClient(log, localMac, mac, host, port, token)
err := client.Open()
if err != nil {
return err
Expand Down
19 changes: 13 additions & 6 deletions cli/cmd/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,33 @@ package cmd
import (
"bisecure/cli"
"bisecure/sdk"
"github.com/spf13/viper"
"os"

"github.com/spf13/cobra"
)

func init() {
groupsCmd := &cobra.Command{
Use: "groups",
Short: "Manages users defined in your Hörmann BiSecur gateway.",
Long: ``,
Use: "groups",
Short: "Manages users defined in your Hörmann BiSecur gateway.",
Long: ``,
PreRun: toggleDebug,
Run: func(cmd *cobra.Command, args []string) {
// TODO implement query user list and rights, add and delete user, password change of an already existing user

deviceMac := viper.GetString(ArgNameDeviceMac)
host := viper.GetString(ArgNameHost)
port := viper.GetInt(ArgNamePort)
token := viper.GetUint32(ArgNameToken)

mac, err := cli.ParesMacString(deviceMac)
if err != nil {
log.Fatalf("%v", err)
os.Exit(1)
}

err = listGroups(localMac, mac, host, port)
err = listGroups(localMac, mac, host, port, token)
if err != nil {
log.Fatalf("%v", err)
os.Exit(2)
Expand All @@ -33,8 +40,8 @@ func init() {
rootCmd.AddCommand(groupsCmd)
}

func listGroups(localMac [6]byte, mac [6]byte, host string, port int) error {
client := sdk.NewClient(log, localMac, mac, host, port)
func listGroups(localMac [6]byte, mac [6]byte, host string, port int, token uint32) error {
client := sdk.NewClient(log, localMac, mac, host, port, token)
err := client.Open()
if err != nil {
return err
Expand Down
71 changes: 71 additions & 0 deletions cli/cmd/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cmd

import (
"bisecure/cli"
"bisecure/sdk"
"github.com/spf13/viper"
"os"

"github.com/spf13/cobra"
)

func init() {
loginCmd := &cobra.Command{
Use: "login",
Short: "",
Long: ``,
PreRun: toggleDebug,
Run: func(cmd *cobra.Command, args []string) {
deviceMac := viper.GetString(ArgNameDeviceMac)
host := viper.GetString(ArgNameHost)
port := viper.GetInt(ArgNamePort)
username := viper.GetString(ArgNameUsername)
password := viper.GetString(ArgNamePassword)

mac, err := cli.ParesMacString(deviceMac)
if err != nil {
log.Fatalf("%v", err)
os.Exit(1)
}

token, err := login(localMac, mac, host, port, username, password)
if err != nil {
log.Fatalf("%v", err)
os.Exit(2)
}

log.Infof("Token: 0x%X", token)

// Store token in persistent config
viper.Set(ArgNameToken, token)
err = viper.WriteConfig()
if err != nil {
log.Errorf("Failed to save new configuration. %v", err)
}
},
}

rootCmd.AddCommand(loginCmd)
}

func login(localMac [6]byte, mac [6]byte, host string, port int, username string, password string) (uint32, error) {
client := sdk.NewClient(log, localMac, mac, host, port, 0)
err := client.Open()
if err != nil {
return 0, err
}

defer func() {
err2 := client.Close()
if err2 != nil {
log.Errorf("%v", err)
}
}()

err = client.Login(username, password)
if err != nil {
return 0, err
}

return client.GetToken(), nil
}
75 changes: 75 additions & 0 deletions cli/cmd/logout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cmd

import (
"bisecure/cli"
"bisecure/sdk"
"fmt"
"github.com/spf13/viper"
"os"

"github.com/spf13/cobra"
)

func init() {
logoutCmd := &cobra.Command{
Use: "logout",
Short: "",
Long: ``,
PreRun: toggleDebug,
Run: func(cmd *cobra.Command, args []string) {
deviceMac := viper.GetString(ArgNameDeviceMac)
host := viper.GetString(ArgNameHost)
port := viper.GetInt(ArgNamePort)
token := viper.GetUint32(ArgNameToken)

mac, err := cli.ParesMacString(deviceMac)
if err != nil {
log.Fatalf("%v", err)
os.Exit(1)
}

err = logout(localMac, mac, host, port, token)
if err != nil {
log.Fatalf("%v", err)
os.Exit(2)
}

// Clear token in persistent config file
viper.Set(ArgNameToken, 0)
err = viper.WriteConfig()
if err != nil {
log.Errorf("Failed to save new configuration. %v", err)
}
},
}

rootCmd.AddCommand(logoutCmd)
}

func logout(localMac [6]byte, mac [6]byte, host string, port int, token uint32) error {
if token == 0 {
return fmt.Errorf("invalid token value: 0x%X", token)
}

client := sdk.NewClient(log, localMac, mac, host, port, token)
err := client.Open()
if err != nil {
return err
}

defer func() {
err2 := client.Close()
if err2 != nil {
log.Errorf("%v", err)
}
}()

client.SetToken(token)

err = client.Logout()
if err != nil {
return err
}

return nil
}
21 changes: 14 additions & 7 deletions cli/cmd/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"bisecure/cli"
"bisecure/sdk"
"github.com/spf13/viper"
"os"

"github.com/spf13/cobra"
Expand All @@ -14,17 +15,23 @@ func init() {
)

pingCmd := &cobra.Command{
Use: "ping",
Short: "Check if your Hörmann BiSecur gateway is reachable or not.",
Long: ``,
Use: "ping",
Short: "Check if your Hörmann BiSecur gateway is reachable or not.",
Long: ``,
PreRun: toggleDebug,
Run: func(cmd *cobra.Command, args []string) {
deviceMac := viper.GetString(ArgNameDeviceMac)
host := viper.GetString(ArgNameHost)
port := viper.GetInt(ArgNamePort)
token := viper.GetUint32(ArgNameToken)

mac, err := cli.ParesMacString(deviceMac)
if err != nil {
log.Fatalf("%v", err)
os.Exit(1)
}

err = ping(localMac, mac, host, port, count)
err = ping(localMac, mac, host, port, count, token)
if err != nil {
log.Fatalf("%v", err)
os.Exit(2)
Expand All @@ -33,11 +40,11 @@ func init() {
}
rootCmd.AddCommand(pingCmd)

pingCmd.Flags().IntVar(&count, "count", 5, "Amount of the ping packages will be sent to the device")
pingCmd.Flags().IntVar(&count, "count", 3, "Amount of the ping packages will be sent to the device")
}

func ping(localMac [6]byte, mac [6]byte, host string, port int, count int) error {
client := sdk.NewClient(log, localMac, mac, host, port)
func ping(localMac [6]byte, mac [6]byte, host string, port int, count int, token uint32) error {
client := sdk.NewClient(log, localMac, mac, host, port, token)
err := client.Open()
if err != nil {
return err
Expand Down
Loading

0 comments on commit 091e85f

Please sign in to comment.