gpaste/users/user.go
Torjus Håkestad 6fdd55def8
All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
Add custom claims
2022-01-20 13:33:11 +01:00

37 lines
766 B
Go

package users
import "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:"hashed_password"`
Role Role `json:"role"`
}
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
}