/customer/user.go

https://gitlab.com/wiliamsouza/apollo · Go · 111 lines · 89 code · 13 blank · 9 comment · 18 complexity · 76c63aaf7a4dabd67ba770d672db642c MD5 · raw file

  1. package customer
  2. import (
  3. "encoding/base64"
  4. "errors"
  5. "regexp"
  6. "time"
  7. "code.google.com/p/go-uuid/uuid"
  8. "code.google.com/p/go.crypto/bcrypt"
  9. "labix.org/v2/mgo/bson"
  10. "github.com/wiliamsouza/apollo/db"
  11. )
  12. // User represent a system user
  13. type User struct {
  14. Name string `bson:"name"`
  15. Email string `bson:"_id"`
  16. Password string `bson:"password"`
  17. APIKey string `bson:"apikey"`
  18. Created time.Time `bson:"created"`
  19. LastLogin time.Time `bson:"lastlogin"`
  20. }
  21. // ValidateEmail check if email is valid
  22. func (u *User) ValidateEmail() (bool, error) {
  23. m, err := regexp.Match(`^[^@]+@[^@]+\.[^@]+$`, []byte(u.Email))
  24. if err != nil {
  25. panic(err)
  26. }
  27. if !m {
  28. return false, errors.New("validation error: email is not valid")
  29. }
  30. return true, nil
  31. }
  32. // ValidatePassword check if password is valid
  33. func (u *User) ValidatePassword(password string) error {
  34. if bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) == nil {
  35. return nil
  36. }
  37. return errors.New("validation error: invalid password")
  38. }
  39. // EncryptPassword before store on DB encrypt user password
  40. func (u *User) EncryptPassword() {
  41. if passwd, err := bcrypt.GenerateFromPassword([]byte(u.Password),
  42. bcrypt.MinCost); err == nil {
  43. u.Password = string(passwd)
  44. }
  45. }
  46. // GenerateAPIKey for new users
  47. func (u *User) GenerateAPIKey() {
  48. token := uuid.New()
  49. u.APIKey = base64.StdEncoding.EncodeToString([]byte(token))
  50. }
  51. // NewUser create a new user, check email, encrypt pass and generate APIKey
  52. func NewUser(name, email, password string) (User, error) {
  53. u := User{Name: name, Email: email, Password: password}
  54. v, err := u.ValidateEmail()
  55. if !v {
  56. return u, err
  57. }
  58. if err != nil {
  59. return u, err
  60. }
  61. u.EncryptPassword()
  62. u.Created = time.Now()
  63. u.GenerateAPIKey()
  64. err = db.Session.User().Insert(&u)
  65. if err != nil {
  66. return u, err
  67. }
  68. var user User
  69. err = db.Session.User().FindId(email).One(&user)
  70. if err != nil {
  71. return user, err
  72. }
  73. return user, nil
  74. }
  75. // GetUserByAPIKey find user by APIKey
  76. func GetUserByAPIKey(APIKey string) (User, error) {
  77. var u User
  78. err := db.Session.User().Find(bson.M{"apikey": APIKey}).One(&u)
  79. if err != nil {
  80. return u, err
  81. }
  82. return u, nil
  83. }
  84. // GetUserByEmail find user by Email
  85. func GetUserByEmail(email string) (User, error) {
  86. var u User
  87. err := db.Session.User().FindId(email).One(&u)
  88. if err != nil {
  89. return u, err
  90. }
  91. return u, nil
  92. }
  93. // DetailUser detail user
  94. func DetailUser(email string) (User, error) {
  95. var user User
  96. err := db.Session.User().FindId(email).One(&user)
  97. return user, err
  98. }