2014-01-09 06:42:05 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2014-01-09 23:18:49 +00:00
|
|
|
"encoding/gob"
|
2014-02-10 19:02:44 +00:00
|
|
|
"errors"
|
2014-02-05 00:39:01 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-01-09 06:42:05 +00:00
|
|
|
"net/http"
|
|
|
|
|
2014-03-18 19:28:47 +00:00
|
|
|
"crypto/rand"
|
2015-02-07 23:30:22 +00:00
|
|
|
|
2016-01-12 04:46:48 +00:00
|
|
|
"github.com/gophish/gophish/models"
|
2014-01-09 06:42:05 +00:00
|
|
|
ctx "github.com/gorilla/context"
|
|
|
|
"github.com/gorilla/securecookie"
|
|
|
|
"github.com/gorilla/sessions"
|
2016-01-10 20:54:59 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2014-01-09 06:42:05 +00:00
|
|
|
)
|
|
|
|
|
2014-01-09 23:18:49 +00:00
|
|
|
//init registers the necessary models to be saved in the session later
|
|
|
|
func init() {
|
|
|
|
gob.Register(&models.User{})
|
2014-02-02 20:47:06 +00:00
|
|
|
gob.Register(&models.Flash{})
|
2014-01-09 23:18:49 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 04:37:42 +00:00
|
|
|
var Store = sessions.NewCookieStore(
|
|
|
|
[]byte(securecookie.GenerateRandomKey(64)), //Signing key
|
2014-01-11 06:10:52 +00:00
|
|
|
[]byte(securecookie.GenerateRandomKey(32)))
|
2014-01-09 06:42:05 +00:00
|
|
|
|
2014-02-10 19:02:44 +00:00
|
|
|
var ErrInvalidPassword = errors.New("Invalid Password")
|
|
|
|
|
2014-02-05 00:39:01 +00:00
|
|
|
// Login attempts to login the user given a request.
|
2014-01-11 06:10:52 +00:00
|
|
|
func Login(r *http.Request) (bool, error) {
|
2014-01-09 06:42:05 +00:00
|
|
|
username, password := r.FormValue("username"), r.FormValue("password")
|
|
|
|
session, _ := Store.Get(r, "gophish")
|
2014-03-25 03:31:33 +00:00
|
|
|
u, err := models.GetUserByUsername(username)
|
2014-03-26 04:53:51 +00:00
|
|
|
if err != nil && err != models.ErrUsernameTaken {
|
2014-01-09 06:42:05 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
//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 {
|
2014-01-09 23:18:49 +00:00
|
|
|
ctx.Set(r, "user", nil)
|
2014-03-18 19:35:02 +00:00
|
|
|
return false, ErrInvalidPassword
|
2014-01-09 06:42:05 +00:00
|
|
|
}
|
2014-01-09 23:18:49 +00:00
|
|
|
ctx.Set(r, "user", u)
|
|
|
|
session.Values["id"] = u.Id
|
2014-01-09 06:42:05 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2014-02-05 00:39:01 +00:00
|
|
|
// Register attempts to register the user given a request.
|
|
|
|
func Register(r *http.Request) (bool, error) {
|
|
|
|
username, password := r.FormValue("username"), r.FormValue("password")
|
2014-03-25 03:31:33 +00:00
|
|
|
u, err := models.GetUserByUsername(username)
|
2014-02-10 19:02:44 +00:00
|
|
|
// If we have an error which is not simply indicating that no user was found, report it
|
2015-02-07 23:30:22 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2014-02-06 16:49:53 +00:00
|
|
|
return false, err
|
2014-02-05 00:39:01 +00:00
|
|
|
}
|
2015-02-07 23:30:22 +00:00
|
|
|
fmt.Println("Made it here!")
|
|
|
|
u = models.User{}
|
2014-02-05 00:39:01 +00:00
|
|
|
//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
|
|
|
|
}
|
2015-02-07 23:30:22 +00:00
|
|
|
u.Username = username
|
|
|
|
u.Hash = string(h)
|
|
|
|
u.ApiKey = GenerateSecureKey()
|
|
|
|
err = models.PutUser(&u)
|
2014-02-05 00:39:01 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateSecureKey() string {
|
|
|
|
// Inspired from gorilla/securecookie
|
|
|
|
k := make([]byte, 32)
|
|
|
|
io.ReadFull(rand.Reader, k)
|
|
|
|
return fmt.Sprintf("%x", k)
|
|
|
|
}
|
2014-02-06 16:49:53 +00:00
|
|
|
|
2014-02-10 19:02:44 +00:00
|
|
|
func ChangePassword(r *http.Request) error {
|
|
|
|
u := ctx.Get(r, "user").(models.User)
|
|
|
|
c, n := r.FormValue("current_password"), r.FormValue("new_password")
|
2014-02-06 16:49:53 +00:00
|
|
|
// Check the current password
|
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(u.Hash), []byte(c))
|
|
|
|
if err != nil {
|
2014-02-10 19:02:44 +00:00
|
|
|
return ErrInvalidPassword
|
2014-02-06 16:49:53 +00:00
|
|
|
} else {
|
|
|
|
// Generate the new hash
|
|
|
|
h, err := bcrypt.GenerateFromPassword([]byte(n), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
2014-02-10 19:02:44 +00:00
|
|
|
return err
|
2014-02-06 16:49:53 +00:00
|
|
|
}
|
|
|
|
u.Hash = string(h)
|
2014-03-25 03:31:33 +00:00
|
|
|
if err = models.PutUser(&u); err != nil {
|
2014-02-10 19:02:44 +00:00
|
|
|
return err
|
2014-02-06 16:49:53 +00:00
|
|
|
}
|
2014-02-10 19:02:44 +00:00
|
|
|
return nil
|
2014-02-06 16:49:53 +00:00
|
|
|
}
|
|
|
|
}
|