Advertisement
Guest User

Untitled

a guest
May 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.51 KB | None | 0 0
  1. func (s *Server) createUserHandler(w http.ResponseWriter, r *http.Request) http.Handler {
  2.     usernameRegex := regex.MustCompile(`^[0-9A-Za-z\s]+$`)
  3.  
  4.     const minUsernameLength = 3
  5.     const maxUsernameLength = 20
  6.     const minPasswordLength = 8
  7.     const maxPasswordLength = 64
  8.  
  9.     var (
  10.         shortUsername = ResponseError{fmt.Sprintf("Username must be equal to or more than %d characters.", minUsernameLength), 0},
  11.         longUsername = ResponseError{fmt.Sprintf("Username must be equal to or less than %d characters.", maxUsernameLength), 1},
  12.         shortPassword = ResponseError{fmt.Sprintf("Username must be equal to or more than %d characters.", minPasswordLength), 2},
  13.         longPassword = ResponseError{fmt.Sprintf("Username must be equal to or less than %d characters.", maxPasswordLength), 3},
  14.         notAlphanumericUsername = ResponseError{"Usernames must be alphanumeric.", 4},
  15.     )
  16.  
  17.     type requestUser struct {
  18.         Username string `json:"username"`
  19.         Password string `json:"password"`
  20.         IsAdmin  bool   `json:"isAdmin"`
  21.     }
  22.  
  23.     return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  24.         var reqUser requestUser
  25.  
  26.         if err := json.NewDecoder(r).Decode(&reqUser); err != nil {
  27.             http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError))
  28.             return
  29.         }
  30.  
  31.         if len(reqUser.Username) < minUsernameLength {
  32.             respond.JSON(w, nil, shortUsername)
  33.             return
  34.         } else if len(reqUser.Username) > maxUsernameLength {
  35.             respond.JSON(w, nil, longUsername)
  36.             return
  37.         }
  38.  
  39.         if len(reqUser.Password) < minPasswordLength {
  40.             respond.JSON(w, nil, shortPassword)
  41.             return
  42.         } else if len(reqUser.Password) > maxPasswordLength {
  43.             respond.JSON(w, nil, longPassword)
  44.             return
  45.         }
  46.  
  47.         if !usernameRegex.Match([]byte(reqUser.Username)) {
  48.             respond.JSON(w, nil, notAlphanumericUsername)
  49.             return
  50.         }
  51.  
  52.         hashedPassword, err := bcrypt.GenerateFromPassword([]byte(reqUser.Password), bcrypt.DefaultCost)
  53.         if err != nil {
  54.             s.logger.LogRequestError(r, fmt.Errorf("generating bcrypt hash from password: %v", err))
  55.             http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  56.             return
  57.         }
  58.  
  59.         user := user.User{Username: reqUser.Username, HashedPassword: string(hashedPassword), IsAdmin: reqUser.IsAdmin, IsVerified: s.getIsAdmin(r)}
  60.         if err := s.store.User.Create(user); err != nil {
  61.             s.logger.LogRequestError(r, fmt.Errorf("creating user: %v", err))
  62.             http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  63.             return
  64.         }
  65.     })
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement