mirror of
https://github.com/Unkn0wnCat/matrix-veles.git
synced 2025-04-28 17:56:49 +02:00
45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package model
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
type DBUser struct {
|
|
ID primitive.ObjectID `bson:"_id" json:"id"`
|
|
|
|
Username string `bson:"username" json:"username"` // Username is the username the user has
|
|
HashedPassword string `bson:"password" json:"password"` // HashedPassword contains the bcrypt-ed password
|
|
|
|
MatrixLinks []*string `bson:"matrix_links" json:"matrix_links"` // MatrixLinks is the matrix-users this user has verified ownership over
|
|
PendingMatrixLinks []*string `bson:"pending_matrix_links" json:"pending_matrix_links"` // PendingMatrixLinks is the matrix-users pending verification
|
|
|
|
Password *string `bson:"-" json:"-"` // Password may never be sent out!
|
|
}
|
|
|
|
func (usr *DBUser) HashPassword() error {
|
|
if usr.Password == nil {
|
|
return nil
|
|
}
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(*usr.Password), 14)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
usr.HashedPassword = base64.StdEncoding.EncodeToString(hash)
|
|
usr.Password = nil
|
|
return nil
|
|
}
|
|
|
|
func (usr *DBUser) CheckPassword(password string) error {
|
|
hash, err := base64.StdEncoding.DecodeString(usr.HashedPassword)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = bcrypt.CompareHashAndPassword(hash, []byte(password))
|
|
|
|
return err
|
|
}
|