gpaste/users/user.go
Torjus Håkestad e7b0c5fa33
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Add aggressive linting
2022-01-24 20:25:52 +01:00

46 lines
854 B
Go

package users
import (
"fmt"
"golang.org/x/crypto/bcrypt"
)
type Role string
const (
RoleUnset Role = ""
RoleUser Role = "user"
RoleAdmin Role = "admin"
)
type User struct {
Username string `json:"username"`
HashedPassword []byte `json:"hashedPassword"`
Role Role `json:"role"`
}
var ErrNoSuchUser = fmt.Errorf("no such user")
type UserStore interface {
Get(username string) (*User, error)
Store(user *User) error
Delete(username string) error
List() ([]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
}