gpaste/users/user.go

37 lines
766 B
Go
Raw Normal View History

2022-01-20 02:35:55 +00:00
package users
2022-01-19 20:45:53 +00:00
import "golang.org/x/crypto/bcrypt"
2022-01-20 00:19:32 +00:00
type Role string
const (
RoleUnset Role = ""
RoleUser Role = "user"
RoleAdmin Role = "admin"
)
2022-01-19 20:45:53 +00:00
type User struct {
Username string `json:"username"`
HashedPassword []byte `json:"hashed_password"`
2022-01-20 12:33:11 +00:00
Role Role `json:"role"`
2022-01-19 20:45:53 +00:00
}
type UserStore interface {
Get(username string) (*User, error)
Store(user *User) error
Delete(username string) error
}
func (u *User) ValidatePassword(password string) error {
return bcrypt.CompareHashAndPassword(u.HashedPassword, []byte(password))
}
func (u *User) SetPassword(password string) error {
hashed, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return err
}
u.HashedPassword = hashed
return nil
}