Guest User

auth.go

a guest
Apr 3rd, 2018
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.14 KB | None | 0 0
  1. package models
  2.  
  3. import (
  4.     "fmt"
  5.     "reflect"
  6.     "regexp"
  7.  
  8.     "gopkg.in/mgo.v2/bson"
  9.  
  10.     "golang.org/x/crypto/bcrypt"
  11.     "gopkg.in/mgo.v2"
  12. )
  13.  
  14. // URL is the url to the mongodb
  15. var URL = "localhost:27017"
  16.  
  17. //LoginInfo is the body of the login post method
  18. type LoginInfo struct {
  19.     Username string `json:"username"`
  20.     Password string `json:"password"`
  21. }
  22.  
  23. //RegisterReq is the body of the register post method
  24. type RegisterReq struct {
  25.     Username string `json:"username" bson:"username"`
  26.     Password string `json:"password" bson:"password"`
  27.     Email    string `json:"email" bson:"email"`
  28. }
  29.  
  30. //BasicRes is the basic response used for many of the api calls
  31. type BasicRes struct {
  32.     Success bool   `json:"success"`
  33.     Message string `json:"message"`
  34. }
  35.  
  36. //UserData is the model of the user in the mongodb
  37. type UserData struct {
  38.     ID       bson.ObjectId `bson:"_id"`
  39.     Username string        `bson:"username"`
  40.     Password string        `bson:"password"`
  41.     Email    string        `bson:"email"`
  42. }
  43.  
  44. var passwordReg = regexp.MustCompile("^[a-z0-9]{8,35}$")
  45. var emailReg = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
  46. var usernameReg = regexp.MustCompile("^\\w.{3,16}$")
  47.  
  48. //ValidBody checks if the boddy is valid
  49. func (r *RegisterReq) ValidBody() error {
  50.     req := *r
  51.     if req.Username == "" {
  52.         return fmt.Errorf("no username specified")
  53.     }
  54.     if req.Password == "" {
  55.         return fmt.Errorf("no password specified")
  56.     }
  57.     if req.Email == "" {
  58.         return fmt.Errorf("no email specified")
  59.     }
  60.     if !usernameReg.Match([]byte(req.Username)) {
  61.         return fmt.Errorf("username: %v invalid username", req.Username)
  62.     }
  63.     if !emailReg.Match([]byte(req.Email)) {
  64.         return fmt.Errorf("email: %v is invalid", req.Email)
  65.     }
  66.     if !passwordReg.Match([]byte(req.Password)) {
  67.         return fmt.Errorf("password: %v is invalid", req.Password)
  68.     }
  69.  
  70.     return nil
  71. }
  72.  
  73. //HashPass replaces the pass with a hash
  74. func (r *RegisterReq) HashPass() {
  75.     // fmt.Println(r, *r, r.Password)
  76.     hash, _ := bcrypt.GenerateFromPassword([]byte(r.Password), 10)
  77.     r.Password = string(hash)
  78. }
  79.  
  80. //SaveToDB saves the body to the db if it doesn't exist already
  81. func (r RegisterReq) SaveToDB() error {
  82.     exist := make(chan error, 2)
  83.     type Username struct {
  84.         Username string `bson:"username"`
  85.     }
  86.     type Email struct {
  87.         Email string `bson:"email"`
  88.     }
  89.     session, err := mgo.Dial(URL)
  90.     if err != nil {
  91.         return err
  92.     }
  93.     defer session.Close()
  94.     go exists(Username{Username: r.Username}, exist, session)
  95.     go exists(Email{Email: r.Email}, exist, session)
  96.     if err := <-exist; err != nil {
  97.         return err
  98.     }
  99.     if err := <-exist; err != nil {
  100.         return err
  101.     }
  102.     if err := session.DB("dobrich-uslugi").C("users").Insert(r); err != nil {
  103.         return err
  104.     }
  105.     return nil
  106. }
  107.  
  108. //exists checks if the email or username already exists in the db
  109. func exists(param interface{}, ch chan error, session *mgo.Session) {
  110.     var result UserData
  111.     session.DB("dobrich-uslugi").C("users").Find(param).One(&result)
  112.     if (UserData{}) != result {
  113.         ch <- fmt.Errorf("user w/ such %v exists", reflect.TypeOf(param).Name())
  114.         return
  115.     }
  116.     ch <- nil
  117. }
Add Comment
Please, Sign In to add comment