user.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "golang.org/x/crypto/bcrypt"
  6. "log"
  7. "rate-it-api/database"
  8. "regexp"
  9. "strings"
  10. )
  11. // User define a user
  12. type User struct {
  13. UUID string
  14. Pseudo string
  15. Firstname string `default:""`
  16. Lastname string `default:"bite"`
  17. Password string
  18. Email string
  19. Hash string
  20. Verify int
  21. }
  22. type regexCheck struct {
  23. Regexp string
  24. Message string
  25. }
  26. func validatePassword(password string) error {
  27. if password == "" {
  28. return errors.New("Password must be provided")
  29. }
  30. regexPassword := [5]regexCheck{
  31. {".{6,}", "must be at least 6 character long"},
  32. {"[0-9]+", "must contains one digit from 0-9"},
  33. {"[a-z]+", "must contains one lowercase characters"},
  34. {"[A-Z]+", "must contains one uppercase characters"},
  35. {"[\\^#\\.\\@\\$\\%\\[\\]\\;\\:\\,]+", "must contains one of this special parameters : ^, #, ., @, $, %, [, ], ;, or comma:"},
  36. }
  37. /**
  38. * Passwword must fit some requirements:
  39. * - length at least 6 characters
  40. * - must contains one digit from 0-9
  41. * - must contains one lowercase characters
  42. * - must contains one uppercase characters
  43. * - must contains one special symbols in the list \"@#$%\"
  44. * - match anything with previous condition checking
  45. */
  46. for _, regex := range regexPassword {
  47. r, _ := regexp.Compile(regex.Regexp)
  48. match := r.MatchString(password)
  49. if !match {
  50. return errors.New(strings.Join([]string{"Password incorrect", regex.Message}, ": "))
  51. }
  52. }
  53. return nil
  54. }
  55. func validateEmail(email string) error {
  56. if email == "" {
  57. return errors.New("Email must be provided")
  58. }
  59. r, _ := regexp.Compile(".+@.+")
  60. match := r.MatchString(email)
  61. if !match {
  62. return errors.New(strings.Join([]string{"Email incorrect"}, ": "))
  63. }
  64. return nil
  65. }
  66. func (u *User) validate() error {
  67. err := validatePassword(u.Password)
  68. if err != nil {
  69. return err
  70. }
  71. err = validateEmail(u.Email)
  72. if err != nil {
  73. return err
  74. }
  75. return nil
  76. }
  77. // UserGetByEmail retrieve a user following its email
  78. func UserGetByEmail(userEmail string) (User, error) {
  79. var user User
  80. err := validateEmail(userEmail)
  81. if err != nil {
  82. return user, err
  83. }
  84. query := fmt.Sprintf("SELECT uuid, email, password, verify FROM users WHERE email=\"%s\"", userEmail)
  85. return userGet(query)
  86. }
  87. // UserGetByUUID retrieve a user following its hash
  88. func UserGetByUUID(UUID string) (User, error) {
  89. query := fmt.Sprintf("SELECT uuid, email, password, verify FROM users WHERE uuid=\"%s\"", UUID)
  90. return userGet(query)
  91. }
  92. // UserValidate validate a user mail
  93. func UserValidate(uuid string) error {
  94. query := fmt.Sprintf(`UPDATE users SET verify=1 WHERE uuid='%s'`, uuid)
  95. fmt.Println(query)
  96. _, err := database.MysqlExecInsert(query)
  97. if err != nil {
  98. fmt.Println(err.Error())
  99. return err
  100. }
  101. return nil
  102. }
  103. // UserUpdatePassword validate a user mail
  104. func UserUpdatePassword(email string, password string) error {
  105. query := fmt.Sprintf(`UPDATE users SET password="%s" WHERE email='%s'`, password, email)
  106. fmt.Println(query)
  107. _, err := database.MysqlExecInsert(query)
  108. if err != nil {
  109. fmt.Println(err.Error())
  110. return err
  111. }
  112. return nil
  113. }
  114. // userGet retrieve a user
  115. func userGet(query string) (User, error) {
  116. var user User
  117. results, errSelect := database.MysqlExecSelect(query)
  118. if errSelect != nil {
  119. return user, errSelect
  120. }
  121. var (
  122. uuid string
  123. email string
  124. password string
  125. verify int
  126. )
  127. if !results.Next() {
  128. return user, errors.New("User not found")
  129. }
  130. err := results.Scan(&uuid, &email, &password, &verify)
  131. if err != nil {
  132. log.Fatal(err)
  133. }
  134. user = User{
  135. UUID: uuid,
  136. Email: email,
  137. Password: password,
  138. Verify: verify,
  139. }
  140. return user, nil
  141. }
  142. // Create create a new user from json data
  143. func (u *User) Create() error {
  144. err := u.validate()
  145. if err != nil {
  146. return err
  147. }
  148. // password hashing
  149. password, err := bcrypt.GenerateFromPassword([]byte(u.Password), 2)
  150. if err != nil {
  151. return err
  152. }
  153. //Set up hashing data
  154. u.Verify = 0
  155. query := fmt.Sprintf(`INSERT INTO users (uuid, email, password, lastname, firstname, verify)
  156. VALUES ("%s", "%s", "%s", "%s", "%s", "%d")`, u.UUID, u.Email, password, u.Lastname, u.Firstname, u.Verify)
  157. _, err = database.MysqlExecInsert(query)
  158. if err != nil {
  159. return err
  160. }
  161. return nil
  162. }