2020-06-22 15:16:26 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Password represents a password hash.
|
|
|
|
type Password struct {
|
2020-09-13 17:51:43 +02:00
|
|
|
UID string `gorm:"type:VARBINARY(255);primary_key;" json:"UID"`
|
|
|
|
Hash string `deepcopier:"skip" gorm:"type:VARBINARY(255);" json:"Hash"`
|
2020-06-22 15:16:26 +02:00
|
|
|
CreatedAt time.Time `deepcopier:"skip" json:"CreatedAt"`
|
|
|
|
UpdatedAt time.Time `deepcopier:"skip" json:"UpdatedAt"`
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// TableName returns the entity table name.
|
2022-09-02 21:30:50 +02:00
|
|
|
func (Password) TableName() string {
|
|
|
|
return "passwords"
|
|
|
|
}
|
|
|
|
|
2020-06-25 01:20:58 +02:00
|
|
|
// NewPassword creates a new password instance.
|
2020-06-22 15:16:26 +02:00
|
|
|
func NewPassword(uid, password string) Password {
|
|
|
|
if uid == "" {
|
2022-01-05 11:40:44 +01:00
|
|
|
panic("auth: cannot set password without uid")
|
2020-06-22 15:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m := Password{UID: uid}
|
|
|
|
|
|
|
|
if password != "" {
|
|
|
|
if err := m.SetPassword(password); err != nil {
|
2020-06-27 14:15:25 +02:00
|
|
|
log.Errorf("auth: failed setting password for %s", uid)
|
2020-06-22 15:16:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-06-25 01:20:58 +02:00
|
|
|
// SetPassword sets a new password stored as hash.
|
2020-06-22 15:16:26 +02:00
|
|
|
func (m *Password) SetPassword(password string) error {
|
|
|
|
if bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
m.Hash = string(bytes)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 01:20:58 +02:00
|
|
|
// InvalidPassword returns true if the given password does not match the hash.
|
2020-06-22 15:16:26 +02:00
|
|
|
func (m *Password) InvalidPassword(password string) bool {
|
|
|
|
if m.Hash == "" && password == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(m.Hash), []byte(password))
|
|
|
|
return err != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create inserts a new row to the database.
|
|
|
|
func (m *Password) Create() error {
|
|
|
|
return Db().Create(m).Error
|
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// Save updates the record in the database or inserts a new record if it does not already exist.
|
2020-06-22 15:16:26 +02:00
|
|
|
func (m *Password) Save() error {
|
|
|
|
return Db().Save(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindPassword returns an entity pointer if exists.
|
|
|
|
func FindPassword(uid string) *Password {
|
|
|
|
result := Password{}
|
|
|
|
|
|
|
|
if err := Db().Where("uid = ?", uid).First(&result).Error; err == nil {
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the password hash.
|
|
|
|
func (m *Password) String() string {
|
|
|
|
return m.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unknown returns true if the password is an empty string.
|
|
|
|
func (m *Password) Unknown() bool {
|
|
|
|
return m.Hash == ""
|
|
|
|
}
|