2022-05-22 09:02:37 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
package handlers
|
2022-05-22 09:02:37 +00:00
|
|
|
|
2022-05-22 10:19:25 +00:00
|
|
|
import (
|
2022-10-15 17:58:58 +00:00
|
|
|
"bytes"
|
2022-05-26 13:27:25 +00:00
|
|
|
"context"
|
2022-05-27 15:39:54 +00:00
|
|
|
"crypto/x509"
|
2022-10-15 17:58:58 +00:00
|
|
|
"encoding/pem"
|
2022-05-26 13:27:25 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-10-15 17:58:58 +00:00
|
|
|
"log"
|
2022-05-22 10:19:25 +00:00
|
|
|
"net/http"
|
2022-05-26 13:27:25 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-05-26 15:25:25 +00:00
|
|
|
"github.com/justinas/nosurf"
|
|
|
|
|
2022-05-26 13:27:25 +00:00
|
|
|
"git.cacert.org/cacert-boardvoting/internal/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
type contextKey int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ctxUser contextKey = iota
|
2022-05-27 15:39:54 +00:00
|
|
|
ctxAuthenticatedCert
|
2022-05-22 10:19:25 +00:00
|
|
|
)
|
2022-05-22 09:02:37 +00:00
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
func SecureHeaders(next http.Handler) http.Handler {
|
2022-05-22 09:02:37 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Security-Policy", "default-src 'self'; font-src 'self' data:")
|
|
|
|
w.Header().Set("Referrer-Policy", "origin-when-cross-origin")
|
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
|
|
w.Header().Set("X-Frame-Options", "deny")
|
|
|
|
w.Header().Set("X-XSS-Protection", "0")
|
|
|
|
w.Header().Set("Strict-Transport-Security", "max-age=63072000")
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
2022-05-22 10:06:23 +00:00
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
type UserMiddleware struct {
|
|
|
|
users *models.UserModel
|
|
|
|
errorLog *log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserMiddleware(users *models.UserModel, errorLog *log.Logger) *UserMiddleware {
|
|
|
|
return &UserMiddleware{users: users, errorLog: errorLog}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *UserMiddleware) AuthenticateRequest(
|
2022-09-26 09:58:36 +00:00
|
|
|
ctx context.Context,
|
|
|
|
r *http.Request,
|
|
|
|
) (*models.User, *x509.Certificate, error) {
|
2022-05-26 13:27:25 +00:00
|
|
|
if r.TLS == nil {
|
2022-05-27 15:39:54 +00:00
|
|
|
return nil, nil, nil
|
2022-05-26 13:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.TLS.PeerCertificates) < 1 {
|
2022-05-27 15:39:54 +00:00
|
|
|
return nil, nil, nil
|
2022-05-26 13:27:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
clientCert := r.TLS.PeerCertificates[0]
|
2022-05-29 13:36:27 +00:00
|
|
|
|
|
|
|
allowClientAuth := false
|
2022-05-29 13:43:45 +00:00
|
|
|
|
2022-05-29 13:36:27 +00:00
|
|
|
for _, eku := range clientCert.ExtKeyUsage {
|
|
|
|
if eku == x509.ExtKeyUsageClientAuth {
|
|
|
|
allowClientAuth = true
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allowClientAuth {
|
|
|
|
// presented certificate is not valid for client authentication
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
emails := clientCert.EmailAddresses
|
2022-05-26 14:47:57 +00:00
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
user, err := m.users.ByEmails(ctx, emails)
|
2022-05-26 13:27:25 +00:00
|
|
|
if err != nil {
|
2022-05-27 15:39:54 +00:00
|
|
|
return nil, nil, fmt.Errorf("could not get user information from database: %w", err)
|
2022-05-26 13:27:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
return user, clientCert, nil
|
2022-05-26 13:27:25 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
func (m *UserMiddleware) TryAuthenticate(next http.Handler) http.Handler {
|
2022-05-26 13:27:25 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2022-10-15 17:58:58 +00:00
|
|
|
user, cert, err := m.AuthenticateRequest(r.Context(), r)
|
2022-05-26 13:27:25 +00:00
|
|
|
if err != nil {
|
2022-06-04 17:00:57 +00:00
|
|
|
panic(err)
|
2022-05-26 13:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:05:47 +00:00
|
|
|
w.Header().Add("Cache-Control", "no-store")
|
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
certContext := context.WithValue(r.Context(), ctxAuthenticatedCert, cert)
|
|
|
|
userContext := context.WithValue(certContext, ctxUser, user)
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r.WithContext(userContext))
|
2022-05-26 13:27:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
func (m *UserMiddleware) HasRole(r *http.Request, roles ...models.RoleName) (bool, bool, error) {
|
|
|
|
user, err := getUser(r)
|
2022-05-26 13:27:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
|
|
|
return false, false, nil
|
|
|
|
}
|
|
|
|
|
2022-05-29 13:36:27 +00:00
|
|
|
roleMatched, err := user.HasRole(roles...)
|
2022-05-26 13:27:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, true, fmt.Errorf("could not determin user role assignment: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !roleMatched {
|
2022-05-29 13:36:27 +00:00
|
|
|
roleNames := make([]string, len(roles))
|
|
|
|
for idx := range roles {
|
|
|
|
roleNames[idx] = string(roles[idx])
|
|
|
|
}
|
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
m.errorLog.Printf(
|
2022-05-26 13:27:25 +00:00
|
|
|
"user %s does not have any of the required role(s) %s assigned",
|
|
|
|
user.Name,
|
2022-05-29 13:36:27 +00:00
|
|
|
strings.Join(roleNames, ", "),
|
2022-05-26 13:27:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return false, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, true, nil
|
|
|
|
}
|
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
func (m *UserMiddleware) requireRole(next http.Handler, roles ...models.RoleName) http.Handler {
|
2022-05-26 13:27:25 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2022-10-15 17:58:58 +00:00
|
|
|
hasRole, hasUser, err := m.HasRole(r, roles...)
|
2022-05-26 13:27:25 +00:00
|
|
|
if err != nil {
|
2022-06-04 17:00:57 +00:00
|
|
|
panic(err)
|
2022-05-26 13:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !hasUser {
|
2022-10-15 17:58:58 +00:00
|
|
|
ClientError(w, http.StatusUnauthorized)
|
2022-05-26 13:27:25 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasRole {
|
2022-10-15 17:58:58 +00:00
|
|
|
ClientError(w, http.StatusForbidden)
|
2022-05-26 13:27:25 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
func (m *UserMiddleware) UserCanVote(next http.Handler) http.Handler {
|
|
|
|
return m.requireRole(next, models.RoleVoter)
|
2022-05-26 13:27:25 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
func (m *UserMiddleware) UserCanEditVote(next http.Handler) http.Handler {
|
|
|
|
return m.requireRole(next, models.RoleVoter)
|
2022-05-26 13:27:25 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
func (m *UserMiddleware) CanManageUsers(next http.Handler) http.Handler {
|
|
|
|
return m.requireRole(next, models.RoleSecretary, models.RoleAdmin)
|
2022-05-26 13:27:25 +00:00
|
|
|
}
|
2022-05-26 15:25:25 +00:00
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
func NoSurf(next http.Handler) http.Handler {
|
2022-05-26 15:25:25 +00:00
|
|
|
csrfHandler := nosurf.New(next)
|
|
|
|
csrfHandler.SetBaseCookie(http.Cookie{
|
|
|
|
HttpOnly: true,
|
|
|
|
Path: "/",
|
|
|
|
Secure: true,
|
|
|
|
SameSite: http.SameSiteStrictMode,
|
|
|
|
})
|
|
|
|
|
|
|
|
return csrfHandler
|
|
|
|
}
|
2022-10-15 17:58:58 +00:00
|
|
|
|
|
|
|
func getUser(r *http.Request) (*models.User, error) {
|
|
|
|
user := r.Context().Value(ctxUser)
|
|
|
|
if user == nil {
|
|
|
|
return nil, errors.New("no user in context")
|
|
|
|
}
|
|
|
|
|
|
|
|
result, ok := user.(*models.User)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("%v is not a user", user)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
err := pem.Encode(clientCertPEM, &pem.Block{Type: "CERTIFICATE", Bytes: authenticatedCertificate.Raw})
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error encoding client certificate: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return clientCertPEM.String(), nil
|
|
|
|
}
|