credential.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 `csv:"firstname"`
  13. Surname string `csv:"lastname"`
  14. Password string
  15. PlainPassword string
  16. Email string `csv:"email"`
  17. AltEmail string
  18. DateFrom time.Time
  19. DateTo 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. }
  46. func (c *Credential) MailAddress(domain string) string {
  47. return fmt.Sprintf("%s@%s", c.Username(), domain)
  48. }