2014-01-09 06:42:05 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2014-02-10 19:02:44 +00:00
|
|
|
"errors"
|
2014-01-09 06:42:05 +00:00
|
|
|
"net/http"
|
|
|
|
|
2016-09-15 03:24:51 +00:00
|
|
|
ctx "github.com/gophish/gophish/context"
|
2016-01-12 04:46:48 +00:00
|
|
|
"github.com/gophish/gophish/models"
|
2016-01-10 20:54:59 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2014-01-09 06:42:05 +00:00
|
|
|
)
|
|
|
|
|
2016-01-25 02:47:16 +00:00
|
|
|
// ErrInvalidPassword is thrown when a user provides an incorrect password.
|
2014-02-10 19:02:44 +00:00
|
|
|
var ErrInvalidPassword = errors.New("Invalid Password")
|
|
|
|
|
2019-05-31 18:58:18 +00:00
|
|
|
// ErrPasswordMismatch is thrown when a user provides a blank password to the register
|
2016-02-13 22:37:12 +00:00
|
|
|
// or change password functions
|
2019-05-31 18:58:18 +00:00
|
|
|
var ErrPasswordMismatch = errors.New("Password cannot be blank")
|
2016-03-02 13:30:46 +00:00
|
|
|
|
2019-05-31 18:58:18 +00:00
|
|
|
// ErrEmptyPassword is thrown when a user provides a blank password to the register
|
|
|
|
// or change password functions
|
|
|
|
var ErrEmptyPassword = errors.New("No password provided")
|
2018-01-13 22:35:58 +00:00
|
|
|
|
2014-02-05 00:39:01 +00:00
|
|
|
// Login attempts to login the user given a request.
|
2016-09-15 03:24:51 +00:00
|
|
|
func Login(r *http.Request) (bool, models.User, error) {
|
2014-01-09 06:42:05 +00:00
|
|
|
username, password := r.FormValue("username"), r.FormValue("password")
|
2014-03-25 03:31:33 +00:00
|
|
|
u, err := models.GetUserByUsername(username)
|
2018-01-12 00:37:38 +00:00
|
|
|
if err != nil {
|
2016-09-15 03:24:51 +00:00
|
|
|
return false, models.User{}, err
|
2014-01-09 06:42:05 +00:00
|
|
|
}
|
|
|
|
//If we've made it here, we should have a valid user stored in u
|
|
|
|
//Let's check the password
|
2014-01-09 23:18:49 +00:00
|
|
|
err = bcrypt.CompareHashAndPassword([]byte(u.Hash), []byte(password))
|
2014-01-09 06:42:05 +00:00
|
|
|
if err != nil {
|
2016-09-15 03:24:51 +00:00
|
|
|
return false, models.User{}, ErrInvalidPassword
|
2014-01-09 06:42:05 +00:00
|
|
|
}
|
2016-09-15 03:24:51 +00:00
|
|
|
return true, u, nil
|
2014-01-09 06:42:05 +00:00
|
|
|
}
|
|
|
|
|
2018-12-16 03:38:51 +00:00
|
|
|
// ChangePassword verifies the current password provided in the request and,
|
|
|
|
// if it's valid, changes the password for the authenticated user.
|
2014-02-10 19:02:44 +00:00
|
|
|
func ChangePassword(r *http.Request) error {
|
|
|
|
u := ctx.Get(r, "user").(models.User)
|
2016-03-02 13:30:46 +00:00
|
|
|
currentPw := r.FormValue("current_password")
|
2016-03-03 00:59:40 +00:00
|
|
|
newPassword := r.FormValue("new_password")
|
|
|
|
confirmPassword := r.FormValue("confirm_new_password")
|
2014-02-06 16:49:53 +00:00
|
|
|
// Check the current password
|
2016-03-02 13:30:46 +00:00
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(u.Hash), []byte(currentPw))
|
2014-02-06 16:49:53 +00:00
|
|
|
if err != nil {
|
2014-02-10 19:02:44 +00:00
|
|
|
return ErrInvalidPassword
|
2014-02-06 16:49:53 +00:00
|
|
|
}
|
2016-02-13 22:37:12 +00:00
|
|
|
// Check that the new password isn't blank
|
2016-03-03 00:59:40 +00:00
|
|
|
if newPassword == "" {
|
2016-02-13 22:37:12 +00:00
|
|
|
return ErrEmptyPassword
|
|
|
|
}
|
2016-03-02 13:30:46 +00:00
|
|
|
// Check that new passwords match
|
2016-03-03 00:59:40 +00:00
|
|
|
if newPassword != confirmPassword {
|
2016-03-02 13:30:46 +00:00
|
|
|
return ErrPasswordMismatch
|
|
|
|
}
|
2016-02-13 22:37:12 +00:00
|
|
|
// Generate the new hash
|
2016-03-03 00:59:40 +00:00
|
|
|
h, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost)
|
2016-02-13 22:37:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.Hash = string(h)
|
|
|
|
if err = models.PutUser(&u); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2014-02-06 16:49:53 +00:00
|
|
|
}
|