guildgate/main.go

127 lines
3.0 KiB
Go
Raw Normal View History

2020-08-20 12:51:02 -04:00
package main
import (
"log"
"net/http"
"text/template"
"github.com/gorilla/securecookie"
2020-08-20 12:51:02 -04:00
)
2020-08-20 13:24:52 -04:00
var Conf *Config
var tpl *template.Template
var cookieHandler = securecookie.New(
securecookie.GenerateRandomKey(64),
securecookie.GenerateRandomKey(32))
2020-08-20 12:51:02 -04:00
func signupPage(res http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
log.Println("GET /register")
u := getUserName(req)
if u != "" {
http.Redirect(res, req, "/", 302)
} else {
tpl.ExecuteTemplate(res, "register", nil)
}
2020-08-20 12:51:02 -04:00
return
}
username := req.FormValue("username")
password := req.FormValue("password")
email := req.FormValue("email")
secret := req.FormValue("secret")
2020-08-20 13:24:52 -04:00
if Conf.Secret != "" && Conf.Secret != secret {
log.Printf("Bad secret entered\n")
2020-08-20 12:51:02 -04:00
res.Write([]byte("Get a load of this guy, not knowing the secret code"))
return
}
//insert into LDAP
2020-08-20 13:24:52 -04:00
log.Printf("Attempting to create account for %v", username)
2020-08-20 12:51:02 -04:00
err := createLDAPAccount(username, password, email)
if err == nil {
res.Write([]byte("User created!"))
return
} else {
res.Write([]byte("Failure to create account"))
return
}
}
func loginPage(res http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
log.Println("GET /login")
u := getUserName(req)
if u != "" {
http.Redirect(res, req, "/", 302)
} else {
tpl.ExecuteTemplate(res, "login", nil)
}
return
}
username := req.FormValue("username")
password := req.FormValue("password")
log.Printf("Attempting login for user %v\n", username)
err := loginLDAPAccount(username, password)
if err != nil {
log.Printf("Error logging in user %v: %v\n", username, err)
res.Write([]byte("Error logging in. Incorrect password?"))
return
} else {
setSession(username, res)
http.Redirect(res, req, "/", 302)
return
}
}
func logoutPage(res http.ResponseWriter, req *http.Request) {
clearSession(res)
http.Redirect(res, req, "/", 302)
}
2020-08-20 12:51:02 -04:00
func homePage(res http.ResponseWriter, req *http.Request) {
u := getUserName(req)
uname := "Unregistered"
if u != "" {
uname = u
}
data := struct {
Title string
Username string
}{
"Index",
uname,
}
tpl.ExecuteTemplate(res, "index", data)
2020-08-20 12:51:02 -04:00
}
func main() {
2020-08-20 13:24:52 -04:00
Conf, _ = LoadConfig()
2020-08-20 12:51:02 -04:00
log.Println("Loaded config")
http.HandleFunc("/register", signupPage)
http.HandleFunc("/login", loginPage)
http.HandleFunc("/logout", logoutPage)
2020-08-20 12:51:02 -04:00
http.HandleFunc("/", homePage)
log.Printf("Registering templates from %v/\n", Conf.TplPath)
tpl = template.Must(template.ParseGlob(Conf.TplPath + "/*.html"))
2020-08-20 13:24:52 -04:00
log.Printf("Guildgate starting on %v\n", Conf.Port)
2020-08-27 12:45:00 -04:00
var err error
2020-08-23 17:53:48 -04:00
if Conf.Tls {
2020-08-27 12:45:00 -04:00
log.Printf("Starting TLS\n")
if Conf.Cert == "" {
log.Fatalf("Need to specify a certificate if using TLS!\n")
} else if Conf.Key == "" {
log.Fatalf("Need to specify a private key is usingTLS!\n")
} else {
err = http.ListenAndServeTLS(":"+Conf.Port, Conf.Cert, Conf.Key, nil)
}
2020-08-23 17:53:48 -04:00
} else {
2020-08-27 12:45:00 -04:00
log.Printf("Starting unencrypted\n")
err = http.ListenAndServe(":"+Conf.Port, nil)
}
if err != nil {
log.Printf("HTTP server failed with %v\n", err)
2020-08-23 17:53:48 -04:00
}
2020-08-20 12:51:02 -04:00
}