2014-01-09 06:42:05 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2014-02-10 19:02:44 +00:00
|
|
|
"database/sql"
|
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-02-05 00:39:01 +00:00
|
|
|
"crypto/rand"
|
2014-01-09 06:42:05 +00:00
|
|
|
"code.google.com/p/go.crypto/bcrypt"
|
|
|
|
ctx "github.com/gorilla/context"
|
|
|
|
"github.com/gorilla/securecookie"
|
|
|
|
"github.com/gorilla/sessions"
|
2014-01-09 23:18:49 +00:00
|
|
|
"github.com/jordan-wright/gophish/db"
|
2014-01-09 06:42:05 +00:00
|
|
|
"github.com/jordan-wright/gophish/models"
|
|
|
|
)
|
|
|
|
|
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-02-06 16:49:53 +00:00
|
|
|
u, err := db.GetUserByUsername(username)
|
|
|
|
if err != db.ErrUsernameTaken {
|
2014-01-10 03:21:54 +00:00
|
|
|
//Return false, but don't return an error
|
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-02-06 16:49:53 +00:00
|
|
|
fmt.Println("Error in comparing hash and password")
|
2014-01-09 23:18:49 +00:00
|
|
|
ctx.Set(r, "user", nil)
|
2014-01-09 06:42:05 +00:00
|
|
|
//Return false, but don't return an error
|
|
|
|
return false, nil
|
|
|
|
}
|
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-02-06 16:49:53 +00:00
|
|
|
u, err := db.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
|
|
|
|
if err != sql.ErrNoRows {
|
2014-02-06 16:49:53 +00:00
|
|
|
return false, err
|
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)
|
|
|
|
u.Username = username
|
|
|
|
u.Hash = string(h)
|
|
|
|
u.APIKey = GenerateSecureKey()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
err = db.Conn.Insert(&u)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
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-02-10 19:02:44 +00:00
|
|
|
if err = db.PutUser(&u); err != nil {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|