summaryrefslogtreecommitdiff
path: root/db/user.go
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2023-05-01 14:07:39 -0500
committersanine-a <sanine.not@pm.me>2023-05-01 14:07:39 -0500
commit416055e02b74f640ab6030deaa9f7767221a49cd (patch)
treeae6f1696404f083c805297bbd025a240841e641e /db/user.go
parentaa7c3eb23c31fa6051ee0caaebea47c8225d55a4 (diff)
add additional configuration functions for users and endpoints
Diffstat (limited to 'db/user.go')
-rw-r--r--db/user.go28
1 files changed, 25 insertions, 3 deletions
diff --git a/db/user.go b/db/user.go
index 37c0744..c1f2efe 100644
--- a/db/user.go
+++ b/db/user.go
@@ -30,6 +30,17 @@ func saltPassword(password string, salt []byte) []byte {
}
+func hashPassword(password string, salt []byte) ([]byte, error) {
+ salted := saltPassword(password, salt)
+ hash, err := bcrypt.GenerateFromPassword(salted, bcrypt.DefaultCost)
+ if err != nil {
+ return []byte{}, err
+ }
+
+ return hash, nil
+}
+
+
func (p *Phlox) CreateUser(username, password string) (User, error) {
user := User{}
@@ -44,9 +55,7 @@ func (p *Phlox) CreateUser(username, password string) (User, error) {
return user, err
}
- salted := saltPassword(password, salt)
-
- hash, err := bcrypt.GenerateFromPassword(salted, bcrypt.DefaultCost)
+ hash, err := hashPassword(password, salt)
if err != nil {
return user, err
}
@@ -69,6 +78,19 @@ func (p *Phlox) CreateUser(username, password string) (User, error) {
+func (p *Phlox) SetPassword(user User, password string) error {
+ hash, err := hashPassword(password, user.Salt)
+ if err != nil {
+ return err
+ }
+ hash64 := base64.StdEncoding.EncodeToString(hash)
+
+ _, err = p.db.Exec("update users set passwordhash=? where userid=?;", hash64, user.Id)
+ return err
+}
+
+
+
func extractUser(s Scanner) (User, error) {
var userid int
var username string