credential.go 1.4 KB

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