can now update user passwords

master
Jordan Orelli 5 years ago
parent c8a5e5089f
commit 7e6a56ee1e

@ -78,4 +78,21 @@ func (db *SQLite) CheckPassword(name, pass string) error {
return nil return nil
} }
func (db *SQLite) SetPassword(name, pass, salt string) error {
combined := []byte(pass + salt)
hashBytes, err := bcrypt.GenerateFromPassword(combined, 13)
if err != nil {
return fmt.Errorf("unable to generate password hash: %v", err)
}
hash := string(hashBytes)
if _, err := db.db.Exec(`
update users
set phash = ?, psalt = ?
where name = ?;
`, hash, salt, name); err != nil {
return fmt.Errorf("unable to update user: %v", err)
}
return nil
}
func (db *SQLite) Close() error { return db.db.Close() } func (db *SQLite) Close() error { return db.db.Close() }

@ -92,6 +92,21 @@ func runUserCheckPassword(cmd *cobra.Command, args []string) {
} }
} }
func runUserSetPassword(cmd *cobra.Command, args []string) {
conn, err := db.OpenSQLite(cmd.Flag("db").Value.String())
if err != nil {
fmt.Fprintf(os.Stderr, "unable to open sqlite database: %v\n", err)
}
defer conn.Close()
user := args[0]
pass := args[1]
salt := cryptostring(12)
if err := conn.SetPassword(user, pass, salt); err != nil {
fmt.Fprintf(os.Stderr, "failed to set password: %v\n", err)
}
}
func main() { func main() {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "kloam", Use: "kloam",
@ -122,11 +137,19 @@ func main() {
userCheckPassword := &cobra.Command{ userCheckPassword := &cobra.Command{
Use: "check-password", Use: "check-password",
Short: "checks a users password", Short: "checks a user's password",
Args: cobra.ExactArgs(2), Args: cobra.ExactArgs(2),
Run: runUserCheckPassword, Run: runUserCheckPassword,
} }
user.AddCommand(userCheckPassword) user.AddCommand(userCheckPassword)
userSetPassword := &cobra.Command{
Use: "set-password",
Short: "sets a user's password",
Args: cobra.ExactArgs(2),
Run: runUserSetPassword,
}
user.AddCommand(userSetPassword)
cmd.Execute() cmd.Execute()
} }

Loading…
Cancel
Save