2017-04-15 17:23:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-04-16 23:33:51 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2017-04-15 17:23:40 +00:00
|
|
|
"fmt"
|
2017-04-16 23:33:51 +00:00
|
|
|
"github.com/Masterminds/sprig"
|
|
|
|
"github.com/jmoiron/sqlx"
|
2017-04-15 17:23:40 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
"html/template"
|
2017-04-16 23:33:51 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-04-15 17:23:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var logger *log.Logger
|
2017-04-17 14:24:37 +00:00
|
|
|
var config *Config
|
2017-04-15 17:23:40 +00:00
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
func getTemplateFilenames(tmpl []string) (result []string) {
|
|
|
|
result = make([]string, len(tmpl))
|
|
|
|
for i := range tmpl {
|
|
|
|
result[i] = fmt.Sprintf("templates/%s", tmpl[i])
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
2017-04-17 14:24:37 +00:00
|
|
|
return result
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
func renderTemplate(w http.ResponseWriter, tmpl []string, context interface{}) {
|
|
|
|
t := template.Must(template.New(tmpl[0]).Funcs(sprig.FuncMap()).ParseFiles(getTemplateFilenames(tmpl)...))
|
2017-04-16 23:33:51 +00:00
|
|
|
if err := t.Execute(w, context); err != nil {
|
2017-04-15 17:23:40 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-16 23:33:51 +00:00
|
|
|
func authenticateRequest(w http.ResponseWriter, r *http.Request, authRequired bool, handler func(http.ResponseWriter, *http.Request, *Voter)) {
|
|
|
|
for _, cert := range r.TLS.PeerCertificates {
|
2017-04-15 17:23:40 +00:00
|
|
|
for _, extKeyUsage := range cert.ExtKeyUsage {
|
|
|
|
if extKeyUsage == x509.ExtKeyUsageClientAuth {
|
2017-04-16 23:33:51 +00:00
|
|
|
for _, emailAddress := range cert.EmailAddresses {
|
2017-04-17 14:24:37 +00:00
|
|
|
voter, err := FindVoterByAddress(emailAddress)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if voter != nil {
|
2017-04-16 23:33:51 +00:00
|
|
|
handler(w, r, voter)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-16 23:33:51 +00:00
|
|
|
if authRequired {
|
2017-04-15 17:23:40 +00:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
2017-04-17 14:24:37 +00:00
|
|
|
renderTemplate(w, []string{"denied.html"}, nil)
|
2017-04-15 17:23:40 +00:00
|
|
|
return
|
|
|
|
}
|
2017-04-16 23:33:51 +00:00
|
|
|
handler(w, r, nil)
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-16 23:33:51 +00:00
|
|
|
type motionParameters struct {
|
|
|
|
ShowVotes bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type motionListParameters struct {
|
2017-04-17 14:24:37 +00:00
|
|
|
Page int64
|
2017-04-16 23:33:51 +00:00
|
|
|
Flags struct {
|
|
|
|
Confirmed, Withdraw, Unvoted bool
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
2017-04-15 17:23:40 +00:00
|
|
|
|
2017-04-16 23:33:51 +00:00
|
|
|
func parseMotionParameters(r *http.Request) motionParameters {
|
|
|
|
var m = motionParameters{}
|
|
|
|
m.ShowVotes, _ = strconv.ParseBool(r.URL.Query().Get("showvotes"))
|
|
|
|
logger.Printf("parsed parameters: %+v\n", m)
|
|
|
|
return m
|
|
|
|
}
|
2017-04-15 17:23:40 +00:00
|
|
|
|
2017-04-16 23:33:51 +00:00
|
|
|
func parseMotionListParameters(r *http.Request) motionListParameters {
|
|
|
|
var m = motionListParameters{}
|
|
|
|
if page, err := strconv.ParseInt(r.URL.Query().Get("page"), 10, 0); err != nil {
|
|
|
|
m.Page = 1
|
|
|
|
} else {
|
|
|
|
m.Page = page
|
|
|
|
}
|
|
|
|
m.Flags.Withdraw, _ = strconv.ParseBool(r.URL.Query().Get("withdraw"))
|
|
|
|
m.Flags.Unvoted, _ = strconv.ParseBool(r.URL.Query().Get("unvoted"))
|
|
|
|
|
|
|
|
if r.Method == http.MethodPost {
|
|
|
|
m.Flags.Confirmed, _ = strconv.ParseBool(r.PostFormValue("confirm"))
|
|
|
|
}
|
|
|
|
logger.Printf("parsed parameters: %+v\n", m)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func motionListHandler(w http.ResponseWriter, r *http.Request, voter *Voter) {
|
|
|
|
params := parseMotionListParameters(r)
|
|
|
|
|
|
|
|
var context struct {
|
2017-04-17 14:24:37 +00:00
|
|
|
Decisions []*DecisionForDisplay
|
2017-04-16 23:33:51 +00:00
|
|
|
Voter *Voter
|
|
|
|
Params *motionListParameters
|
|
|
|
PrevPage, NextPage int64
|
2017-04-17 14:24:37 +00:00
|
|
|
PageTitle string
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
2017-04-16 23:33:51 +00:00
|
|
|
context.Voter = voter
|
|
|
|
context.Params = ¶ms
|
2017-04-17 14:24:37 +00:00
|
|
|
var err error
|
2017-04-15 17:23:40 +00:00
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
if params.Flags.Unvoted {
|
|
|
|
if context.Decisions, err = FindVotersUnvotedDecisionsForDisplayOnPage(params.Page, voter); err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
2017-04-17 14:24:37 +00:00
|
|
|
} else {
|
|
|
|
if context.Decisions, err = FindDecisionsForDisplayOnPage(params.Page); err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-17 14:24:37 +00:00
|
|
|
|
|
|
|
if len(context.Decisions) > 0 {
|
|
|
|
olderExists, err := context.Decisions[len(context.Decisions)-1].OlderExists()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
2017-04-17 14:24:37 +00:00
|
|
|
if olderExists {
|
2017-04-16 23:33:51 +00:00
|
|
|
context.NextPage = params.Page + 1
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 14:24:37 +00:00
|
|
|
|
2017-04-16 23:33:51 +00:00
|
|
|
if params.Page > 1 {
|
|
|
|
context.PrevPage = params.Page - 1
|
|
|
|
}
|
2017-04-15 17:23:40 +00:00
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
renderTemplate(w, []string{"motions.html", "motion_fragments.html", "header.html", "footer.html"}, context)
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
func motionHandler(w http.ResponseWriter, r *http.Request, voter *Voter, decision *DecisionForDisplay) {
|
2017-04-16 23:33:51 +00:00
|
|
|
params := parseMotionParameters(r)
|
|
|
|
|
|
|
|
var context struct {
|
2017-04-17 14:24:37 +00:00
|
|
|
Decision *DecisionForDisplay
|
2017-04-16 23:33:51 +00:00
|
|
|
Voter *Voter
|
|
|
|
Params *motionParameters
|
|
|
|
PrevPage, NextPage int64
|
2017-04-17 14:24:37 +00:00
|
|
|
PageTitle string
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
|
|
|
context.Voter = voter
|
|
|
|
context.Params = ¶ms
|
2017-04-17 14:24:37 +00:00
|
|
|
if params.ShowVotes {
|
|
|
|
if err := decision.LoadVotes(); err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
context.Decision = decision
|
|
|
|
context.PageTitle = fmt.Sprintf("Motion %s: %s", decision.Tag, decision.Title)
|
|
|
|
renderTemplate(w, []string{"motion.html", "motion_fragments.html", "header.html", "footer.html"}, context)
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
func singleDecisionHandler(w http.ResponseWriter, r *http.Request, v *Voter, tag string, handler func(http.ResponseWriter, *http.Request, *Voter, *DecisionForDisplay)) {
|
|
|
|
decision, err := FindDecisionForDisplayByTag(tag)
|
2017-04-16 23:33:51 +00:00
|
|
|
if err != nil {
|
2017-04-17 14:24:37 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
2017-04-17 14:24:37 +00:00
|
|
|
if decision == nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
2017-04-17 14:24:37 +00:00
|
|
|
handler(w, r, v, decision)
|
|
|
|
}
|
2017-04-16 23:33:51 +00:00
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
type motionsHandler struct{}
|
|
|
|
|
|
|
|
type motionActionHandler interface {
|
|
|
|
Handle(w http.ResponseWriter, r *http.Request, voter *Voter, decision *DecisionForDisplay)
|
|
|
|
NeedsAuth() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type withDrawMotionAction struct{}
|
|
|
|
|
|
|
|
func (withDrawMotionAction) Handle(w http.ResponseWriter, r *http.Request, voter *Voter, decision *DecisionForDisplay) {
|
|
|
|
fmt.Fprintln(w, "Withdraw motion", decision.Tag)
|
|
|
|
// TODO: implement
|
|
|
|
if r.Method == http.MethodPost {
|
|
|
|
if confirm, err := strconv.ParseBool(r.PostFormValue("confirm")); err != nil {
|
|
|
|
log.Println("could not parse confirm parameter:", err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
} else if confirm {
|
|
|
|
WithdrawMotion(&decision.Decision, voter)
|
|
|
|
} else {
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-17 14:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (withDrawMotionAction) NeedsAuth() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type editMotionAction struct{}
|
|
|
|
|
|
|
|
func (editMotionAction) Handle(w http.ResponseWriter, r *http.Request, voter *Voter, decision *DecisionForDisplay) {
|
|
|
|
fmt.Fprintln(w, "Edit motion", decision.Tag)
|
|
|
|
// TODO: implement
|
|
|
|
}
|
2017-04-16 23:33:51 +00:00
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
func (editMotionAction) NeedsAuth() bool {
|
|
|
|
return true
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
func (h motionsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2017-04-16 23:33:51 +00:00
|
|
|
if err := db.Ping(); err != nil {
|
|
|
|
logger.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
subURL := r.URL.Path
|
|
|
|
|
|
|
|
var motionActionMap = map[string]motionActionHandler{
|
|
|
|
"withdraw": withDrawMotionAction{},
|
|
|
|
"edit": editMotionAction{},
|
|
|
|
}
|
2017-04-16 23:33:51 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case subURL == "":
|
|
|
|
authenticateRequest(w, r, false, motionListHandler)
|
|
|
|
return
|
|
|
|
case strings.Count(subURL, "/") == 1:
|
|
|
|
parts := strings.Split(subURL, "/")
|
|
|
|
logger.Printf("handle %v\n", parts)
|
|
|
|
motionTag := parts[0]
|
2017-04-17 14:24:37 +00:00
|
|
|
action, ok := motionActionMap[parts[1]]
|
|
|
|
if !ok {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
authenticateRequest(w, r, action.NeedsAuth(), func(w http.ResponseWriter, r *http.Request, v *Voter) {
|
|
|
|
singleDecisionHandler(w, r, v, motionTag, action.Handle)
|
|
|
|
})
|
|
|
|
|
2017-04-16 23:33:51 +00:00
|
|
|
logger.Printf("motion: %s, action: %s\n", motionTag, action)
|
|
|
|
return
|
|
|
|
case strings.Count(subURL, "/") == 0:
|
|
|
|
authenticateRequest(w, r, false, func(w http.ResponseWriter, r *http.Request, v *Voter) {
|
|
|
|
singleDecisionHandler(w, r, v, subURL, motionHandler)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(w, "No handler for '%s'", subURL)
|
|
|
|
return
|
|
|
|
}
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
func newMotionHandler(w http.ResponseWriter, _ *http.Request, _ *Voter) {
|
|
|
|
fmt.Fprintln(w,"New motion")
|
|
|
|
// TODO: implement
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
BoardMailAddress string `yaml:"board_mail_address"`
|
|
|
|
NoticeSenderAddress string `yaml:"notice_sender_address"`
|
|
|
|
DatabaseFile string `yaml:"database_file"`
|
|
|
|
ClientCACertificates string `yaml:"client_ca_certificates"`
|
|
|
|
ServerCert string `yaml:"server_certificate"`
|
|
|
|
ServerKey string `yaml:"server_key"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2017-04-16 23:33:51 +00:00
|
|
|
logger = log.New(os.Stderr, "boardvoting: ", log.LstdFlags|log.LUTC|log.Lshortfile)
|
2017-04-15 17:23:40 +00:00
|
|
|
|
|
|
|
var filename = "config.yaml"
|
|
|
|
if len(os.Args) == 2 {
|
|
|
|
filename = os.Args[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
var source []byte
|
|
|
|
|
|
|
|
source, err = ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(err)
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(source, &config)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(err)
|
|
|
|
}
|
|
|
|
logger.Printf("read configuration %v", config)
|
|
|
|
|
|
|
|
db, err = sqlx.Open("sqlite3", config.DatabaseFile)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(err)
|
|
|
|
}
|
2017-04-17 14:24:37 +00:00
|
|
|
defer db.Close()
|
2017-04-15 17:23:40 +00:00
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
http.Handle("/motions/", http.StripPrefix("/motions/", motionsHandler{}))
|
|
|
|
http.HandleFunc("/newmotion/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
authenticateRequest(w, r, true, newMotionHandler)
|
2017-04-15 17:23:40 +00:00
|
|
|
})
|
2017-04-16 23:33:51 +00:00
|
|
|
http.Handle("/static/", http.FileServer(http.Dir(".")))
|
2017-04-17 14:24:37 +00:00
|
|
|
http.Handle("/", http.RedirectHandler("/motions/", http.StatusMovedPermanently))
|
2017-04-15 17:23:40 +00:00
|
|
|
|
|
|
|
// load CA certificates for client authentication
|
|
|
|
caCert, err := ioutil.ReadFile(config.ClientCACertificates)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(err)
|
|
|
|
}
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
if !caCertPool.AppendCertsFromPEM(caCert) {
|
|
|
|
logger.Fatal("could not initialize client CA certificate pool")
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup HTTPS server
|
|
|
|
tlsConfig := &tls.Config{
|
2017-04-16 23:33:51 +00:00
|
|
|
ClientCAs: caCertPool,
|
|
|
|
ClientAuth: tls.RequireAndVerifyClientCert,
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
|
|
|
tlsConfig.BuildNameToCertificate()
|
|
|
|
|
|
|
|
server := &http.Server{
|
2017-04-16 23:33:51 +00:00
|
|
|
Addr: ":8443",
|
|
|
|
TLSConfig: tlsConfig,
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
logger.Printf("Launching application on https://localhost%s/\n", server.Addr)
|
|
|
|
|
|
|
|
if err = server.ListenAndServeTLS(config.ServerCert, config.ServerKey); err != nil {
|
2017-04-15 17:23:40 +00:00
|
|
|
logger.Fatal("ListenAndServerTLS: ", err)
|
|
|
|
}
|
|
|
|
}
|