credential.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package orm
  2. import (
  3. "crypto/sha1"
  4. "encoding/base64"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "github.com/sethvargo/go-password/password"
  9. )
  10. type Checkbox bool
  11. type Credential struct {
  12. Name string
  13. Surname string
  14. Password string
  15. PlainPassword string
  16. Email string
  17. AltEmail string
  18. FromDate time.Time
  19. ToDate time.Time
  20. TelephoneNumber string
  21. Regenerate bool `schema:"Regenerate" sql:"default: false"`
  22. }
  23. func (c *Credential) sanitize(s string) string {
  24. lower := strings.ToLower(s)
  25. r := strings.NewReplacer("'", "", "-", "", " ", "", "ò", "o", "ì", "i")
  26. return r.Replace(lower)
  27. }
  28. func (c *Credential) Username() string {
  29. return strings.Join([]string{c.sanitize(c.Name), c.sanitize(c.Surname)}, ".")
  30. }
  31. func (c *Credential) SaltPassword(password string) string {
  32. bs := sha1.Sum([]byte(password + "salt"))
  33. str := base64.StdEncoding.EncodeToString(append(bs[:], []byte("salt")...))
  34. return fmt.Sprintf("%s", str)
  35. }
  36. func (c *Credential) GeneratePassword() (string, error) {
  37. password, err := password.Generate(8, 2, 0, false, true)
  38. if err != nil {
  39. return "", err
  40. }
  41. return password, nil
  42. }
  43. func (c *Credential) CompleteName() string {
  44. return fmt.Sprintf("%s, %s", c.Name, c.Surname)
  45. }