2014-01-09 06:42:05 +00:00
|
|
|
package controllers
|
2013-12-03 04:56:55 +00:00
|
|
|
|
|
|
|
import (
|
2014-01-10 04:21:12 +00:00
|
|
|
"fmt"
|
2014-01-06 06:09:41 +00:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
|
2014-01-10 03:21:54 +00:00
|
|
|
ctx "github.com/gorilla/context"
|
2013-12-03 04:56:55 +00:00
|
|
|
"github.com/gorilla/mux"
|
2014-01-10 03:21:54 +00:00
|
|
|
"github.com/gorilla/sessions"
|
2014-01-09 06:42:05 +00:00
|
|
|
"github.com/jordan-wright/gophish/auth"
|
2014-01-11 04:11:44 +00:00
|
|
|
mid "github.com/jordan-wright/gophish/middleware"
|
2014-01-10 03:21:54 +00:00
|
|
|
"github.com/jordan-wright/gophish/models"
|
2013-12-03 04:56:55 +00:00
|
|
|
)
|
|
|
|
|
2014-02-01 02:49:22 +00:00
|
|
|
var templateDelims = []string{"{{%", "%}}"}
|
|
|
|
|
2014-01-11 04:37:42 +00:00
|
|
|
func CreateRouter() *mux.Router {
|
2013-12-03 04:56:55 +00:00
|
|
|
router := mux.NewRouter()
|
|
|
|
// Base Front-end routes
|
|
|
|
router.HandleFunc("/login", Login)
|
|
|
|
router.HandleFunc("/register", Register)
|
2014-01-11 04:37:42 +00:00
|
|
|
router.HandleFunc("/", Use(Base, mid.RequireLogin))
|
|
|
|
router.HandleFunc("/campaigns/{id}", Use(Campaigns_Id, mid.RequireLogin))
|
|
|
|
router.HandleFunc("/users", Use(Users, mid.RequireLogin))
|
|
|
|
router.HandleFunc("/settings", Use(Settings, mid.RequireLogin))
|
2013-12-03 04:56:55 +00:00
|
|
|
|
|
|
|
// Create the API routes
|
|
|
|
api := router.PathPrefix("/api").Subrouter()
|
2014-02-01 03:49:35 +00:00
|
|
|
api.HandleFunc("/", Use(API, mid.RequireLogin))
|
2014-01-31 04:46:25 +00:00
|
|
|
api.HandleFunc("/campaigns", Use(API_Campaigns, mid.RequireAPIKey))
|
|
|
|
api.HandleFunc("/campaigns/{id}", Use(API_Campaigns_Id, mid.RequireAPIKey))
|
2013-12-03 04:56:55 +00:00
|
|
|
|
|
|
|
//Setup static file serving
|
|
|
|
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))
|
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
2014-01-11 04:11:44 +00:00
|
|
|
// Use allows us to stack middleware to process the request
|
|
|
|
// Example taken from https://github.com/gorilla/mux/pull/36#issuecomment-25849172
|
2014-01-11 04:37:42 +00:00
|
|
|
func Use(handler http.HandlerFunc, mid ...func(http.Handler) http.HandlerFunc) http.HandlerFunc {
|
2014-01-11 04:11:44 +00:00
|
|
|
for _, m := range mid {
|
|
|
|
handler = m(handler)
|
|
|
|
}
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
2013-12-03 04:56:55 +00:00
|
|
|
func Register(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// If it is a post request, attempt to register the account
|
|
|
|
// Now that we are all registered, we can log the user in
|
|
|
|
Login(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Base(w http.ResponseWriter, r *http.Request) {
|
2014-01-06 06:09:41 +00:00
|
|
|
// Example of using session - will be removed.
|
2014-01-10 04:21:12 +00:00
|
|
|
params := struct {
|
|
|
|
User models.User
|
|
|
|
Title string
|
|
|
|
Flashes []interface{}
|
2014-01-13 04:39:40 +00:00
|
|
|
}{Title: "Dashboard", User: ctx.Get(r, "user").(models.User)}
|
2014-01-30 21:08:14 +00:00
|
|
|
getTemplate(w, "dashboard").ExecuteTemplate(w, "base", params)
|
2013-12-03 04:56:55 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 07:00:22 +00:00
|
|
|
func Users(w http.ResponseWriter, r *http.Request) {
|
2014-01-10 03:21:54 +00:00
|
|
|
getTemplate(w, "users").ExecuteTemplate(w, "base", nil)
|
2013-12-12 07:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Settings(w http.ResponseWriter, r *http.Request) {
|
2014-01-30 21:15:12 +00:00
|
|
|
params := struct {
|
|
|
|
User models.User
|
|
|
|
Title string
|
|
|
|
}{Title: "Settings", User: ctx.Get(r, "user").(models.User)}
|
|
|
|
getTemplate(w, "settings").ExecuteTemplate(w, "base", params)
|
2013-12-12 07:00:22 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 04:11:44 +00:00
|
|
|
func Campaigns_Id(w http.ResponseWriter, r *http.Request) {
|
2014-02-01 03:49:35 +00:00
|
|
|
params := struct {
|
|
|
|
User models.User
|
|
|
|
Title string
|
|
|
|
Flashes []interface{}
|
|
|
|
}{Title: "Results", User: ctx.Get(r, "user").(models.User)}
|
|
|
|
getTemplate(w, "dashboard").ExecuteTemplate(w, "base", params)
|
2013-12-03 04:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Login(w http.ResponseWriter, r *http.Request) {
|
2014-01-10 03:21:54 +00:00
|
|
|
params := struct {
|
|
|
|
User models.User
|
|
|
|
Title string
|
|
|
|
Flashes []interface{}
|
2014-01-13 04:39:40 +00:00
|
|
|
}{Title: "Login"}
|
2014-01-10 03:21:54 +00:00
|
|
|
session := ctx.Get(r, "session").(*sessions.Session)
|
2014-01-06 06:09:41 +00:00
|
|
|
switch {
|
|
|
|
case r.Method == "GET":
|
2014-01-10 03:21:54 +00:00
|
|
|
getTemplate(w, "login").ExecuteTemplate(w, "base", params)
|
2014-01-06 06:09:41 +00:00
|
|
|
case r.Method == "POST":
|
|
|
|
//Attempt to login
|
2014-01-31 04:46:25 +00:00
|
|
|
err := r.ParseForm()
|
2014-01-31 22:25:02 +00:00
|
|
|
if checkError(err, w, "Error parsing request") {
|
|
|
|
return
|
|
|
|
}
|
2014-01-11 06:10:52 +00:00
|
|
|
succ, err := auth.Login(r)
|
2014-01-31 22:25:02 +00:00
|
|
|
if checkError(err, w, "Error logging in") {
|
|
|
|
return
|
|
|
|
}
|
2014-01-07 06:58:48 +00:00
|
|
|
//If we've logged in, save the session and redirect to the dashboard
|
|
|
|
if succ {
|
2014-01-06 06:09:41 +00:00
|
|
|
session.Save(r, w)
|
|
|
|
http.Redirect(w, r, "/", 302)
|
2014-01-07 06:58:48 +00:00
|
|
|
} else {
|
2014-01-10 03:21:54 +00:00
|
|
|
session.AddFlash(models.Flash{
|
|
|
|
Type: "danger",
|
|
|
|
Message: "Invalid Username/Password",
|
|
|
|
})
|
|
|
|
params.Flashes = session.Flashes()
|
|
|
|
getTemplate(w, "login").ExecuteTemplate(w, "base", params)
|
2014-01-06 06:09:41 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-03 04:56:55 +00:00
|
|
|
}
|
|
|
|
|
2014-01-10 03:21:54 +00:00
|
|
|
func getTemplate(w http.ResponseWriter, tmpl string) *template.Template {
|
2014-02-01 02:49:22 +00:00
|
|
|
templates := template.New("template")
|
|
|
|
templates.Delims(templateDelims[0], templateDelims[1])
|
|
|
|
_, err := templates.ParseFiles("templates/base.html", "templates/nav.html", "templates/"+tmpl+".html", "templates/flashes.html")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
return template.Must(templates, err)
|
2013-12-03 04:56:55 +00:00
|
|
|
}
|
2014-01-31 04:46:25 +00:00
|
|
|
|
2014-01-31 22:25:02 +00:00
|
|
|
func checkError(e error, w http.ResponseWriter, m string) bool {
|
2014-01-31 04:46:25 +00:00
|
|
|
if e != nil {
|
|
|
|
fmt.Println(e)
|
2014-01-31 22:25:02 +00:00
|
|
|
http.Error(w, "Error: "+m, http.StatusInternalServerError)
|
|
|
|
return true
|
2014-01-31 04:46:25 +00:00
|
|
|
}
|
2014-01-31 22:25:02 +00:00
|
|
|
return false
|
2014-01-31 04:46:25 +00:00
|
|
|
}
|