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.
73 lines
1.8 KiB
73 lines
1.8 KiB
package models
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"unicode/utf8"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
type userCtxKeyType string
|
|
|
|
const userCtxKey = userCtxKeyType("luciter_user")
|
|
|
|
// The User model represents a registered user.
|
|
type User struct {
|
|
ID int `db:"id" json:"id"`
|
|
Name string `db:"name" json:"name"`
|
|
PassHash []byte `db:"hash" json:"-"`
|
|
}
|
|
|
|
// SetPassword sets the user's password
|
|
func (user *User) SetPassword(password string) error {
|
|
passwordBytes := []byte(password)
|
|
|
|
if utf8.RuneCount(passwordBytes) < 6 {
|
|
return errors.New("Password is too short (<6 unicode runes)")
|
|
}
|
|
|
|
hash, err := bcrypt.GenerateFromPassword(passwordBytes, bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
user.PassHash = hash
|
|
return nil
|
|
}
|
|
|
|
// CheckPassword checks the user's password against the hash.
|
|
func (user *User) CheckPassword(attempt string) error {
|
|
err := bcrypt.CompareHashAndPassword(user.PassHash, []byte(attempt))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// InContext returns a child context with the value.
|
|
func (user *User) InContext(parent context.Context) context.Context {
|
|
return context.WithValue(parent, userCtxKey, user)
|
|
}
|
|
|
|
// UserFromContext gets the user from context, or `nil` if no user is available.
|
|
func UserFromContext(ctx context.Context) *User {
|
|
v := ctx.Value(userCtxKey)
|
|
if v == nil {
|
|
return nil
|
|
}
|
|
|
|
return v.(*User)
|
|
}
|
|
|
|
// UserRepository is an interface for all database operations
|
|
// the user model makes.
|
|
type UserRepository interface {
|
|
FindByID(ctx context.Context, id int) (User, error)
|
|
FindByName(ctx context.Context, name string) (User, error)
|
|
List(ctx context.Context) ([]User, error)
|
|
Insert(ctx context.Context, user User) (User, error)
|
|
Update(ctx context.Context, user User) error
|
|
Remove(ctx context.Context, user User) error
|
|
}
|