// Register attempts to register the user given a request. func Register(r *http.Request) (bool, error) { username := r.FormValue("username") newPassword := r.FormValue("password") confirmPassword := r.FormValue("confirm_password") u, err := models.GetUserByUsername(username) // If we have an error which is not simply indicating that no user was found, report it if err != nil { fmt.Println(err) return false, err } u = models.User{} // If we've made it here, we should have a valid username given // Check that the passsword isn't blank if newPassword == "" { return false, ErrEmptyPassword } // Make sure passwords match if newPassword != confirmPassword { return false, ErrPasswordMismatch } // Let's create the password hash h, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost) if err != nil { return false, err } u.Username = username u.Hash = string(h) u.ApiKey = GenerateSecureKey() err = models.PutUser(&u) return true, nil }
func ChangePassword(r *http.Request) error { u := ctx.Get(r, "user").(models.User) currentPw := r.FormValue("current_password") newPassword := r.FormValue("new_password") confirmPassword := r.FormValue("confirm_new_password") // Check the current password err := bcrypt.CompareHashAndPassword([]byte(u.Hash), []byte(currentPw)) if err != nil { return ErrInvalidPassword } // Check that the new password isn't blank if newPassword == "" { return ErrEmptyPassword } // Check that new passwords match if newPassword != confirmPassword { return ErrPasswordMismatch } // Generate the new hash h, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost) if err != nil { return err } u.Hash = string(h) if err = models.PutUser(&u); err != nil { return err } return nil }
// API (/api/reset) resets a user's API key func API_Reset(w http.ResponseWriter, r *http.Request) { switch { case r.Method == "POST": u := ctx.Get(r, "user").(models.User) u.ApiKey = auth.GenerateSecureKey() err := models.PutUser(&u) if err != nil { http.Error(w, "Error setting API Key", http.StatusInternalServerError) } else { JSONResponse(w, models.Response{Success: true, Message: "API Key successfully reset!", Data: u.ApiKey}, http.StatusOK) } } }
func ChangePassword(r *http.Request) error { u := ctx.Get(r, "user").(models.User) c, n := r.FormValue("current_password"), r.FormValue("new_password") // Check the current password err := bcrypt.CompareHashAndPassword([]byte(u.Hash), []byte(c)) if err != nil { return ErrInvalidPassword } else { // Generate the new hash h, err := bcrypt.GenerateFromPassword([]byte(n), bcrypt.DefaultCost) if err != nil { return err } u.Hash = string(h) if err = models.PutUser(&u); err != nil { return err } return nil } }
// Register attempts to register the user given a request. func Register(r *http.Request) (bool, error) { username, password := r.FormValue("username"), r.FormValue("password") u, err := models.GetUserByUsername(username) // If we have an error which is not simply indicating that no user was found, report it if err != nil { fmt.Println(err) return false, err } u = models.User{} //If we've made it here, we should have a valid username given //Let's create the password hash h, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil { return false, err } u.Username = username u.Hash = string(h) u.ApiKey = GenerateSecureKey() err = models.PutUser(&u) return true, nil }