dyndns/cmd/hash-password.go
Torben Nehmer 9a31bbc912 - Clarified web handler name
- Implemented Configuration Loader with unauthorized error handling
- More elaborate test user config
- Use Viper Unmarshalling for User Config loading
- Centralized password hasing code
2021-08-22 14:36:55 +02:00

49 lines
1.2 KiB
Go

package cmd
import (
"bytes"
"fmt"
"log"
"os"
"gitea.nehmer.net/torben/dyndns/service"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh/terminal"
)
var cmdHashPassword = &cobra.Command{
Use: "hash-password",
Short: "Hashes a password for usage in user config files",
Long: `Creates a salted hash using bcrypt for use in userconfig files, so that we don't
have to store a plaintext password.`,
Run: func(cmd *cobra.Command, args []string) {
log.Printf("Configuration in use: %v", viper.AllSettings())
service.LoadConfig()
fmt.Println("Enter password:")
password, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Fatalf("failed to read password: %v", err)
}
fmt.Println("Enter password again:")
passwordCheck, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Fatalf("failed to read password: %v", err)
}
if !bytes.Equal(password, passwordCheck) {
log.Fatalln("the passwords do not match.")
}
hash, err := service.HashPassword(password)
if err != nil {
log.Fatalf("failed to create password hash: %v", err)
}
fmt.Printf("Hashed password: %s\n", hash)
},
}
func init() {
rootCmd.AddCommand(cmdHashPassword)
}