2019-07-31 15:30:58 +00:00
|
|
|
/*
|
2022-03-21 12:39:00 +00:00
|
|
|
Copyright 2017-2022 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
2019-07-31 15:30:58 +00:00
|
|
|
|
2021-01-09 14:49:19 +00:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
2022-03-21 12:39:00 +00:00
|
|
|
you may not use this file except in compliance with the License.
|
2021-01-09 14:49:19 +00:00
|
|
|
You may obtain a copy of the License at
|
2019-07-31 15:30:58 +00:00
|
|
|
|
2021-01-09 14:49:19 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2019-07-31 15:30:58 +00:00
|
|
|
|
2021-01-09 14:49:19 +00:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2019-07-31 15:30:58 +00:00
|
|
|
*/
|
2021-01-09 14:49:19 +00:00
|
|
|
|
|
|
|
// The CAcert board voting software.
|
2017-04-15 17:23:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-04-21 00:25:49 +00:00
|
|
|
"bytes"
|
2017-04-17 20:56:20 +00:00
|
|
|
"context"
|
2017-04-16 23:33:51 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2018-03-29 18:08:41 +00:00
|
|
|
"database/sql"
|
2021-02-28 17:25:17 +00:00
|
|
|
"embed"
|
2017-04-18 00:34:21 +00:00
|
|
|
"encoding/base64"
|
2017-04-21 00:25:49 +00:00
|
|
|
"encoding/pem"
|
2022-03-21 11:18:55 +00:00
|
|
|
"errors"
|
2018-03-29 18:08:41 +00:00
|
|
|
"flag"
|
2017-04-15 17:23:40 +00:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2017-04-16 23:33:51 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2022-05-08 14:13:50 +00:00
|
|
|
"net/url"
|
2017-04-22 18:07:39 +00:00
|
|
|
"os"
|
2018-01-14 13:22:10 +00:00
|
|
|
"sort"
|
2017-04-16 23:33:51 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-04-19 19:35:08 +00:00
|
|
|
"time"
|
2018-03-31 08:50:06 +00:00
|
|
|
|
2021-02-28 17:25:17 +00:00
|
|
|
"github.com/Masterminds/sprig/v3"
|
2018-03-31 08:50:06 +00:00
|
|
|
"github.com/gorilla/csrf"
|
|
|
|
"github.com/gorilla/sessions"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2019-07-31 12:14:21 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-02-28 17:25:17 +00:00
|
|
|
"github.com/vearutop/statigz"
|
|
|
|
"github.com/vearutop/statigz/brotli"
|
2018-03-31 08:50:06 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2017-04-15 17:23:40 +00:00
|
|
|
)
|
|
|
|
|
2018-03-29 18:08:41 +00:00
|
|
|
var configFile string
|
2017-04-17 14:24:37 +00:00
|
|
|
var config *Config
|
2017-04-18 00:34:21 +00:00
|
|
|
var store *sessions.CookieStore
|
2018-03-31 08:50:06 +00:00
|
|
|
var csrfKey []byte
|
2017-04-18 14:07:54 +00:00
|
|
|
var version = "undefined"
|
2021-04-14 17:02:44 +00:00
|
|
|
var commit = "undefined"
|
|
|
|
var date = "undefined"
|
2017-04-15 17:23:40 +00:00
|
|
|
|
2021-01-09 14:49:19 +00:00
|
|
|
const (
|
|
|
|
cookieSecretMinLen = 32
|
|
|
|
csrfKeyLength = 32
|
|
|
|
httpIdleTimeout = 5
|
|
|
|
httpReadHeaderTimeout = 10
|
|
|
|
httpReadTimeout = 10
|
|
|
|
httpWriteTimeout = 60
|
|
|
|
sessionCookieName = "votesession"
|
|
|
|
)
|
2017-04-18 00:34:21 +00:00
|
|
|
|
2021-02-28 17:25:17 +00:00
|
|
|
//go:embed boardvoting/templates
|
|
|
|
var fsTemplates embed.FS
|
|
|
|
|
2018-03-31 08:50:06 +00:00
|
|
|
func renderTemplate(w http.ResponseWriter, r *http.Request, templates []string, context interface{}) {
|
2017-05-14 13:01:12 +00:00
|
|
|
funcMaps := sprig.FuncMap()
|
|
|
|
funcMaps["nl2br"] = func(text string) template.HTML {
|
2021-01-09 14:49:19 +00:00
|
|
|
// #nosec G203 input is sanitized
|
|
|
|
return template.HTML(strings.ReplaceAll(template.HTMLEscapeString(text), "\n", "<br>"))
|
2017-05-14 13:01:12 +00:00
|
|
|
}
|
2018-03-31 08:50:06 +00:00
|
|
|
funcMaps[csrf.TemplateTag] = func() template.HTML {
|
|
|
|
return csrf.TemplateField(r)
|
|
|
|
}
|
2018-03-29 19:26:12 +00:00
|
|
|
|
|
|
|
var baseTemplate *template.Template
|
|
|
|
|
|
|
|
for count, t := range templates {
|
2021-01-09 14:49:19 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
assetBytes []byte
|
|
|
|
)
|
|
|
|
|
2021-02-28 17:25:17 +00:00
|
|
|
if assetBytes, err = fsTemplates.ReadFile(fmt.Sprintf("boardvoting/templates/%s", t)); err != nil {
|
2018-03-29 19:26:12 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if count == 0 {
|
|
|
|
if baseTemplate, err = template.New(t).Funcs(funcMaps).Parse(string(assetBytes)); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2018-03-29 19:26:12 +00:00
|
|
|
} else {
|
2021-01-09 14:49:19 +00:00
|
|
|
if _, err := baseTemplate.New(t).Funcs(funcMaps).Parse(string(assetBytes)); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2018-03-29 19:26:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := baseTemplate.Execute(w, context); err != nil {
|
2017-04-15 17:23:40 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
type contextKey int
|
|
|
|
|
|
|
|
const (
|
2019-07-31 12:14:21 +00:00
|
|
|
ctxNeedsAuth contextKey = iota
|
2017-04-18 22:05:42 +00:00
|
|
|
ctxVoter
|
|
|
|
ctxDecision
|
2017-04-21 00:25:49 +00:00
|
|
|
ctxVote
|
2017-04-21 09:31:32 +00:00
|
|
|
ctxAuthenticatedCert
|
2017-04-17 20:56:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func authenticateRequest(w http.ResponseWriter, r *http.Request, handler func(http.ResponseWriter, *http.Request)) {
|
2018-01-14 13:22:10 +00:00
|
|
|
emailsTried := make(map[string]bool)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-16 23:33:51 +00:00
|
|
|
for _, cert := range r.TLS.PeerCertificates {
|
2017-04-15 17:23:40 +00:00
|
|
|
for _, extKeyUsage := range cert.ExtKeyUsage {
|
2021-01-09 14:49:19 +00:00
|
|
|
if extKeyUsage != x509.ExtKeyUsageClientAuth {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-28 17:25:17 +00:00
|
|
|
log.Infof(
|
|
|
|
"got a client certificate for the following email addresses: %s",
|
|
|
|
strings.Join(cert.EmailAddresses, ", "),
|
|
|
|
)
|
|
|
|
|
2021-01-09 14:49:19 +00:00
|
|
|
for _, emailAddress := range cert.EmailAddresses {
|
|
|
|
emailLower := strings.ToLower(emailAddress)
|
|
|
|
emailsTried[emailLower] = true
|
|
|
|
|
|
|
|
voter, err := FindVoterByAddress(emailLower)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if voter != nil {
|
|
|
|
requestContext := context.WithValue(r.Context(), ctxVoter, voter)
|
|
|
|
requestContext = context.WithValue(requestContext, ctxAuthenticatedCert, cert)
|
2021-02-28 17:25:17 +00:00
|
|
|
|
|
|
|
log.Infof("authenticated as %s", voter.Name)
|
|
|
|
|
2021-01-09 14:49:19 +00:00
|
|
|
handler(w, r.WithContext(requestContext))
|
|
|
|
|
|
|
|
return
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
needsAuth, ok := r.Context().Value(ctxNeedsAuth).(bool)
|
|
|
|
if ok && needsAuth {
|
2018-01-14 13:22:10 +00:00
|
|
|
var templateContext struct {
|
2018-03-29 18:08:41 +00:00
|
|
|
PageTitle string
|
|
|
|
Voter *Voter
|
|
|
|
Flashes interface{}
|
|
|
|
Emails []string
|
2018-01-14 13:22:10 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2018-01-14 13:22:10 +00:00
|
|
|
for k := range emailsTried {
|
|
|
|
templateContext.Emails = append(templateContext.Emails, k)
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2018-01-14 13:22:10 +00:00
|
|
|
sort.Strings(templateContext.Emails)
|
2017-04-15 17:23:40 +00:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
2018-03-31 08:50:06 +00:00
|
|
|
renderTemplate(w, r, []string{"denied.html", "header.html", "footer.html"}, templateContext)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-15 17:23:40 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
handler(w, r)
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-16 23:33:51 +00:00
|
|
|
type motionParameters struct {
|
|
|
|
ShowVotes bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type motionListParameters struct {
|
2022-03-21 11:16:24 +00:00
|
|
|
Page int
|
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"))
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-16 23:33:51 +00:00
|
|
|
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{}
|
2022-03-21 11:16:24 +00:00
|
|
|
if page, err := strconv.Atoi(r.URL.Query().Get("page")); err != nil {
|
2017-04-16 23:33:51 +00:00
|
|
|
m.Page = 1
|
|
|
|
} else {
|
|
|
|
m.Page = page
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-16 23:33:51 +00:00
|
|
|
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"))
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-16 23:33:51 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
func motionListHandler(w http.ResponseWriter, r *http.Request) {
|
2017-04-16 23:33:51 +00:00
|
|
|
params := parseMotionListParameters(r)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 00:34:21 +00:00
|
|
|
session, err := store.Get(r, sessionCookieName)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 00:34:21 +00:00
|
|
|
return
|
|
|
|
}
|
2017-04-16 23:33:51 +00:00
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
var templateContext struct {
|
2017-04-17 14:24:37 +00:00
|
|
|
Decisions []*DecisionForDisplay
|
2017-04-16 23:33:51 +00:00
|
|
|
Voter *Voter
|
|
|
|
Params *motionListParameters
|
2022-03-21 11:16:24 +00:00
|
|
|
PrevPage, NextPage int
|
2017-04-17 14:24:37 +00:00
|
|
|
PageTitle string
|
2017-04-18 00:34:21 +00:00
|
|
|
Flashes interface{}
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
if voter, ok := r.Context().Value(ctxVoter).(*Voter); ok {
|
|
|
|
templateContext.Voter = voter
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 00:34:21 +00:00
|
|
|
if flashes := session.Flashes(); len(flashes) > 0 {
|
|
|
|
templateContext.Flashes = flashes
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2019-07-31 12:14:21 +00:00
|
|
|
err = session.Save(r, w)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2019-07-31 12:14:21 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
templateContext.Params = ¶ms
|
2017-04-15 17:23:40 +00:00
|
|
|
|
2021-01-09 14:49:19 +00:00
|
|
|
if templateContext.Decisions, err = FindDecisionsForDisplayOnPage(
|
|
|
|
params.Page, params.Flags.Unvoted, templateContext.Voter,
|
|
|
|
); err != nil {
|
2017-04-18 18:30:08 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 18:30:08 +00:00
|
|
|
return
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
2017-04-17 14:24:37 +00:00
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
if len(templateContext.Decisions) > 0 {
|
2021-01-09 14:49:19 +00:00
|
|
|
olderExists, err := templateContext.Decisions[len(templateContext.Decisions)-1].OlderExists(
|
|
|
|
params.Flags.Unvoted, templateContext.Voter,
|
|
|
|
)
|
2017-04-17 14:24:37 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
return
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
if olderExists {
|
2017-04-17 20:56:20 +00:00
|
|
|
templateContext.NextPage = params.Page + 1
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-17 14:24:37 +00:00
|
|
|
|
2017-04-16 23:33:51 +00:00
|
|
|
if params.Page > 1 {
|
2017-04-17 20:56:20 +00:00
|
|
|
templateContext.PrevPage = params.Page - 1
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
2017-04-15 17:23:40 +00:00
|
|
|
|
2019-08-02 23:39:55 +00:00
|
|
|
renderTemplate(w, r, []string{
|
|
|
|
"motions.html", "motion_fragments.html", "page_fragments.html", "header.html", "footer.html",
|
|
|
|
}, templateContext)
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
func motionHandler(w http.ResponseWriter, r *http.Request) {
|
2017-04-16 23:33:51 +00:00
|
|
|
params := parseMotionParameters(r)
|
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
decision, ok := getDecisionFromRequest(r)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, http.StatusText(http.StatusPreconditionFailed), http.StatusPreconditionFailed)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var templateContext 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-18 00:34:21 +00:00
|
|
|
Flashes interface{}
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
voter, ok := getVoterFromRequest(r)
|
|
|
|
if ok {
|
|
|
|
templateContext.Voter = voter
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
templateContext.Params = ¶ms
|
2021-01-09 14:49:19 +00:00
|
|
|
|
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)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
templateContext.Decision = decision
|
|
|
|
templateContext.PageTitle = fmt.Sprintf("Motion %s: %s", decision.Tag, decision.Title)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
|
|
|
renderTemplate(w, r, []string{
|
|
|
|
"motion.html",
|
|
|
|
"motion_fragments.html",
|
|
|
|
"page_fragments.html",
|
|
|
|
"header.html",
|
|
|
|
"footer.html",
|
|
|
|
}, templateContext)
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-09 14:49:19 +00:00
|
|
|
func singleDecisionHandler(
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
tag string,
|
|
|
|
handler func(http.ResponseWriter, *http.Request),
|
|
|
|
) {
|
2017-04-17 14:24:37 +00:00
|
|
|
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)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
return
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
if decision == nil {
|
|
|
|
http.NotFound(w, r)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
return
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
handler(w, r.WithContext(context.WithValue(r.Context(), ctxDecision, decision)))
|
2017-04-17 14:24:37 +00:00
|
|
|
}
|
2017-04-16 23:33:51 +00:00
|
|
|
|
2017-04-17 14:24:37 +00:00
|
|
|
type motionActionHandler interface {
|
2017-04-17 20:56:20 +00:00
|
|
|
Handle(w http.ResponseWriter, r *http.Request)
|
2017-04-17 14:24:37 +00:00
|
|
|
NeedsAuth() bool
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
type authenticationRequiredHandler struct{}
|
2017-04-17 14:24:37 +00:00
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
func (authenticationRequiredHandler) NeedsAuth() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func getVoterFromRequest(r *http.Request) (voter *Voter, ok bool) {
|
|
|
|
voter, ok = r.Context().Value(ctxVoter).(*Voter)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDecisionFromRequest(r *http.Request) (decision *DecisionForDisplay, ok bool) {
|
|
|
|
decision, ok = r.Context().Value(ctxDecision).(*DecisionForDisplay)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
func getVoteFromRequest(r *http.Request) (vote VoteChoice, ok bool) {
|
|
|
|
vote, ok = r.Context().Value(ctxVote).(VoteChoice)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-19 19:35:08 +00:00
|
|
|
type FlashMessageAction struct{}
|
2017-04-17 20:56:20 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
func (a *FlashMessageAction) AddFlash(w http.ResponseWriter, r *http.Request, message interface{}, tags ...string) {
|
2017-04-19 19:35:08 +00:00
|
|
|
session, err := store.Get(r, sessionCookieName)
|
|
|
|
if err != nil {
|
2019-07-31 12:14:21 +00:00
|
|
|
log.Warnf("could not get session cookie: %v", err)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-19 19:35:08 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-19 19:35:08 +00:00
|
|
|
session.AddFlash(message, tags...)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2019-07-31 12:14:21 +00:00
|
|
|
err = session.Save(r, w)
|
2017-04-19 19:35:08 +00:00
|
|
|
if err != nil {
|
2019-07-31 12:14:21 +00:00
|
|
|
log.Warnf("could not save flash message: %v", err)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-19 19:35:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type withDrawMotionAction struct {
|
|
|
|
FlashMessageAction
|
|
|
|
authenticationRequiredHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *withDrawMotionAction) Handle(w http.ResponseWriter, r *http.Request) {
|
|
|
|
decision, ok := getDecisionFromRequest(r)
|
|
|
|
if !ok || decision.Status != voteStatusPending {
|
|
|
|
http.Error(w, http.StatusText(http.StatusPreconditionFailed), http.StatusPreconditionFailed)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-19 19:35:08 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-19 19:35:08 +00:00
|
|
|
voter, ok := getVoterFromRequest(r)
|
|
|
|
if !ok {
|
2017-04-17 20:56:20 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusPreconditionFailed), http.StatusPreconditionFailed)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
|
|
|
templates := []string{
|
|
|
|
"withdraw_motion_form.html",
|
|
|
|
"header.html",
|
|
|
|
"footer.html",
|
|
|
|
"motion_fragments.html",
|
|
|
|
"page_fragments.html",
|
|
|
|
}
|
|
|
|
|
2017-04-19 19:35:08 +00:00
|
|
|
var templateContext struct {
|
|
|
|
PageTitle string
|
|
|
|
Decision *DecisionForDisplay
|
|
|
|
Flashes interface{}
|
2018-03-31 08:50:06 +00:00
|
|
|
Voter *Voter
|
2017-04-19 19:35:08 +00:00
|
|
|
}
|
2017-04-17 20:56:20 +00:00
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodPost:
|
2017-04-19 19:35:08 +00:00
|
|
|
decision.Status = voteStatusWithdrawn
|
|
|
|
decision.Modified = time.Now().UTC()
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-19 21:32:12 +00:00
|
|
|
if err := decision.UpdateStatus(); err != nil {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Errorf("withdrawing motion failed: %v", err)
|
2017-04-19 19:35:08 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-19 19:35:08 +00:00
|
|
|
return
|
|
|
|
}
|
2017-04-19 21:32:12 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
NotifyMailChannel <- NewNotificationWithDrawMotion(&(decision.Decision), voter)
|
2017-04-19 21:32:12 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
a.AddFlash(w, r, fmt.Sprintf("Motion %s has been withdrawn!", decision.Tag))
|
2017-04-19 21:32:12 +00:00
|
|
|
|
2017-04-17 20:56:20 +00:00
|
|
|
http.Redirect(w, r, "/motions/", http.StatusTemporaryRedirect)
|
|
|
|
default:
|
2017-04-19 19:35:08 +00:00
|
|
|
templateContext.Decision = decision
|
2018-03-31 08:50:06 +00:00
|
|
|
templateContext.Voter = voter
|
|
|
|
renderTemplate(w, r, templates, templateContext)
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
2017-04-17 14:24:37 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 19:35:08 +00:00
|
|
|
type newMotionHandler struct {
|
|
|
|
FlashMessageAction
|
2017-04-17 14:24:37 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 19:35:08 +00:00
|
|
|
func (h *newMotionHandler) Handle(w http.ResponseWriter, r *http.Request) {
|
2017-04-18 22:05:42 +00:00
|
|
|
voter, ok := getVoterFromRequest(r)
|
|
|
|
if !ok {
|
2017-04-17 20:56:20 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusPreconditionFailed), http.StatusPreconditionFailed)
|
|
|
|
}
|
2017-04-16 23:33:51 +00:00
|
|
|
|
2019-08-02 23:39:55 +00:00
|
|
|
templates := []string{"create_motion_form.html", "page_fragments.html", "header.html", "footer.html"}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 22:05:42 +00:00
|
|
|
var templateContext struct {
|
|
|
|
Form NewDecisionForm
|
|
|
|
PageTitle string
|
|
|
|
Voter *Voter
|
|
|
|
Flashes interface{}
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 22:05:42 +00:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodPost:
|
|
|
|
form := NewDecisionForm{
|
|
|
|
Title: r.FormValue("Title"),
|
|
|
|
Content: r.FormValue("Content"),
|
|
|
|
VoteType: r.FormValue("VoteType"),
|
|
|
|
Due: r.FormValue("Due"),
|
|
|
|
}
|
2017-04-16 23:33:51 +00:00
|
|
|
|
2017-04-18 22:05:42 +00:00
|
|
|
if valid, data := form.Validate(); !valid {
|
|
|
|
templateContext.Voter = voter
|
|
|
|
templateContext.Form = form
|
2018-03-31 08:50:06 +00:00
|
|
|
renderTemplate(w, r, templates, templateContext)
|
2017-04-18 22:05:42 +00:00
|
|
|
} else {
|
2017-04-19 19:35:08 +00:00
|
|
|
data.Proposed = time.Now().UTC()
|
2021-01-09 14:49:19 +00:00
|
|
|
data.ProponentID = voter.ID
|
2017-04-19 21:32:12 +00:00
|
|
|
if err := data.Create(); err != nil {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Errorf("saving motion failed: %v", err)
|
|
|
|
http.Error(w, "Saving motion failed", http.StatusInternalServerError)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 22:05:42 +00:00
|
|
|
return
|
|
|
|
}
|
2017-04-19 21:32:12 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
NotifyMailChannel <- &NotificationCreateMotion{decision: *data, voter: *voter}
|
2017-04-19 21:32:12 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
h.AddFlash(w, r, "The motion has been proposed!")
|
2017-04-16 23:33:51 +00:00
|
|
|
|
2017-04-18 22:05:42 +00:00
|
|
|
http.Redirect(w, r, "/motions/", http.StatusMovedPermanently)
|
2017-04-17 14:24:37 +00:00
|
|
|
}
|
2017-04-18 22:05:42 +00:00
|
|
|
|
2017-04-16 23:33:51 +00:00
|
|
|
return
|
|
|
|
default:
|
2017-04-18 22:05:42 +00:00
|
|
|
templateContext.Voter = voter
|
|
|
|
templateContext.Form = NewDecisionForm{
|
2022-03-21 11:16:24 +00:00
|
|
|
VoteType: strconv.Itoa(voteTypeMotion),
|
2017-04-18 22:05:42 +00:00
|
|
|
}
|
2018-03-31 08:50:06 +00:00
|
|
|
renderTemplate(w, r, templates, templateContext)
|
2017-04-16 23:33:51 +00:00
|
|
|
}
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 19:35:08 +00:00
|
|
|
type editMotionAction struct {
|
|
|
|
FlashMessageAction
|
|
|
|
authenticationRequiredHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a editMotionAction) Handle(w http.ResponseWriter, r *http.Request) {
|
2017-04-18 22:05:42 +00:00
|
|
|
decision, ok := getDecisionFromRequest(r)
|
|
|
|
if !ok || decision.Status != voteStatusPending {
|
|
|
|
http.Error(w, http.StatusText(http.StatusPreconditionFailed), http.StatusPreconditionFailed)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 22:05:42 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 00:34:21 +00:00
|
|
|
voter, ok := getVoterFromRequest(r)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, http.StatusText(http.StatusPreconditionFailed), http.StatusPreconditionFailed)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-19 19:35:08 +00:00
|
|
|
return
|
2017-04-18 00:34:21 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2019-08-02 23:39:55 +00:00
|
|
|
templates := []string{"edit_motion_form.html", "page_fragments.html", "header.html", "footer.html"}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 00:34:21 +00:00
|
|
|
var templateContext struct {
|
2017-04-18 22:05:42 +00:00
|
|
|
Form EditDecisionForm
|
2017-04-18 00:34:21 +00:00
|
|
|
PageTitle string
|
|
|
|
Voter *Voter
|
|
|
|
Flashes interface{}
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 00:34:21 +00:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodPost:
|
2017-04-18 22:05:42 +00:00
|
|
|
form := EditDecisionForm{
|
2017-04-18 00:34:21 +00:00
|
|
|
Title: r.FormValue("Title"),
|
|
|
|
Content: r.FormValue("Content"),
|
|
|
|
VoteType: r.FormValue("VoteType"),
|
|
|
|
Due: r.FormValue("Due"),
|
2017-04-18 22:05:42 +00:00
|
|
|
Decision: &decision.Decision,
|
2017-04-18 00:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if valid, data := form.Validate(); !valid {
|
|
|
|
templateContext.Voter = voter
|
|
|
|
templateContext.Form = form
|
2018-03-31 08:50:06 +00:00
|
|
|
renderTemplate(w, r, templates, templateContext)
|
2017-04-18 00:34:21 +00:00
|
|
|
} else {
|
2017-04-19 19:35:08 +00:00
|
|
|
data.Modified = time.Now().UTC()
|
2017-04-19 21:32:12 +00:00
|
|
|
if err := data.Update(); err != nil {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Errorf("updating motion failed: %v", err)
|
|
|
|
http.Error(w, "Updating the motion failed.", http.StatusInternalServerError)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 00:34:21 +00:00
|
|
|
return
|
|
|
|
}
|
2017-04-19 21:32:12 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
NotifyMailChannel <- NewNotificationUpdateMotion(*data, *voter)
|
2017-04-19 21:32:12 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
a.AddFlash(w, r, "The motion has been modified!")
|
2017-04-18 00:34:21 +00:00
|
|
|
|
2017-04-18 18:30:08 +00:00
|
|
|
http.Redirect(w, r, "/motions/", http.StatusMovedPermanently)
|
2017-04-18 00:34:21 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 00:34:21 +00:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
templateContext.Voter = voter
|
2017-04-18 22:05:42 +00:00
|
|
|
templateContext.Form = EditDecisionForm{
|
|
|
|
Title: decision.Title,
|
|
|
|
Content: decision.Content,
|
|
|
|
VoteType: fmt.Sprintf("%d", decision.VoteType),
|
|
|
|
Decision: &decision.Decision,
|
2017-04-18 00:34:21 +00:00
|
|
|
}
|
2018-03-31 08:50:06 +00:00
|
|
|
renderTemplate(w, r, templates, templateContext)
|
2017-04-18 00:34:21 +00:00
|
|
|
}
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 22:05:42 +00:00
|
|
|
type motionsHandler struct{}
|
|
|
|
|
|
|
|
func (h motionsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
subURL := r.URL.Path
|
|
|
|
|
|
|
|
var motionActionMap = map[string]motionActionHandler{
|
2017-04-19 19:35:08 +00:00
|
|
|
"withdraw": &withDrawMotionAction{},
|
|
|
|
"edit": &editMotionAction{},
|
2017-04-18 22:05:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case subURL == "":
|
|
|
|
authenticateRequest(w, r.WithContext(context.WithValue(r.Context(), ctxNeedsAuth, false)), motionListHandler)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 22:05:42 +00:00
|
|
|
return
|
2017-04-19 19:35:08 +00:00
|
|
|
case subURL == "/newmotion/":
|
|
|
|
handler := &newMotionHandler{}
|
|
|
|
authenticateRequest(
|
|
|
|
w, r.WithContext(context.WithValue(r.Context(), ctxNeedsAuth, true)),
|
|
|
|
handler.Handle)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-19 19:35:08 +00:00
|
|
|
return
|
2017-04-18 22:05:42 +00:00
|
|
|
case strings.Count(subURL, "/") == 1:
|
|
|
|
parts := strings.Split(subURL, "/")
|
|
|
|
motionTag := parts[0]
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 22:05:42 +00:00
|
|
|
action, ok := motionActionMap[parts[1]]
|
|
|
|
if !ok {
|
|
|
|
http.NotFound(w, r)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 22:05:42 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 22:05:42 +00:00
|
|
|
authenticateRequest(
|
|
|
|
w, r.WithContext(context.WithValue(r.Context(), ctxNeedsAuth, action.NeedsAuth())),
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
singleDecisionHandler(w, r, motionTag, action.Handle)
|
|
|
|
})
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 22:05:42 +00:00
|
|
|
return
|
|
|
|
case strings.Count(subURL, "/") == 0:
|
|
|
|
authenticateRequest(w, r.WithContext(context.WithValue(r.Context(), ctxNeedsAuth, false)),
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
singleDecisionHandler(w, r, subURL, motionHandler)
|
|
|
|
})
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 22:05:42 +00:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
http.NotFound(w, r)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 22:05:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 09:31:32 +00:00
|
|
|
type directVoteHandler struct {
|
2017-04-21 00:25:49 +00:00
|
|
|
FlashMessageAction
|
|
|
|
authenticationRequiredHandler
|
|
|
|
}
|
|
|
|
|
2017-04-21 09:31:32 +00:00
|
|
|
func (h *directVoteHandler) Handle(w http.ResponseWriter, r *http.Request) {
|
2017-04-21 00:25:49 +00:00
|
|
|
decision, ok := getDecisionFromRequest(r)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
voter, ok := getVoterFromRequest(r)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
vote, ok := getVoteFromRequest(r)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 09:31:32 +00:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodPost:
|
2022-03-21 11:18:55 +00:00
|
|
|
clientCert, err := getPEMClientCert(r)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("could not get client certificate from request: %v", err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-21 09:31:32 +00:00
|
|
|
voteResult := &Vote{
|
2021-01-09 14:49:19 +00:00
|
|
|
VoterID: voter.ID, Vote: vote, DecisionID: decision.ID, Voted: time.Now().UTC(),
|
2022-03-21 11:18:55 +00:00
|
|
|
Notes: fmt.Sprintf("Direct Vote\n\n%s", clientCert)}
|
2017-04-21 09:31:32 +00:00
|
|
|
if err := voteResult.Save(); err != nil {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Errorf("Problem saving vote: %v", err)
|
|
|
|
http.Error(w, "Problem saving vote", http.StatusInternalServerError)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 09:31:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
NotifyMailChannel <- NewNotificationDirectVote(&decision.Decision, voter, voteResult)
|
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
h.AddFlash(w, r, "Your vote has been registered.")
|
2017-04-21 09:31:32 +00:00
|
|
|
|
|
|
|
http.Redirect(w, r, "/motions/", http.StatusMovedPermanently)
|
|
|
|
default:
|
2021-01-09 14:49:19 +00:00
|
|
|
templates := []string{
|
|
|
|
"direct_vote_form.html",
|
|
|
|
"header.html",
|
|
|
|
"footer.html",
|
|
|
|
"motion_fragments.html",
|
|
|
|
"page_fragments.html",
|
|
|
|
}
|
|
|
|
|
2017-04-21 09:31:32 +00:00
|
|
|
var templateContext struct {
|
|
|
|
Decision *DecisionForDisplay
|
|
|
|
VoteChoice VoteChoice
|
|
|
|
PageTitle string
|
|
|
|
Flashes interface{}
|
2018-03-31 08:50:06 +00:00
|
|
|
Voter *Voter
|
2017-04-21 09:31:32 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 09:31:32 +00:00
|
|
|
templateContext.Decision = decision
|
|
|
|
templateContext.VoteChoice = vote
|
2018-03-31 08:50:06 +00:00
|
|
|
templateContext.Voter = voter
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2018-03-31 08:50:06 +00:00
|
|
|
renderTemplate(w, r, templates, templateContext)
|
2017-04-21 09:31:32 +00:00
|
|
|
}
|
2017-04-21 00:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type proxyVoteHandler struct {
|
|
|
|
FlashMessageAction
|
|
|
|
authenticationRequiredHandler
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:18:55 +00:00
|
|
|
func getPEMClientCert(r *http.Request) (string, error) {
|
|
|
|
cert := r.Context().Value(ctxAuthenticatedCert)
|
|
|
|
|
|
|
|
authenticatedCertificate, ok := cert.(*x509.Certificate)
|
|
|
|
if !ok {
|
|
|
|
return "", errors.New("could not handle certificate as x509.Certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
clientCertPEM := bytes.NewBuffer(make([]byte, 0))
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2019-07-31 12:14:21 +00:00
|
|
|
err := pem.Encode(clientCertPEM, &pem.Block{Type: "CERTIFICATE", Bytes: authenticatedCertificate.Raw})
|
|
|
|
if err != nil {
|
2022-03-21 11:18:55 +00:00
|
|
|
return "", fmt.Errorf("error encoding client certificate: %w", err)
|
2019-07-31 12:14:21 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2022-03-21 11:18:55 +00:00
|
|
|
return clientCertPEM.String(), nil
|
2017-04-21 00:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *proxyVoteHandler) Handle(w http.ResponseWriter, r *http.Request) {
|
|
|
|
decision, ok := getDecisionFromRequest(r)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
proxy, ok := getVoterFromRequest(r)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
|
|
|
templates := []string{
|
|
|
|
"proxy_vote_form.html",
|
|
|
|
"header.html",
|
|
|
|
"footer.html",
|
|
|
|
"motion_fragments.html",
|
|
|
|
"page_fragments.html",
|
|
|
|
}
|
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
var templateContext struct {
|
|
|
|
Form ProxyVoteForm
|
|
|
|
Decision *DecisionForDisplay
|
|
|
|
Voters *[]Voter
|
|
|
|
PageTitle string
|
|
|
|
Flashes interface{}
|
2018-03-31 08:50:06 +00:00
|
|
|
Voter *Voter
|
2017-04-21 00:25:49 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2018-03-31 08:50:06 +00:00
|
|
|
templateContext.Voter = proxy
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodPost:
|
|
|
|
form := ProxyVoteForm{
|
|
|
|
Voter: r.FormValue("Voter"),
|
|
|
|
Vote: r.FormValue("Vote"),
|
|
|
|
Justification: r.FormValue("Justification"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if valid, voter, data, justification := form.Validate(); !valid {
|
|
|
|
templateContext.Form = form
|
|
|
|
templateContext.Decision = decision
|
2021-01-09 14:49:19 +00:00
|
|
|
|
|
|
|
voters, err := GetVotersForProxy(proxy)
|
|
|
|
if err != nil {
|
2017-04-21 00:25:49 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
|
|
|
templateContext.Voters = voters
|
|
|
|
|
2018-03-31 08:50:06 +00:00
|
|
|
renderTemplate(w, r, templates, templateContext)
|
2017-04-21 00:25:49 +00:00
|
|
|
} else {
|
2022-03-21 11:18:55 +00:00
|
|
|
clientCert, err := getPEMClientCert(r)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("could not get client certificate information: %v", err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-09 14:49:19 +00:00
|
|
|
data.DecisionID = decision.ID
|
2017-04-21 00:25:49 +00:00
|
|
|
data.Voted = time.Now().UTC()
|
2022-03-21 11:18:55 +00:00
|
|
|
data.Notes = fmt.Sprintf("Proxy-Vote by %s\n\n%s\n\n%s", proxy.Name, justification, clientCert)
|
2017-04-21 00:25:49 +00:00
|
|
|
|
|
|
|
if err := data.Save(); err != nil {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Errorf("Error saving vote: %s", err)
|
2017-04-21 00:25:49 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
NotifyMailChannel <- NewNotificationProxyVote(&decision.Decision, proxy, voter, data, justification)
|
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
h.AddFlash(w, r, "The vote has been registered.")
|
2017-04-21 00:25:49 +00:00
|
|
|
|
|
|
|
http.Redirect(w, r, "/motions/", http.StatusMovedPermanently)
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
templateContext.Form = ProxyVoteForm{}
|
|
|
|
templateContext.Decision = decision
|
2021-01-09 14:49:19 +00:00
|
|
|
|
|
|
|
voters, err := GetVotersForProxy(proxy)
|
|
|
|
if err != nil {
|
2017-04-21 00:25:49 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
|
|
|
templateContext.Voters = voters
|
|
|
|
|
2018-03-31 08:50:06 +00:00
|
|
|
renderTemplate(w, r, templates, templateContext)
|
2017-04-21 00:25:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type decisionVoteHandler struct{}
|
|
|
|
|
|
|
|
func (h *decisionVoteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(r.URL.Path, "/proxy/"):
|
|
|
|
motionTag := r.URL.Path[len("/proxy/"):]
|
|
|
|
handler := &proxyVoteHandler{}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
authenticateRequest(
|
|
|
|
w, r.WithContext(context.WithValue(r.Context(), ctxNeedsAuth, true)),
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
singleDecisionHandler(w, r, motionTag, handler.Handle)
|
|
|
|
})
|
|
|
|
case strings.HasPrefix(r.URL.Path, "/vote/"):
|
2021-01-09 14:49:19 +00:00
|
|
|
const expectedParts = 2
|
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
parts := strings.Split(r.URL.Path[len("/vote/"):], "/")
|
2021-01-09 14:49:19 +00:00
|
|
|
if len(parts) != expectedParts {
|
2017-04-21 09:31:32 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 09:31:32 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
motionTag := parts[0]
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
voteValue, ok := VoteValues[parts[1]]
|
|
|
|
if !ok {
|
2017-04-21 09:31:32 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 09:31:32 +00:00
|
|
|
handler := &directVoteHandler{}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
authenticateRequest(
|
|
|
|
w, r.WithContext(context.WithValue(r.Context(), ctxNeedsAuth, true)),
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
singleDecisionHandler(
|
|
|
|
w, r.WithContext(context.WithValue(r.Context(), ctxVote, voteValue)),
|
|
|
|
motionTag, handler.Handle)
|
|
|
|
})
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-21 00:25:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-15 17:23:40 +00:00
|
|
|
type Config struct {
|
2017-04-21 19:41:25 +00:00
|
|
|
NoticeMailAddress string `yaml:"notice_mail_address"`
|
|
|
|
VoteNoticeMailAddress string `yaml:"vote_notice_mail_address"`
|
|
|
|
NotificationSenderAddress string `yaml:"notification_sender_address"`
|
2017-04-21 00:25:49 +00:00
|
|
|
DatabaseFile string `yaml:"database_file"`
|
|
|
|
ClientCACertificates string `yaml:"client_ca_certificates"`
|
|
|
|
ServerCert string `yaml:"server_certificate"`
|
|
|
|
ServerKey string `yaml:"server_key"`
|
|
|
|
CookieSecret string `yaml:"cookie_secret"`
|
2018-03-31 08:50:06 +00:00
|
|
|
CsrfKey string `yaml:"csrf_key"`
|
2017-04-21 00:25:49 +00:00
|
|
|
BaseURL string `yaml:"base_url"`
|
2021-01-09 14:49:19 +00:00
|
|
|
HTTPAddress string `yaml:"http_address"`
|
|
|
|
HTTPSAddress string `yaml:"https_address"`
|
2019-07-31 12:14:21 +00:00
|
|
|
MailServer struct {
|
2017-04-18 22:05:42 +00:00
|
|
|
Host string `yaml:"host"`
|
2017-04-19 19:35:08 +00:00
|
|
|
Port int `yaml:"port"`
|
2017-04-18 22:05:42 +00:00
|
|
|
} `yaml:"mail_server"`
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
func readConfig() {
|
2018-03-29 18:08:41 +00:00
|
|
|
source, err := ioutil.ReadFile(configFile)
|
2017-04-15 17:23:40 +00:00
|
|
|
if err != nil {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Panicf("Opening configuration file failed: %v", err)
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 00:34:21 +00:00
|
|
|
if err := yaml.Unmarshal(source, &config); err != nil {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Panicf("Loading configuration failed: %v", err)
|
2017-04-18 00:34:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-09 14:49:19 +00:00
|
|
|
if config.HTTPSAddress == "" {
|
|
|
|
config.HTTPSAddress = "127.0.0.1:8443"
|
2018-03-29 18:08:41 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
|
|
|
if config.HTTPAddress == "" {
|
|
|
|
config.HTTPAddress = "127.0.0.1:8080"
|
2018-03-29 18:08:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 00:34:21 +00:00
|
|
|
cookieSecret, err := base64.StdEncoding.DecodeString(config.CookieSecret)
|
2017-04-15 17:23:40 +00:00
|
|
|
if err != nil {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Panicf("Decoding cookie secret failed: %v", err)
|
2017-04-21 22:06:16 +00:00
|
|
|
panic(err)
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
|
|
|
if len(cookieSecret) < cookieSecretMinLen {
|
|
|
|
log.Panicf("Cookie secret is less than %d bytes long", cookieSecretMinLen)
|
2017-04-18 00:34:21 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2018-03-31 08:50:06 +00:00
|
|
|
csrfKey, err = base64.StdEncoding.DecodeString(config.CsrfKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("Decoding csrf key failed: %v", err)
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
|
|
|
if len(csrfKey) != csrfKeyLength {
|
|
|
|
log.Panicf(
|
|
|
|
"CSRF key must be exactly %d bytes long but is %d bytes long",
|
|
|
|
csrfKeyLength,
|
|
|
|
len(csrfKey),
|
|
|
|
)
|
2018-03-31 08:50:06 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-18 00:34:21 +00:00
|
|
|
store = sessions.NewCookieStore(cookieSecret)
|
2021-02-28 17:25:17 +00:00
|
|
|
store.Options.Secure = true
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Info("Read configuration")
|
|
|
|
}
|
2017-04-15 17:23:40 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
func setupDbConfig(ctx context.Context) {
|
2018-03-29 19:26:12 +00:00
|
|
|
database, err := sql.Open("sqlite3", config.DatabaseFile)
|
2017-04-15 17:23:40 +00:00
|
|
|
if err != nil {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Panicf("Opening database failed: %v", err)
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
db = NewDB(database)
|
2017-04-18 18:30:08 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
go func() {
|
|
|
|
for range ctx.Done() {
|
|
|
|
if err := db.Close(); err != nil {
|
2019-07-31 12:14:21 +00:00
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "Problem closing the database: %v", err)
|
2017-04-22 18:07:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2017-04-18 18:30:08 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Infof("opened database connection")
|
|
|
|
}
|
2017-04-15 17:23:40 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
func setupNotifications(ctx context.Context) {
|
2017-04-21 00:25:49 +00:00
|
|
|
quitMailChannel := make(chan int)
|
|
|
|
go MailNotifier(quitMailChannel)
|
2017-04-19 21:32:12 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
go func() {
|
|
|
|
for range ctx.Done() {
|
|
|
|
quitMailChannel <- 1
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupJobs(ctx context.Context) {
|
2017-04-20 09:35:33 +00:00
|
|
|
quitChannel := make(chan int)
|
|
|
|
go JobScheduler(quitChannel)
|
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
go func() {
|
|
|
|
for range ctx.Done() {
|
|
|
|
quitChannel <- 1
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-05-08 14:13:50 +00:00
|
|
|
//go:embed ui/static
|
|
|
|
var uiStatic embed.FS
|
2021-02-28 17:25:17 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
func setupHandlers() {
|
2017-04-17 14:24:37 +00:00
|
|
|
http.Handle("/motions/", http.StripPrefix("/motions/", motionsHandler{}))
|
2017-04-19 19:35:08 +00:00
|
|
|
http.Handle("/newmotion/", motionsHandler{})
|
2017-04-21 00:25:49 +00:00
|
|
|
http.Handle("/proxy/", &decisionVoteHandler{})
|
|
|
|
http.Handle("/vote/", &decisionVoteHandler{})
|
2022-05-08 14:13:50 +00:00
|
|
|
http.Handle("/static/", addPrefix("/ui", statigz.FileServer(uiStatic, brotli.AddEncoding, statigz.EncodeOnInit)))
|
2017-04-17 14:24:37 +00:00
|
|
|
http.Handle("/", http.RedirectHandler("/motions/", http.StatusMovedPermanently))
|
2017-04-22 18:07:39 +00:00
|
|
|
}
|
2017-04-15 17:23:40 +00:00
|
|
|
|
2022-05-08 14:13:50 +00:00
|
|
|
func addPrefix(prefix string, h http.Handler) http.Handler {
|
|
|
|
if prefix == "" {
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
newPath := prefix + r.URL.Path
|
|
|
|
newRawPath := prefix + r.URL.RawPath
|
|
|
|
|
|
|
|
r2 := new(http.Request)
|
|
|
|
*r2 = *r
|
|
|
|
r2.URL = new(url.URL)
|
|
|
|
*r2.URL = *r.URL
|
|
|
|
r2.URL.Path = newPath
|
|
|
|
r2.URL.RawPath = newRawPath
|
|
|
|
|
|
|
|
h.ServeHTTP(w, r2)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
func setupTLSConfig() (tlsConfig *tls.Config) {
|
2017-04-15 17:23:40 +00:00
|
|
|
// load CA certificates for client authentication
|
|
|
|
caCert, err := ioutil.ReadFile(config.ClientCACertificates)
|
|
|
|
if err != nil {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Panicf("Error reading client certificate CAs %v", err)
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-15 17:23:40 +00:00
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
if !caCertPool.AppendCertsFromPEM(caCert) {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Panic("could not initialize client CA certificate pool")
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// setup HTTPS server
|
2017-04-22 18:07:39 +00:00
|
|
|
tlsConfig = &tls.Config{
|
2021-01-09 14:49:19 +00:00
|
|
|
MinVersion: tls.VersionTLS12,
|
2017-04-16 23:33:51 +00:00
|
|
|
ClientCAs: caCertPool,
|
2017-04-17 20:56:20 +00:00
|
|
|
ClientAuth: tls.VerifyClientCertIfGiven,
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2019-07-31 15:45:05 +00:00
|
|
|
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
|
2021-04-14 17:02:44 +00:00
|
|
|
log.Infof("CAcert Board Voting version %s, commit %s built at %s", version, commit, date)
|
2019-07-31 12:14:21 +00:00
|
|
|
|
2019-07-31 15:45:05 +00:00
|
|
|
flag.StringVar(
|
|
|
|
&configFile, "config", "config.yaml", "Configuration file name")
|
|
|
|
|
2018-03-29 18:08:41 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
var stopAll func()
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
executionContext, stopAll := context.WithCancel(context.Background())
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
readConfig()
|
|
|
|
setupDbConfig(executionContext)
|
|
|
|
setupNotifications(executionContext)
|
|
|
|
setupJobs(executionContext)
|
|
|
|
setupHandlers()
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
tlsConfig := setupTLSConfig()
|
|
|
|
|
|
|
|
defer stopAll()
|
|
|
|
|
2017-04-15 17:23:40 +00:00
|
|
|
server := &http.Server{
|
2021-01-09 14:49:19 +00:00
|
|
|
Addr: config.HTTPSAddress,
|
2020-04-26 11:18:58 +00:00
|
|
|
TLSConfig: tlsConfig,
|
2021-01-09 14:49:19 +00:00
|
|
|
IdleTimeout: time.Second * httpIdleTimeout,
|
|
|
|
ReadHeaderTimeout: time.Second * httpReadHeaderTimeout,
|
|
|
|
ReadTimeout: time.Second * httpReadTimeout,
|
|
|
|
WriteTimeout: time.Second * httpWriteTimeout,
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
|
|
|
|
2018-03-31 08:50:06 +00:00
|
|
|
server.Handler = csrf.Protect(csrfKey)(http.DefaultServeMux)
|
|
|
|
|
2018-03-29 18:08:41 +00:00
|
|
|
log.Infof("Launching application on https://%s/", server.Addr)
|
2017-04-17 14:24:37 +00:00
|
|
|
|
2017-04-19 19:35:08 +00:00
|
|
|
errs := make(chan error, 1)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-19 19:35:08 +00:00
|
|
|
go func() {
|
2020-04-26 11:18:58 +00:00
|
|
|
httpRedirector := &http.Server{
|
2021-01-09 14:49:19 +00:00
|
|
|
Addr: config.HTTPAddress,
|
2020-04-26 11:18:58 +00:00
|
|
|
Handler: http.RedirectHandler(config.BaseURL, http.StatusMovedPermanently),
|
2021-01-09 14:49:19 +00:00
|
|
|
IdleTimeout: time.Second * httpIdleTimeout,
|
|
|
|
ReadHeaderTimeout: time.Second * httpReadHeaderTimeout,
|
|
|
|
ReadTimeout: time.Second * httpReadTimeout,
|
|
|
|
WriteTimeout: time.Second * httpWriteTimeout,
|
2020-04-26 11:18:58 +00:00
|
|
|
}
|
|
|
|
if err := httpRedirector.ListenAndServe(); err != nil {
|
2017-04-19 19:35:08 +00:00
|
|
|
errs <- err
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-19 19:35:08 +00:00
|
|
|
close(errs)
|
|
|
|
}()
|
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
if err := server.ListenAndServeTLS(config.ServerCert, config.ServerKey); err != nil {
|
|
|
|
log.Panicf("ListenAndServerTLS failed: %v", err)
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-19 19:35:08 +00:00
|
|
|
if err := <-errs; err != nil {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Panicf("ListenAndServe failed: %v", err)
|
2017-04-19 19:35:08 +00:00
|
|
|
}
|
2017-04-15 17:23:40 +00:00
|
|
|
}
|