You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
874 B
44 lines
874 B
package models
|
|
|
|
import (
|
|
"github.com/gisle/stufflog/slerrors"
|
|
"golang.org/x/crypto/bcrypt"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
// A User is a user.
|
|
type User struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Hash []byte `json:"-"`
|
|
}
|
|
|
|
func (user *User) SetPassword(password string) error {
|
|
if len(password) < 6 {
|
|
return &slerrors.SLError{Code: http.StatusBadRequest, Text: "Password is too short"}
|
|
}
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
user.Hash = hash
|
|
return nil
|
|
}
|
|
|
|
func (user *User) CheckPassword(password string) error {
|
|
err := bcrypt.CompareHashAndPassword(user.Hash, []byte(password))
|
|
if err != nil {
|
|
return slerrors.LoginFailed()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type UserSession struct {
|
|
ID string `json:"id"`
|
|
UserID string `json:"userId"`
|
|
Expires time.Time `json:"expires"`
|
|
}
|