Working on setting up Login functionality

Introduced working with SQLite DB - working on setting up database on first run
Added "dbpath" to config
pull/24/head
Jordan 2014-01-06 00:09:41 -06:00
parent 5a5c9f600f
commit e6343292be
8 changed files with 100 additions and 22 deletions

1
.gitignore vendored
View File

@ -20,3 +20,4 @@ _cgo_export.*
_testmain.go _testmain.go
*.exe *.exe
*.db

14
api.go
View File

@ -2,24 +2,36 @@ package main
import ( import (
"fmt" "fmt"
"github.com/gorilla/mux"
"net/http" "net/http"
"github.com/gorilla/mux"
) )
func API(w http.ResponseWriter, r *http.Request) { func API(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello api") fmt.Fprintf(w, "Hello api")
} }
//API_Campaigns returns a list of campaigns if requested via GET.
//If requested via POST, API_Campaigns creates a new campaign and returns a reference to it.
func API_Campaigns(w http.ResponseWriter, r *http.Request) { func API_Campaigns(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "GET":
case r.Method == "POST":
fmt.Fprintf(w, "Hello POST!")
}
fmt.Fprintf(w, "Hello api") fmt.Fprintf(w, "Hello api")
} }
//API_Campaigns_Id returns details about the requested campaign. If the campaign is not
//valid, API_Campaigns_Id returns null.
func API_Campaigns_Id(w http.ResponseWriter, r *http.Request) { func API_Campaigns_Id(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
vars := mux.Vars(r) vars := mux.Vars(r)
fmt.Fprintf(w, "{\"method\" : \""+r.Method+"\", \"id\" : "+vars["id"]+"}") fmt.Fprintf(w, "{\"method\" : \""+r.Method+"\", \"id\" : "+vars["id"]+"}")
} }
//API_Doc renders a template describing the API documentation.
func API_Doc(w http.ResponseWriter, r *http.Request) { func API_Doc(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "api_doc") renderTemplate(w, "api_doc")
} }

View File

@ -4,5 +4,6 @@
"host" : "smtp.example.com:25", "host" : "smtp.example.com:25",
"user" : "username", "user" : "username",
"pass" : "password" "pass" : "password"
} },
"dbpath" : "db/gophish.db"
} }

View File

@ -26,7 +26,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
*/ */
import ( import (
"database/sql"
"encoding/gob"
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -34,15 +37,27 @@ import (
) )
var config Config var config Config
var db sql.DB
var setupFlag = flag.Bool("setup", false, "Starts the initial setup process for Gophish")
//init registers the necessary models to be saved in the session later
func init() {
gob.Register(&User{})
}
func main() { func main() {
// Get the config file // Get the config file
config_file, e := ioutil.ReadFile("./config.json") config_file, err := ioutil.ReadFile("./config.json")
if e != nil { defer db.Close()
fmt.Printf("File error: %v\n", e) if err != nil {
fmt.Printf("File error: %v\n", err)
os.Exit(1) os.Exit(1)
} }
json.Unmarshal(config_file, &config) json.Unmarshal(config_file, &config)
_, err = Setup()
if err != nil {
fmt.Println(err)
}
fmt.Printf("Gophish server started at http://%s\n", config.URL) fmt.Printf("Gophish server started at http://%s\n", config.URL)
http.Handle("/", createRouter()) http.Handle("/", createRouter())
http.ListenAndServe(config.URL, nil) http.ListenAndServe(config.URL, nil)

View File

@ -6,22 +6,14 @@ type SMTPServer struct {
Password string `json:"password"` Password string `json:"password"`
} }
type Email struct {
Subject string
Body string
To []string
Bcc []string
Cc []string
From string
}
type Config struct { type Config struct {
URL string `json:"url"` URL string `json:"url"`
SMTP SMTPServer `json:"smtp"` SMTP SMTPServer `json:"smtp"`
DBPath string `json:"dbpath"`
} }
type User struct { type User struct {
Id string Id int
Username string Username string
Hash string Hash string
APIKey string APIKey string

View File

@ -27,11 +27,12 @@ THE SOFTWARE.
*/ */
import ( import (
"html/template"
"net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/gorilla/securecookie" "github.com/gorilla/securecookie"
"github.com/gorilla/sessions" "github.com/gorilla/sessions"
"html/template"
"net/http"
) )
var store = sessions.NewCookieStore([]byte(securecookie.GenerateRandomKey(64))) var store = sessions.NewCookieStore([]byte(securecookie.GenerateRandomKey(64)))
@ -66,7 +67,7 @@ func Register(w http.ResponseWriter, r *http.Request) {
func Base(w http.ResponseWriter, r *http.Request) { func Base(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "gophish") session, _ := store.Get(r, "gophish")
// Example of saving session - will be removed. // Example of using session - will be removed.
session.Save(r, w) session.Save(r, w)
renderTemplate(w, "dashboard") renderTemplate(w, "dashboard")
} }
@ -85,10 +86,27 @@ func Base_Campaigns(w http.ResponseWriter, r *http.Request) {
} }
func Login(w http.ResponseWriter, r *http.Request) { func Login(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "login") switch {
case r.Method == "GET":
renderTemplate(w, "login")
case r.Method == "POST":
//Attempt to login
if login(r) {
session, _ := store.Get(r, "gophish")
session.Save(r, w)
http.Redirect(w, r, "/", 302)
}
}
} }
func renderTemplate(w http.ResponseWriter, tmpl string) { func renderTemplate(w http.ResponseWriter, tmpl string) {
t := template.Must(template.New("template").ParseFiles("templates/base.html", "templates/nav.html", "templates/"+tmpl+".html")) t := template.Must(template.New("template").ParseFiles("templates/base.html", "templates/nav.html", "templates/"+tmpl+".html"))
t.ExecuteTemplate(w, "base", "T") t.ExecuteTemplate(w, "base", "T")
} }
func login(r *http.Request) bool {
//session, _ := store.Get(r, "gophish")
//session.Values["user"] = User{1, "jordan", "hash", "key"}
//user := session.Values["user"].(*User)
return true
}

39
setup.go Normal file
View File

@ -0,0 +1,39 @@
package main
import (
"database/sql"
"fmt"
"os"
_ "github.com/mattn/go-sqlite3"
)
//Setup creates and returns the database needed by Gophish
func Setup() (*sql.DB, error) {
//If the file already exists, delete it and recreate it
if _, err := os.Stat(config.DBPath); err == nil {
os.Remove(config.DBPath)
}
fmt.Println("Creating db at " + config.DBPath)
db, err := sql.Open("sqlite3", config.DBPath)
if err != nil {
return nil, err
}
//Create the tables needed
_, err = db.Exec(
`CREATE TABLE Users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, hash VARCHAR(32), apikey VARCHAR(32));`)
if err != nil {
return nil, err
}
//Create the default user
stmt, err := db.Prepare(`INSERT INTO Users (username, hash, apikey) VALUES (?, ?, ?);`)
defer stmt.Close()
if err != nil {
return nil, err
}
_, err = stmt.Exec("jordan", "12345678901234567890123456789012", "12345678901234567890123456789012")
if err != nil {
return nil, err
}
return db, nil
}

View File

@ -1,6 +1,6 @@
{{define "content"}} {{define "content"}}
<div class="container"> <div class="container">
<form class="form-signin"> <form class="form-signin" action="/login" method="POST">
<img id="logo" src="/images/logo.png" /> <img id="logo" src="/images/logo.png" />
<h2 class="form-signin-heading">Please sign in</h2> <h2 class="form-signin-heading">Please sign in</h2>
<input type="text" class="form-control" placeholder="Username" required autofocus> <input type="text" class="form-control" placeholder="Username" required autofocus>