-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Home Assistant password reset commands (#199)
* Add Home Assistant password reset commands * Fix debug message
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cmd | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var authCmd = &cobra.Command{ | ||
Use: "authentication", | ||
Aliases: []string{"auth", "au"}, | ||
Short: "Authentication for Home Assistant users.", | ||
Long: ` | ||
The authentication command allows you to manage Home Assistant user accounts. | ||
`, | ||
Example: ` | ||
ha authentication reset --username "JohnDoe" --password "123SuperSecret!" | ||
`, | ||
} | ||
|
||
func init() { | ||
log.Debug("Init authentication") | ||
|
||
rootCmd.AddCommand(authCmd) | ||
} |
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,59 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
helper "github.com/home-assistant/cli/client" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var authResetCmd = &cobra.Command{ | ||
Use: "reset", | ||
Aliases: []string{"rst", "change"}, | ||
Short: "Reset the password of a Home Assistant user.", | ||
Long: ` | ||
This command allows you to change a password of a Home Assistant user. | ||
Please note, this command is limited due to security reasons, and will | ||
only work on some locations. For example, the Operating System CLI. | ||
`, | ||
Example: `ha authentication reset --username "JohnDoe" --password "123SuperSecret!"`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
log.WithField("args", args).Debug("auth reset") | ||
|
||
section := "auth" | ||
command := "reset" | ||
base := viper.GetString("endpoint") | ||
|
||
options := make(map[string]interface{}) | ||
|
||
for _, value := range []string{ | ||
"username", | ||
"password", | ||
} { | ||
val, err := cmd.Flags().GetString(value) | ||
if val != "" && err == nil && cmd.Flags().Changed(value) { | ||
options[value] = val | ||
} | ||
} | ||
|
||
resp, err := helper.GenericJSONPost(base, section, command, options) | ||
if err != nil { | ||
fmt.Println(err) | ||
ExitWithError = true | ||
} else { | ||
ExitWithError = !helper.ShowJSONResponse(resp) | ||
} | ||
|
||
return | ||
}, | ||
} | ||
|
||
func init() { | ||
authResetCmd.Flags().String("username", "", "Username to reset the password for") | ||
authResetCmd.Flags().String("password", "", "The new password to assign") | ||
cobra.MarkFlagRequired(authResetCmd.Flags(), "username") | ||
cobra.MarkFlagRequired(authResetCmd.Flags(), "password") | ||
authCmd.AddCommand(authResetCmd) | ||
} |