refactor: small updates in the verify and create subcommands

This commit is contained in:
Stavros
2025-02-10 21:53:44 +02:00
parent cfe2a1967a
commit eb4e157def
2 changed files with 34 additions and 29 deletions

View File

@@ -13,10 +13,12 @@ import (
) )
var interactive bool var interactive bool
var username string
var password string
var docker bool var docker bool
// i stands for input
var iUsername string
var iPassword string
var CreateCmd = &cobra.Command{ var CreateCmd = &cobra.Command{
Use: "create", Use: "create",
Short: "Create a user", Short: "Create a user",
@@ -30,13 +32,13 @@ var CreateCmd = &cobra.Command{
// Create huh form // Create huh form
form := huh.NewForm( form := huh.NewForm(
huh.NewGroup( huh.NewGroup(
huh.NewInput().Title("Username").Value(&username).Validate((func(s string) error { huh.NewInput().Title("Username").Value(&iUsername).Validate((func(s string) error {
if s == "" { if s == "" {
return errors.New("username cannot be empty") return errors.New("username cannot be empty")
} }
return nil return nil
})), })),
huh.NewInput().Title("Password").Value(&password).Validate((func(s string) error { huh.NewInput().Title("Password").Value(&iPassword).Validate((func(s string) error {
if s == "" { if s == "" {
return errors.New("password cannot be empty") return errors.New("password cannot be empty")
} }
@@ -57,20 +59,21 @@ var CreateCmd = &cobra.Command{
} }
// Do we have username and password? // Do we have username and password?
if username == "" || password == "" { if iUsername == "" || iPassword == "" {
log.Error().Msg("Username and password cannot be empty") log.Error().Msg("Username and password cannot be empty")
} }
log.Info().Str("username", username).Str("password", password).Bool("docker", docker).Msg("Creating user") log.Info().Str("username", iUsername).Str("password", iPassword).Bool("docker", docker).Msg("Creating user")
// Hash password // Hash password
passwordByte, passwordErr := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) password, passwordErr := bcrypt.GenerateFromPassword([]byte(iPassword), bcrypt.DefaultCost)
if passwordErr != nil { if passwordErr != nil {
log.Fatal().Err(passwordErr).Msg("Failed to hash password") log.Fatal().Err(passwordErr).Msg("Failed to hash password")
} }
passwordString := string(passwordByte) // Convert password to string
passwordString := string(password)
// Escape $ for docker // Escape $ for docker
if docker { if docker {
@@ -78,14 +81,14 @@ var CreateCmd = &cobra.Command{
} }
// Log user created // Log user created
log.Info().Str("user", fmt.Sprintf("%s:%s", username, passwordString)).Msg("User created") log.Info().Str("user", fmt.Sprintf("%s:%s", iUsername, passwordString)).Msg("User created")
}, },
} }
func init() { func init() {
// Flags // Flags
CreateCmd.Flags().BoolVar(&interactive, "interactive", false, "Create a user interactively") CreateCmd.Flags().BoolVarP(&interactive, "interactive", "i", false, "Create a user interactively")
CreateCmd.Flags().BoolVar(&docker, "docker", false, "Format output for docker") CreateCmd.Flags().BoolVar(&docker, "docker", false, "Format output for docker")
CreateCmd.Flags().StringVar(&username, "username", "", "Username") CreateCmd.Flags().StringVar(&iUsername, "username", "", "Username")
CreateCmd.Flags().StringVar(&password, "password", "", "Password") CreateCmd.Flags().StringVar(&iPassword, "password", "", "Password")
} }

View File

@@ -12,10 +12,12 @@ import (
) )
var interactive bool var interactive bool
var username string
var password string
var docker bool var docker bool
var user string
// i stands for input
var iUsername string
var iPassword string
var iUser string
var VerifyCmd = &cobra.Command{ var VerifyCmd = &cobra.Command{
Use: "verify", Use: "verify",
@@ -30,19 +32,19 @@ var VerifyCmd = &cobra.Command{
// Create huh form // Create huh form
form := huh.NewForm( form := huh.NewForm(
huh.NewGroup( huh.NewGroup(
huh.NewInput().Title("User (username:hash)").Value(&user).Validate((func(s string) error { huh.NewInput().Title("User (username:hash)").Value(&iUser).Validate((func(s string) error {
if s == "" { if s == "" {
return errors.New("user cannot be empty") return errors.New("user cannot be empty")
} }
return nil return nil
})), })),
huh.NewInput().Title("Username").Value(&username).Validate((func(s string) error { huh.NewInput().Title("Username").Value(&iUsername).Validate((func(s string) error {
if s == "" { if s == "" {
return errors.New("username cannot be empty") return errors.New("username cannot be empty")
} }
return nil return nil
})), })),
huh.NewInput().Title("Password").Value(&password).Validate((func(s string) error { huh.NewInput().Title("Password").Value(&iPassword).Validate((func(s string) error {
if s == "" { if s == "" {
return errors.New("password cannot be empty") return errors.New("password cannot be empty")
} }
@@ -63,28 +65,28 @@ var VerifyCmd = &cobra.Command{
} }
// Do we have username, password and user? // Do we have username, password and user?
if username == "" || password == "" || user == "" { if iUsername == "" || iPassword == "" || iUser == "" {
log.Fatal().Msg("Username, password and user cannot be empty") log.Fatal().Msg("Username, password and user cannot be empty")
} }
log.Info().Str("user", user).Str("username", username).Str("password", password).Bool("docker", docker).Msg("Verifying user") log.Info().Str("user", iUser).Str("username", iUsername).Str("password", iPassword).Bool("docker", docker).Msg("Verifying user")
// Split username and password // Split username and password hash
userSplit := strings.Split(user, ":") username, hash, ok := strings.Cut(iUser, ":")
if userSplit[1] == "" { if !ok {
log.Fatal().Msg("User is not formatted correctly") log.Fatal().Msg("User is not formatted correctly")
} }
// Replace $$ with $ if formatted for docker // Replace $$ with $ if formatted for docker
if docker { if docker {
userSplit[1] = strings.ReplaceAll(userSplit[1], "$$", "$") hash = strings.ReplaceAll(hash, "$$", "$")
} }
// Compare username and password // Compare username and password
verifyErr := bcrypt.CompareHashAndPassword([]byte(userSplit[1]), []byte(password)) verifyErr := bcrypt.CompareHashAndPassword([]byte(hash), []byte(iPassword))
if verifyErr != nil || username != userSplit[0] { if verifyErr != nil || username != iUsername {
log.Fatal().Msg("Username or password incorrect") log.Fatal().Msg("Username or password incorrect")
} else { } else {
log.Info().Msg("Verification successful") log.Info().Msg("Verification successful")
@@ -96,7 +98,7 @@ func init() {
// Flags // Flags
VerifyCmd.Flags().BoolVarP(&interactive, "interactive", "i", false, "Create a user interactively") VerifyCmd.Flags().BoolVarP(&interactive, "interactive", "i", false, "Create a user interactively")
VerifyCmd.Flags().BoolVar(&docker, "docker", false, "Is the user formatted for docker?") VerifyCmd.Flags().BoolVar(&docker, "docker", false, "Is the user formatted for docker?")
VerifyCmd.Flags().StringVar(&username, "username", "", "Username") VerifyCmd.Flags().StringVar(&iUsername, "username", "", "Username")
VerifyCmd.Flags().StringVar(&password, "password", "", "Password") VerifyCmd.Flags().StringVar(&iPassword, "password", "", "Password")
VerifyCmd.Flags().StringVar(&user, "user", "", "Hash (username:hash combination)") VerifyCmd.Flags().StringVar(&iUser, "user", "", "Hash (username:hash combination)")
} }