Plan stuff. Log stuff.
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.

44 lines
874 B

5 years ago
  1. package models
  2. import (
  3. "github.com/gisle/stufflog/slerrors"
  4. "golang.org/x/crypto/bcrypt"
  5. "net/http"
  6. "time"
  7. )
  8. // A User is a user.
  9. type User struct {
  10. ID string `json:"id"`
  11. Name string `json:"name"`
  12. Hash []byte `json:"-"`
  13. }
  14. func (user *User) SetPassword(password string) error {
  15. if len(password) < 6 {
  16. return &slerrors.SLError{Code: http.StatusBadRequest, Text: "Password is too short"}
  17. }
  18. hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
  19. if err != nil {
  20. return err
  21. }
  22. user.Hash = hash
  23. return nil
  24. }
  25. func (user *User) CheckPassword(password string) error {
  26. err := bcrypt.CompareHashAndPassword(user.Hash, []byte(password))
  27. if err != nil {
  28. return slerrors.LoginFailed()
  29. }
  30. return nil
  31. }
  32. type UserSession struct {
  33. ID string `json:"id"`
  34. UserID string `json:"userId"`
  35. Expires time.Time `json:"expires"`
  36. }