credential.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. InternalTelephoneNumber string
  22. Regenerate bool `schema:"Regenerate" sql:"default: false"`
  23. Exclude bool
  24. }
  25. func (c *Credential) sanitize(s string) string {
  26. lower := strings.ToLower(s)
  27. r := strings.NewReplacer("'", "", "-", "", " ", "", "ò", "o", "ì", "i")
  28. return r.Replace(lower)
  29. }
  30. func (c *Credential) Username() string {
  31. return strings.Join([]string{c.sanitize(c.Name), c.sanitize(c.Surname)}, ".")
  32. }
  33. func (c *Credential) SaltPassword(password string) string {
  34. bs := sha1.Sum([]byte(password + "salt"))
  35. str := base64.StdEncoding.EncodeToString(append(bs[:], []byte("salt")...))
  36. return fmt.Sprintf("%s", str)
  37. }
  38. func (c *Credential) GeneratePassword() (string, error) {
  39. password, err := password.Generate(8, 2, 0, false, true)
  40. if err != nil {
  41. return "", err
  42. }
  43. return password, nil
  44. }
  45. func (c *Credential) CompleteName() string {
  46. return fmt.Sprintf("%s, %s", c.Name, c.Surname)
  47. }
  48. func (c *Credential) MailAddress(domain string) string {
  49. return fmt.Sprintf("%s@%s", c.Username(), domain)
  50. }