-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Viper to store token between separate runs
- Loading branch information
Showing
18 changed files
with
385 additions
and
91 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
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,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 | ||
} |
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,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 | ||
} |
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
Oops, something went wrong.