mirror of
https://github.com/neilalexander/yggmail.git
synced 2026-04-18 16:16:29 +03:00
Single user refactor
This commit is contained in:
@@ -3,6 +3,8 @@ package sqlite3
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type TableConfig struct {
|
||||
@@ -59,3 +61,26 @@ func (t *TableConfig) ConfigSet(key, value string) error {
|
||||
_, err := t.set.Exec(key, value)
|
||||
return err
|
||||
}
|
||||
|
||||
func (t *TableConfig) ConfigSetPassword(password string) error {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return fmt.Errorf("bcrypt.GenerateFromPassword: %w", err)
|
||||
}
|
||||
return t.ConfigSet("password", string(hash))
|
||||
}
|
||||
|
||||
func (t *TableConfig) ConfigTryPassword(password string) (bool, error) {
|
||||
dbPasswordHash, err := t.ConfigGet("password")
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if dbPasswordHash == "" {
|
||||
return true, nil // TODO: Do we want to allow login if no password is set?
|
||||
}
|
||||
err = bcrypt.CompareHashAndPassword([]byte(dbPasswordHash), []byte(password))
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user