The main server, and probably only repository in this org.
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.

54 lines
1.3 KiB

package models
import (
"context"
"errors"
"unicode/utf8"
"golang.org/x/crypto/bcrypt"
)
// 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
}
// 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
}