2014-01-09 06:42:05 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2014-01-09 23:18:49 +00:00
|
|
|
|
|
|
|
ctx "github.com/gorilla/context"
|
2014-01-10 03:21:54 +00:00
|
|
|
"github.com/jordan-wright/gophish/auth"
|
2014-02-04 21:23:09 +00:00
|
|
|
"github.com/jordan-wright/gophish/db"
|
2014-01-09 06:42:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetContext wraps each request in a function which fills in the context for a given request.
|
|
|
|
// This includes setting the User and Session keys and values as necessary for use in later functions.
|
2014-01-11 04:37:42 +00:00
|
|
|
func GetContext(handler http.Handler) http.HandlerFunc {
|
2014-01-09 06:42:05 +00:00
|
|
|
// Set the context here
|
2014-01-11 04:37:42 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2014-02-04 21:23:09 +00:00
|
|
|
// Parse the request form
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Error parsing request", http.StatusInternalServerError)
|
|
|
|
}
|
2014-01-09 23:18:49 +00:00
|
|
|
// Set the context appropriately here.
|
2014-01-10 03:21:54 +00:00
|
|
|
// Set the session
|
|
|
|
session, _ := auth.Store.Get(r, "gophish")
|
2014-01-10 04:21:12 +00:00
|
|
|
// Put the session in the context so that
|
2014-01-10 03:21:54 +00:00
|
|
|
ctx.Set(r, "session", session)
|
2014-01-10 04:21:12 +00:00
|
|
|
if id, ok := session.Values["id"]; ok {
|
2014-02-05 16:57:53 +00:00
|
|
|
u, err := db.GetUser(id.(int64))
|
2014-01-10 04:21:12 +00:00
|
|
|
if err != nil {
|
|
|
|
ctx.Set(r, "user", nil)
|
|
|
|
}
|
|
|
|
ctx.Set(r, "user", u)
|
|
|
|
} else {
|
|
|
|
ctx.Set(r, "user", nil)
|
|
|
|
}
|
2014-01-09 06:42:05 +00:00
|
|
|
handler.ServeHTTP(w, r)
|
2014-01-10 03:21:54 +00:00
|
|
|
// Remove context contents
|
2014-01-09 23:18:49 +00:00
|
|
|
ctx.Clear(r)
|
2014-01-11 04:37:42 +00:00
|
|
|
}
|
2014-01-09 06:42:05 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 04:46:25 +00:00
|
|
|
func RequireAPIKey(handler http.Handler) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.ParseForm()
|
|
|
|
ak := r.Form.Get("api_key")
|
|
|
|
if ak == "" {
|
|
|
|
JSONError(w, 500, "API Key not set")
|
|
|
|
} else {
|
2014-02-04 21:23:09 +00:00
|
|
|
id, err := db.Conn.SelectInt("SELECT id FROM users WHERE api_key=?", ak)
|
|
|
|
if id == 0 || err != nil {
|
2014-02-05 16:57:53 +00:00
|
|
|
JSONError(w, 500, "Invalid API Key")
|
2014-02-04 21:23:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Set(r, "user_id", id)
|
2014-01-31 04:46:25 +00:00
|
|
|
ctx.Set(r, "api_key", ak)
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 06:42:05 +00:00
|
|
|
// RequireLogin is a simple middleware which checks to see if the user is currently logged in.
|
|
|
|
// If not, the function returns a 302 redirect to the login page.
|
2014-01-11 04:37:42 +00:00
|
|
|
func RequireLogin(handler http.Handler) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2014-01-10 04:21:12 +00:00
|
|
|
if u := ctx.Get(r, "user"); u != nil {
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
} else {
|
|
|
|
http.Redirect(w, r, "/login", 302)
|
|
|
|
}
|
2014-01-11 04:37:42 +00:00
|
|
|
}
|
2014-01-09 06:42:05 +00:00
|
|
|
}
|
2014-01-31 04:46:25 +00:00
|
|
|
|
|
|
|
func JSONError(w http.ResponseWriter, c int, m string) {
|
|
|
|
http.Error(w, m, c)
|
|
|
|
}
|