func UserPasswordPOST(w http.ResponseWriter, r *http.Request) { // Get session sess := session.Instance(r) // Validate with required fields if validate, missingField := view.Validate(r, []string{"passwordold", "passwordnew"}); !validate { sess.AddFlash(view.Flash{"Field missing: " + missingField, view.FlashError}) sess.Save(r, w) UserPasswordGET(w, r) return } user_id := int64(sess.Values["id"].(uint32)) // Form values passwordOld := r.FormValue("passwordold") passwordNew, errp := passhash.HashString(r.FormValue("passwordnew")) if passwordOld == r.FormValue("passwordnew") { sess.AddFlash(view.Flash{"New password cannot be the same as the old password.", view.FlashError}) sess.Save(r, w) UserPasswordGET(w, r) return } // If password hashing failed if errp != nil { log.Println(errp) sess.AddFlash(view.Flash{"An error occurred on the server. Please try again later.", view.FlashError}) sess.Save(r, w) UserPasswordGET(w, r) return } // Get database result result, err := model.UserByEmail(fmt.Sprintf("%v", sess.Values["email"])) // Determine if user exists if err == sql.ErrNoRows { sess.AddFlash(view.Flash{"Password is incorrect.", view.FlashWarning}) sess.Save(r, w) UserPasswordGET(w, r) return } else if err != nil { // Display error message log.Println(err) sess.AddFlash(view.Flash{"There was an error. Please try again later.", view.FlashError}) sess.Save(r, w) UserPasswordGET(w, r) return } else if passhash.MatchString(result.Password, passwordOld) { err = model.UserPasswordUpdate(user_id, passwordNew) if err != nil { log.Println(err) sess.AddFlash(view.Flash{"There was an error. Please try again later.", view.FlashError}) sess.Save(r, w) UserPasswordGET(w, r) return } // Password matches sess.AddFlash(view.Flash{"Password changed!", view.FlashSuccess}) sess.Save(r, w) http.Redirect(w, r, "/", http.StatusFound) return } else { sess.AddFlash(view.Flash{"Password is incorrect.", view.FlashWarning}) sess.Save(r, w) UserPasswordGET(w, r) return } }
func LoginPOST(w http.ResponseWriter, r *http.Request) { // Get session sess := session.Instance(r) // Prevent brute force login attempts by not hitting MySQL and pretending like it was invalid :-) if sess.Values["login_attempt"] != nil && sess.Values["login_attempt"].(int) >= 5 { log.Println("Brute force login prevented") sess.AddFlash(view.Flash{"Sorry, no brute force :-)", view.FlashNotice}) sess.Save(r, w) LoginGET(w, r) return } // Validate with required fields if validate, missingField := view.Validate(r, []string{"email", "password"}); !validate { sess.AddFlash(view.Flash{"Field missing: " + missingField, view.FlashError}) sess.Save(r, w) LoginGET(w, r) return } // Validate with Google reCAPTCHA if !recaptcha.Verified(r) { sess.AddFlash(view.Flash{"reCAPTCHA invalid!", view.FlashError}) sess.Save(r, w) LoginGET(w, r) return } // Form values email := r.FormValue("email") password := r.FormValue("password") // Get database result result, err := model.UserByEmail(email) // Determine if user exists if err == sql.ErrNoRows { loginAttempt(sess) sess.AddFlash(view.Flash{"Password is incorrect - Attempt: " + fmt.Sprintf("%v", sess.Values["login_attempt"]), view.FlashWarning}) sess.Save(r, w) } else if err != nil { // Display error message log.Println(err) sess.AddFlash(view.Flash{"There was an error. Please try again later.", view.FlashError}) sess.Save(r, w) } else if passhash.MatchString(result.Password, password) { if result.Status_id == 2 { // User inactive and display inactive message sess.AddFlash(view.Flash{"Account is inactive so login is disabled.", view.FlashNotice}) sess.Save(r, w) } else if result.Status_id == 3 { // User email not verified sess.AddFlash(view.Flash{"You must confirm your email before login is allowed.", view.FlashWarning}) sess.Save(r, w) } else if result.Status_id == 4 { // User email not re-verified sess.AddFlash(view.Flash{"You must re-confirm your email before login is allowed.", view.FlashWarning}) sess.Save(r, w) } else if result.Status_id == 1 { // Log the login err = model.UserLoginCreate(int64(result.Id), r) if err != nil { log.Println(err) } // Login successfully clearSessionVariables(sess) sess.AddFlash(view.Flash{"Login successful!", view.FlashSuccess}) sess.Values["id"] = result.Id sess.Values["email"] = email sess.Values["first_name"] = result.First_name sess.Values["last_name"] = result.Last_name sess.Save(r, w) http.Redirect(w, r, "/", http.StatusFound) return } else { // User status unknown sess.AddFlash(view.Flash{"Account status is unknown so login is disabled.", view.FlashNotice}) sess.Save(r, w) } } else { loginAttempt(sess) sess.AddFlash(view.Flash{"Password is incorrect - Attempt: " + fmt.Sprintf("%v", sess.Values["login_attempt"]), view.FlashWarning}) sess.Save(r, w) } // Show the login page again LoginGET(w, r) }