puerta/internal/server/server.go

296 lines
7.7 KiB
Go
Raw Normal View History

2022-12-30 06:51:51 +00:00
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2022 Roberto Hidalgo <nidito@un.rob.mx>
package server
import (
2023-04-16 21:17:36 +00:00
"bytes"
2022-12-30 06:51:51 +00:00
"embed"
"fmt"
2023-04-16 21:17:36 +00:00
"io"
2022-12-30 06:51:51 +00:00
"io/fs"
"log"
2023-04-16 23:03:18 +00:00
"mime"
2022-12-30 06:51:51 +00:00
"net/http"
2023-04-16 21:17:36 +00:00
"os"
"strings"
"time"
2022-12-30 06:51:51 +00:00
"git.rob.mx/nidito/puerta/internal/auth"
"git.rob.mx/nidito/puerta/internal/door"
"git.rob.mx/nidito/puerta/internal/errors"
2023-04-16 21:17:36 +00:00
"git.rob.mx/nidito/puerta/internal/push"
2023-01-04 04:21:49 +00:00
"git.rob.mx/nidito/puerta/internal/user"
2022-12-30 06:51:51 +00:00
"github.com/go-webauthn/webauthn/webauthn"
"github.com/julienschmidt/httprouter"
"github.com/sirupsen/logrus"
2022-12-30 06:51:51 +00:00
"github.com/upper/db/v4"
"github.com/upper/db/v4/adapter/sqlite"
)
//go:embed login.html
var loginTemplate []byte
//go:embed index.html
var indexTemplate []byte
//go:embed admin.html
var adminTemplate []byte
2022-12-30 06:51:51 +00:00
//go:embed static/*
var staticFiles embed.FS
type HTTPConfig struct {
2023-01-04 04:21:49 +00:00
// Listen is a hostname:port
Listen string `yaml:"listen"`
// Origin describes the http origins to allow
2023-01-04 07:07:56 +00:00
Origin string `yaml:"origin"`
2023-01-04 19:44:20 +00:00
// Protocol specifies the protocol for the webauthn origin
Protocol string `yaml:"protocol"`
2022-12-30 06:51:51 +00:00
}
type Config struct {
Name string `yaml:"name"`
Adapter map[string]any `yaml:"adapter"`
HTTP *HTTPConfig `yaml:"http"`
WebPush *push.Config `yaml:"push"`
Timezone string `yaml:"timezone"`
DB string `yaml:"db"`
2022-12-30 06:51:51 +00:00
}
func ConfigDefaults(dbPath string) *Config {
return &Config{
DB: dbPath,
HTTP: &HTTPConfig{
2023-01-04 19:44:20 +00:00
Listen: "localhost:8000",
Origin: "localhost",
Protocol: "http",
2022-12-30 06:51:51 +00:00
},
}
}
type auditLog struct {
2023-01-03 07:40:38 +00:00
Timestamp string `db:"timestamp" json:"timestamp"`
User string `db:"user" json:"user"`
SecondFactor bool `db:"second_factor" json:"second_factor"`
Failure string `db:"failure" json:"failure"`
Err string `db:"error" json:"error"`
IpAddress string `db:"ip_address" json:"ip_address"`
UserAgent string `db:"user_agent" json:"user_agent"`
}
func newAuditLog(r *http.Request, err error) *auditLog {
2023-01-04 04:21:49 +00:00
u := user.FromContext(r)
ip := r.RemoteAddr
2023-01-04 07:07:56 +00:00
xforward := r.Header.Get("X-Forwarded-For")
if xforward != "" {
ip = xforward
}
ua := r.Header.Get("user-agent")
al := &auditLog{
2023-01-03 07:40:38 +00:00
Timestamp: time.Now().UTC().Format(time.RFC3339),
2023-01-04 04:21:49 +00:00
User: u.Handle,
SecondFactor: u.Require2FA,
IpAddress: ip,
UserAgent: ua,
}
if err != nil {
al.Failure = err.Error()
if derr, ok := err.(door.Error); ok {
al.Err = derr.Name()
al.Failure = derr.Error()
}
}
return al
}
2023-01-03 07:40:38 +00:00
func allowCORS(handler httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
2023-01-04 07:07:56 +00:00
output := w.Header()
input := r.Header
if input.Get("Access-Control-Request-Method") != "" {
output.Set("Access-Control-Allow-Methods", input.Get("Allow"))
output.Set("Access-Control-Allow-Origin", r.Host)
output.Set("Access-Control-Allow-Credentials", "true")
output.Set("Access-Control-Allow-Headers", "content-type,webauthn")
output.Set("Access-Control-Expose-Headers", "webauthn")
if r.Method == http.MethodOptions {
// Set CORS headers
// Adjust status code to 204
w.WriteHeader(http.StatusOK)
return
}
2023-01-03 07:40:38 +00:00
}
if handler != nil {
handler(w, r, params)
}
}
}
2023-04-16 21:17:36 +00:00
func notifyAdmins(message string) {
subs := []*user.Subscription{}
err := _db.SQL().
SelectFrom("subscription as s").
Join("user as u").
On(`u.id = s.user and u.receives_notifications and u.is_admin`).
All(&subs)
if err != nil {
logrus.Errorf("could not fetch subscriptions: %s", err)
}
2023-04-16 22:19:40 +00:00
logrus.Infof("notifying %v admins", len(subs))
2023-04-16 21:17:36 +00:00
for _, sub := range subs {
err := push.Notify(message, sub)
if err != nil {
logrus.Errorf("could not push notification to subscription %s: %s", sub.ID(), err)
2023-04-16 21:17:36 +00:00
}
}
}
2022-12-30 06:51:51 +00:00
func rex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var err error
2023-01-04 04:21:49 +00:00
u := user.FromContext(r)
defer func(req *http.Request, err error) {
_, sqlErr := _db.Collection("log").Insert(newAuditLog(req, err))
if sqlErr != nil {
logrus.Errorf("could not record error log: %s", sqlErr)
}
}(r, err)
2022-12-30 06:51:51 +00:00
err = u.IsAllowed(time.Now().In(TZ))
if err != nil {
2023-01-04 04:21:49 +00:00
logrus.Errorf("Denying rex to %s: %s", u.Name, err)
http.Error(w, "Access denied", http.StatusForbidden)
return
}
2023-01-04 04:21:49 +00:00
err = door.RequestToEnter(u.Name)
if err != nil {
2022-12-30 06:51:51 +00:00
message, code := errors.ToHTTP(err)
http.Error(w, message, code)
return
}
2023-04-16 21:17:36 +00:00
go notifyAdmins(fmt.Sprintf("%s abrió la puerta", u.Name))
2022-12-30 06:51:51 +00:00
fmt.Fprintf(w, `{"status": "ok"}`)
}
var _db db.Session
var TZ *time.Location = time.UTC
2022-12-30 06:51:51 +00:00
func Initialize(config *Config) (http.Handler, error) {
2023-04-16 21:17:36 +00:00
devMode := os.Getenv("ENV") == "dev"
2022-12-30 06:51:51 +00:00
router := httprouter.New()
2023-01-04 07:07:56 +00:00
router.GlobalOPTIONS = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
allowCORS(nil)(w, r, nil)
})
2022-12-30 06:51:51 +00:00
if config.Timezone != "" {
mtz, err := time.LoadLocation(config.Timezone)
if err != nil {
return nil, fmt.Errorf("Unknown timezone %s", config.Timezone)
}
TZ = mtz
}
2022-12-30 06:51:51 +00:00
db := sqlite.ConnectionURL{
Database: config.DB,
Options: map[string]string{
"_journal": "WAL",
"_busy_timeout": "5000",
},
}
var err error
_db, err = sqlite.Open(db)
if err != nil {
return nil, err
}
if err := door.Connect(config.Adapter); err != nil {
2022-12-30 06:51:51 +00:00
return nil, err
}
2023-04-16 21:17:36 +00:00
origins := []string{config.HTTP.Protocol + "://" + config.HTTP.Origin}
if devMode {
origins = []string{config.HTTP.Protocol + "://" + config.HTTP.Listen}
}
2022-12-30 06:51:51 +00:00
wan, err := webauthn.New(&webauthn.Config{
RPDisplayName: config.Name,
RPID: strings.Split(config.HTTP.Origin, ":")[0],
2023-04-16 21:17:36 +00:00
RPOrigins: origins,
2022-12-30 06:51:51 +00:00
})
if err != nil {
return nil, err
}
2023-04-16 21:17:36 +00:00
push.Initialize(config.WebPush)
var assetRoot http.FileSystem
if devMode {
pwd, _ := os.Getwd()
dir := pwd + "/internal/server/static/"
logrus.Warnf("serving static assets from %s", dir)
assetRoot = http.Dir(dir)
} else {
subfs, err := fs.Sub(staticFiles, "static")
if err != nil {
log.Fatal(err)
}
assetRoot = http.FS(subfs)
2022-12-30 06:51:51 +00:00
}
2023-04-16 23:03:18 +00:00
mime.AddExtensionType(".webmanifest", "application/manifest+json")
2023-04-16 21:17:36 +00:00
router.ServeFiles("/static/*filepath", assetRoot)
2022-12-30 06:51:51 +00:00
router.GET("/login", renderTemplate(loginTemplate))
2023-01-04 04:21:49 +00:00
router.GET("/", auth.RequireAuthOrRedirect(renderTemplate(indexTemplate), "/login"))
2023-04-16 21:17:36 +00:00
router.GET("/admin-serviceworker.js", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
f, err := assetRoot.Open("/admin-serviceworker.js")
if err != nil {
sendError(w, err)
return
}
buf, err := io.ReadAll(f)
if err != nil {
sendError(w, err)
return
}
w.Header().Add("content-type", "application/javascript")
w.WriteHeader(200)
w.Write(buf)
})
2023-04-16 22:00:21 +00:00
router.GET("/admin", auth.RequireAdminOrRedirect(renderTemplate(bytes.ReplaceAll(adminTemplate, []byte("$PUSH_KEY$"), []byte(config.WebPush.Key.Public))), "/login?next=/admin"))
2023-01-04 04:21:49 +00:00
// regular api
router.POST("/api/login", auth.LoginHandler)
router.POST("/api/webauthn/register", auth.RequireAuth(auth.RegisterSecondFactor()))
router.POST("/api/rex", allowCORS(auth.Enforce2FA(rex)))
// admin api
router.GET("/api/log", allowCORS(auth.RequireAdmin(rexRecords)))
router.GET("/api/user", allowCORS(auth.RequireAdmin(listUsers)))
router.GET("/api/user/:id", allowCORS(auth.RequireAdmin(getUser)))
router.POST("/api/user", allowCORS(auth.RequireAdmin(auth.Enforce2FA(createUser))))
router.POST("/api/user/:id", allowCORS(auth.RequireAdmin(auth.Enforce2FA(updateUser))))
router.DELETE("/api/user/:id", allowCORS(auth.RequireAdmin(auth.Enforce2FA(deleteUser))))
2023-04-16 21:17:36 +00:00
router.POST("/api/push/subscribe", allowCORS(auth.RequireAdmin(auth.Enforce2FA(createSubscription))))
router.POST("/api/push/unsubscribe", allowCORS(auth.RequireAdmin(auth.Enforce2FA(deleteSubscription))))
2023-01-04 04:21:49 +00:00
2023-01-04 04:51:35 +00:00
return auth.Route(wan, _db, router), nil
2022-12-30 06:51:51 +00:00
}
func renderTemplate(template []byte) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Write(template)
}
}