Added ability to update Passwords

This commit is contained in:
Jason Streifling 2024-02-22 19:27:41 +01:00
parent 75a21eeb9f
commit 96fe38726c
2 changed files with 38 additions and 1 deletions

View File

@ -41,7 +41,7 @@ func (db *DB) AddUser(user, pass, first, last string, writer, editor, admin bool
}
if !permissionsOK(writer, editor, admin) {
return fmt.Errorf("error with mutually exclusive user permissions: writer = %v, editor = %v, admin = %v",
return fmt.Errorf("error with mutually exclusive permissions: writer = %v, editor = %v, admin = %v",
writer, editor, admin)
}
@ -58,3 +58,39 @@ func (db *DB) AddUser(user, pass, first, last string, writer, editor, admin bool
return nil
}
func (db *DB) ChangePassword(id int64, oldPass, newPass string) error {
var oldHashedPass string
selectQuery := `
SELECT password FROM
users
WHERE
id = ?
`
row := db.QueryRow(selectQuery, id)
if err := row.Scan(&oldHashedPass); err != nil {
return fmt.Errorf("error reading password from DB: %v", err)
}
if err := bcrypt.CompareHashAndPassword([]byte(oldHashedPass), []byte(oldPass)); err != nil {
return fmt.Errorf("error checking password: %v", err)
}
newHashedPass, err := bcrypt.GenerateFromPassword([]byte(newPass), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf("error creating password hash: %v", err)
}
updateQuery := `
UPDATE users
SET password = ?
WHERE id = ?
`
_, err = db.Exec(updateQuery, newHashedPass, id)
if err != nil {
return fmt.Errorf("error updating password in DB: %v", err)
}
return nil
}

View File

@ -15,6 +15,7 @@ func main() {
if err != nil {
log.Fatalln(err)
}
defer db.Close()
f, err := feed.OpenFeed("tmp/rss.gob")
if err != nil {