credential.go 1.2 KB

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