From 825bc4d8b2bac72473d37218c7cf6761eebfbe52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20=C3=85hall?= Date: Tue, 20 Feb 2024 08:05:33 +0100 Subject: [PATCH] Added a function to change password --- database/pkg.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/database/pkg.go b/database/pkg.go index efa3cc8..6450761 100644 --- a/database/pkg.go +++ b/database/pkg.go @@ -284,5 +284,36 @@ func (db *T) CreateUser(username, password, name string) (userID int64, err erro err = row.Scan(&userID) return } // }}} +func (db *T) ChangePassword(userID int, currentPassword, newPassword string) (changed bool, err error) { // {{{ + var res sql.Result + res, err = db.Conn.Exec(` + UPDATE _webservice.user + SET + "password" = _webservice.password_hash( + /* salt in hex */ + ENCODE(_webservice.gen_random_bytes(16), 'hex'), + + /* password */ + $3::bytea + ) + WHERE + id = $1 AND + "password" = _webservice.password_hash(SUBSTRING(password FROM 1 FOR 32), $2::bytea) + + `, + userID, + currentPassword, + newPassword, + ) + + var rowsAffected int64 + rowsAffected, err = res.RowsAffected() + if err != nil { + return + } + + changed = (rowsAffected == 1) + return +} // }}} // vim: foldmethod=marker