2015-05-29 18:20:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-10-06 20:54:45 +00:00
|
|
|
"encoding/json"
|
2015-05-29 18:20:04 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2019-01-31 22:42:42 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2017-10-06 20:54:45 +00:00
|
|
|
"github.com/Luzifer/password/hasher"
|
2017-10-31 11:12:51 +00:00
|
|
|
pwd "github.com/Luzifer/password/lib"
|
2015-05-29 18:20:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getCmdGet() *cobra.Command {
|
|
|
|
cmd := cobra.Command{
|
|
|
|
Use: "get",
|
|
|
|
Short: "generate and return a secure random password",
|
|
|
|
Run: actionCmdGet,
|
|
|
|
}
|
|
|
|
|
2017-10-06 20:54:45 +00:00
|
|
|
cmd.Flags().BoolVarP(&flags.CLI.JSON, "json", "j", false, "return output in JSON format")
|
2015-05-29 18:20:04 +00:00
|
|
|
cmd.Flags().IntVarP(&flags.CLI.Length, "length", "l", 20, "length of the generated password")
|
2017-10-31 11:57:19 +00:00
|
|
|
cmd.Flags().IntVarP(&flags.CLI.Num, "number", "n", 1, "number of passwords to generate")
|
2015-05-29 18:20:04 +00:00
|
|
|
cmd.Flags().BoolVarP(&flags.CLI.SpecialCharacters, "special", "s", false, "use special characters in your password")
|
|
|
|
|
2017-10-31 11:12:51 +00:00
|
|
|
cmd.Flags().BoolVarP(&flags.CLI.XKCD, "xkcd", "x", false, "use XKCD style password")
|
|
|
|
cmd.Flags().BoolVarP(&flags.CLI.PrependDate, "date", "d", true, "prepend current date to XKCD style passwords")
|
|
|
|
|
2019-01-31 22:42:42 +00:00
|
|
|
cmd.Flags().Bool("check-hibp", false, "Check HaveIBeenPwned for this password")
|
|
|
|
|
2015-05-29 18:20:04 +00:00
|
|
|
return &cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func actionCmdGet(cmd *cobra.Command, args []string) {
|
2017-10-31 11:12:51 +00:00
|
|
|
var (
|
|
|
|
password string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2017-10-31 11:57:19 +00:00
|
|
|
for i := 0; i < flags.CLI.Num; i++ {
|
2017-10-31 11:12:51 +00:00
|
|
|
|
2019-01-31 22:42:42 +00:00
|
|
|
regenerate:
|
2017-10-31 11:57:19 +00:00
|
|
|
if flags.CLI.XKCD {
|
|
|
|
password, err = pwd.DefaultXKCD.GeneratePassword(flags.CLI.Length, flags.CLI.PrependDate)
|
|
|
|
} else {
|
|
|
|
password, err = pwd.NewSecurePassword().GeneratePassword(flags.CLI.Length, flags.CLI.SpecialCharacters)
|
2015-05-29 18:20:04 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 22:42:42 +00:00
|
|
|
if c, _ := cmd.Flags().GetBool("check-hibp"); c {
|
|
|
|
switch pwd.CheckHIBPPasswordHash(password) {
|
|
|
|
case pwd.ErrPasswordInBreach:
|
|
|
|
goto regenerate
|
|
|
|
case nil:
|
|
|
|
// Just do nothing
|
|
|
|
default:
|
|
|
|
fmt.Printf("Unable to check for password pwnage: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-31 11:57:19 +00:00
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case err == pwd.ErrLengthTooLow:
|
|
|
|
fmt.Println("The password has to be more than 4 characters long to meet the security considerations")
|
|
|
|
default:
|
|
|
|
fmt.Println("An unknown error occured")
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !flags.CLI.JSON {
|
|
|
|
fmt.Println(password)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
hashes, err := hasher.GetHashMap(password)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Unable to generate hashes: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
hashes["password"] = password
|
|
|
|
json.NewEncoder(os.Stdout).Encode(hashes)
|
2017-10-06 20:54:45 +00:00
|
|
|
|
|
|
|
}
|
2015-05-29 18:20:04 +00:00
|
|
|
}
|