diff --git a/.env.example b/.env.example index ccd30d6..095a33c 100644 --- a/.env.example +++ b/.env.example @@ -5,3 +5,6 @@ UPTIME_CHECKER_REGION=local # Sentinel instead of a single redis (docker compose --profile sentinel up -d) # UPTIME_REDIS_SENTINEL_MASTER=uptime # UPTIME_REDIS_SENTINEL_ADDRS=127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381 + +# Generate with: echo "k1:$(head -c32 /dev/urandom | base64)" +UPTIME_SECRET_KEYS=dev:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= diff --git a/cmd/api/main.go b/cmd/api/main.go index 6541ed6..092d55f 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -4,15 +4,16 @@ import ( "context" "errors" "log/slog" - "net/http" "os" "time" - "github.com/gin-gonic/gin" - + "git.zb-server.de/ZB-Server/uptimesebi/internal/api" + "git.zb-server.de/ZB-Server/uptimesebi/internal/auth" "git.zb-server.de/ZB-Server/uptimesebi/internal/config" "git.zb-server.de/ZB-Server/uptimesebi/internal/migrations" "git.zb-server.de/ZB-Server/uptimesebi/internal/platform" + "git.zb-server.de/ZB-Server/uptimesebi/internal/sealed" + "git.zb-server.de/ZB-Server/uptimesebi/internal/tenant" "git.zb-server.de/ZB-Server/uptimesebi/internal/web" ) @@ -40,17 +41,28 @@ func run(log *slog.Logger, args []string) error { return errors.New("usage: api [serve | migrate up|down|status]") } + if err := cfg.Validate(); err != nil { + return err + } + keys, err := sealed.Parse(cfg.SecretKeys) + if err != nil { + return err + } deps, err := platform.Connect(ctx, cfg.Common, log) if err != nil { return err } defer deps.Close() + srv := api.New(cfg, log, tenant.New(deps.DB), deps.Redis, keys) health := platform.NewHealth(deps.Probes()) r := platform.NewRouter(log) health.Register(r) r.StaticFS("/static", web.Static()) - r.GET("/", func(c *gin.Context) { web.Render(c, http.StatusOK, web.Index()) }) + + app := r.Group("/", srv.Sessions().Load(), auth.CSRF()) + srv.Register(app) + srv.RegisterPages(app) return platform.Serve(ctx, log, platform.NewHTTPServer(cfg.Listen, r), health, 5*time.Second, cfg.ShutdownTimeout) } diff --git a/go.mod b/go.mod index d060a62..6d3d9d8 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( require ( github.com/a-h/parse v0.0.42 // indirect github.com/andybalholm/brotli v1.3.0 // indirect + github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirect github.com/bytedance/gopkg v0.1.3 // indirect github.com/bytedance/sonic v1.15.0 // indirect github.com/bytedance/sonic/loader v0.5.0 // indirect @@ -24,11 +25,11 @@ require ( github.com/cloudwego/base64x v0.1.6 // indirect github.com/fatih/color v1.19.0 // indirect github.com/fsnotify/fsnotify v1.10.1 // indirect - github.com/gabriel-vasile/mimetype v1.4.12 // indirect + github.com/gabriel-vasile/mimetype v1.4.16 // indirect github.com/gin-contrib/sse v1.1.0 // indirect - github.com/go-playground/locales v0.14.1 // indirect - github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-playground/validator/v10 v10.30.1 // indirect + github.com/go-playground/locales v0.14.2 // indirect + github.com/go-playground/universal-translator v0.18.2 // indirect + github.com/go-playground/validator/v10 v10.30.6 // indirect github.com/goccy/go-json v0.10.5 // indirect github.com/goccy/go-yaml v1.19.2 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect @@ -36,7 +37,7 @@ require ( github.com/jackc/puddle/v2 v2.2.3 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.4.0 // indirect - github.com/leodido/go-urn v1.4.0 // indirect + github.com/leodido/go-urn v1.5.0 // indirect github.com/mattn/go-colorable v0.1.16 // indirect github.com/mattn/go-isatty v0.0.24 // indirect github.com/mfridman/interpolate v0.2.0 // indirect @@ -44,6 +45,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/natefinch/atomic v1.0.1 // indirect github.com/pelletier/go-toml/v2 v2.2.4 // indirect + github.com/pquerna/otp v1.5.0 // indirect github.com/quic-go/qpack v0.6.0 // indirect github.com/quic-go/quic-go v0.59.0 // indirect github.com/sethvargo/go-retry v0.5.0 // indirect @@ -53,7 +55,7 @@ require ( go.uber.org/atomic v1.12.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/arch v0.22.0 // indirect - golang.org/x/crypto v0.58.0 // indirect + golang.org/x/crypto v0.59.0 // indirect golang.org/x/mod v0.43.0 // indirect golang.org/x/net v0.62.0 // indirect golang.org/x/sync v0.25.0 // indirect diff --git a/go.sum b/go.sum index be41d9f..22633bb 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/a-h/templ v0.3.1118 h1:rZv90g1xVb7ieNz4j5WK6yQePZfsg7j7fVvP8qVmeJI= github.com/a-h/templ v0.3.1118/go.mod h1:ttKggixbDULrwkJzfEO5fa4IPGTKiRt3qP09zjPuCS4= github.com/andybalholm/brotli v1.3.0 h1:IdkhjSe/+34U+fP7FSR2Q6Poj1hnHUa2GoWmzXJd6qY= github.com/andybalholm/brotli v1.3.0/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY= +github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc h1:biVzkmvwrH8WK8raXaxBx6fRVTlJILwEwQGL1I/ByEI= +github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= @@ -35,6 +37,8 @@ github.com/fsnotify/fsnotify v1.10.1 h1:b0/UzAf9yR5rhf3RPm9gf3ehBPpf0oZKIjtpKrx5 github.com/fsnotify/fsnotify v1.10.1/go.mod h1:TLheqan6HD6GBK6PrDWyDPBaEV8LspOxvPSjC+bVfgo= github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s= +github.com/gabriel-vasile/mimetype v1.4.16 h1:5izNFuvA37st1dDRAcA7TtmngBmRgiIPDmTtiEvjx1E= +github.com/gabriel-vasile/mimetype v1.4.16/go.mod h1:3oFFwxX8kTklZwzIl2aQEDZT0lKY8xH92ST88bAwba4= github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w= github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8= @@ -43,10 +47,16 @@ github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lY github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/locales v0.14.2 h1:d8UmcrM6Nip0hfGZKLGpAvZH37XB4TS0xzK9B56YNCY= +github.com/go-playground/locales v0.14.2/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/universal-translator v0.18.2 h1:LCsMLC9RzmbUMNUPVYD15dmcjwYAJhmX8mPZRW4rAVU= +github.com/go-playground/universal-translator v0.18.2/go.mod h1:67VZIMp5lQpDWlnStOct22q1bkdJGJqHghbOtmkawxk= github.com/go-playground/validator/v10 v10.30.1 h1:f3zDSN/zOma+w6+1Wswgd9fLkdwy06ntQJp0BBvFG0w= github.com/go-playground/validator/v10 v10.30.1/go.mod h1:oSuBIQzuJxL//3MelwSLD5hc2Tu889bF0Idm9Dg26cM= +github.com/go-playground/validator/v10 v10.30.6 h1:E+mymlPNRzAMNz1FShrRvLeGWcprF5w305eLLgWieuo= +github.com/go-playground/validator/v10 v10.30.6/go.mod h1:jPLL8SKCXGe/g87QmEyNw3NO28nmGt5GCnZnufm7rj4= github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM= @@ -70,6 +80,8 @@ github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV91 github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/leodido/go-urn v1.5.0 h1:pLqT2kq1zpHW/1D18QMjMpdtX7cekxqtJJjg5ANyWw0= +github.com/leodido/go-urn v1.5.0/go.mod h1:9BORnCDhdPBJNDEX+w1bJisa8yOKYi116VeO96s4ifE= github.com/mattn/go-colorable v0.1.16 h1:+2YjeAe7xP1OQbZW1jERPWWCDLlNqFNCvjiqGiAt84A= github.com/mattn/go-colorable v0.1.16/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI= @@ -88,6 +100,8 @@ github.com/ncruces/go-strftime v1.1.0/go.mod h1:yVaq7iwCN09vHRrKBrrflXT0M9tTMG2K github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pquerna/otp v1.5.0 h1:NMMR+WrmaqXU4EzdGJEE1aUUI0AMRzsp96fFFWNPwxs= +github.com/pquerna/otp v1.5.0/go.mod h1:dkJfzwRKNiegxyNb54X/3fLwhCynbMspSyWKnvi1AEg= github.com/pressly/goose/v3 v3.30.1 h1:KO28meBdbAUIdMbTP7bl9dp1FYZGXcYWb2O7miT09lE= github.com/pressly/goose/v3 v3.30.1/go.mod h1:CHGwuG2acDerUaXwz2i5T18VSauXpYDiL4WxzDvcOIM= github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8= @@ -100,6 +114,8 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/sethvargo/go-retry v0.5.0 h1:JYfFKmVJ8ER06GReN1efzc5cm52BrVsFVzmcHiKJwfY= github.com/sethvargo/go-retry v0.5.0/go.mod h1:tvsjdKG6xfiCx4LSiUZ06kcv38xvdVQwv8R6/VnnVWg= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -134,6 +150,8 @@ golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI= golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A= golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54= golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w= +golang.org/x/crypto v0.59.0 h1:xS2jhqYbzNrpvkWP2YTDf4TJwBy3ykR03i8vFDxEc7c= +golang.org/x/crypto v0.59.0/go.mod h1:veDyUwATGs3c+vN3VfTbzViMGXeTBk3WyhBOkn+F/4U= golang.org/x/mod v0.43.0 h1:MFuq5J6s35W9D3IjkwyWc9vHh0nENOrt5K8Iof4ofmg= golang.org/x/mod v0.43.0/go.mod h1:4ec1JnuyBz5+FIVhg86UsA39YG8BpnrZdJYSNHmGzHY= golang.org/x/net v0.62.0 h1:18ELalfM5+XNRwzM2nYqXsZQ9ByS1uhj2O/U7+5jqRY= diff --git a/internal/api/auth.go b/internal/api/auth.go new file mode 100644 index 0000000..71472ea --- /dev/null +++ b/internal/api/auth.go @@ -0,0 +1,299 @@ +package api + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "net/http" + "strings" + "time" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + + "git.zb-server.de/ZB-Server/uptimesebi/internal/auth" + "git.zb-server.de/ZB-Server/uptimesebi/internal/config" + "git.zb-server.de/ZB-Server/uptimesebi/internal/store" + "git.zb-server.de/ZB-Server/uptimesebi/internal/tenant" +) + +const ( + mfaCookie = "__Host-mfa" + mfaTTL = 5 * time.Minute +) + +type signupRequest struct { + Email string `json:"email" binding:"required,email,max=320"` + Password string `json:"password" binding:"required,max=1024"` + DisplayName string `json:"display_name" binding:"max=200"` + OrgName string `json:"org_name" binding:"required,max=200"` + OrgSlug string `json:"org_slug" binding:"required,max=63"` +} + +func (s *Server) signup(c *gin.Context) { + var req signupRequest + if !bind(c, &req) { + return + } + ctx := c.Request.Context() + if !s.limit(c, "signup:"+c.ClientIP(), 10, time.Hour) { + return + } + if allowed, err := s.signupAllowed(ctx); err != nil { + internalError(c, err) + return + } else if !allowed { + fail(c, http.StatusForbidden, "signup_closed", "Registrierung ist deaktiviert.") + return + } + hash, err := auth.HashPassword(req.Password) + if errors.Is(err, auth.ErrPasswordTooShort) { + fail(c, http.StatusUnprocessableEntity, "password_too_short", err.Error()) + return + } else if err != nil { + internalError(c, err) + return + } + + orgID := uuid.New() + var user store.User + err = s.db.Tx(ctx, tenant.Org(orgID, uuid.Nil), func(q *store.Queries) error { + var err error + user, err = q.CreateUser(ctx, store.CreateUserParams{ + Email: normalizeEmail(req.Email), + DisplayName: strings.TrimSpace(req.DisplayName), + PasswordHash: &hash, + }) + if err != nil { + return err + } + if _, err := q.CreateOrg(ctx, store.CreateOrgParams{ID: orgID, Slug: strings.ToLower(req.OrgSlug), Name: strings.TrimSpace(req.OrgName)}); err != nil { + return err + } + return q.AddMembership(ctx, store.AddMembershipParams{OrgID: orgID, UserID: user.ID, IsOwner: true}) + }) + switch { + case isUniqueViolation(err, "users_email_key"): + fail(c, http.StatusConflict, "email_taken", "Diese E-Mail-Adresse ist bereits registriert.") + return + case isUniqueViolation(err, "orgs_slug_key"): + fail(c, http.StatusConflict, "slug_taken", "Dieser Kurzname ist bereits vergeben.") + return + case isCheckViolation(err): + fail(c, http.StatusUnprocessableEntity, "invalid_input", "Kurzname: 2–63 Zeichen, a–z, 0–9 und Bindestrich.") + return + case err != nil: + internalError(c, err) + return + } + s.startSession(c, user.ID, orgID) +} + +func (s *Server) signupAllowed(ctx context.Context) (bool, error) { + switch s.cfg.Signup { + case config.SignupOpen: + return true, nil + case config.SignupFirstUser: + var exists bool + err := s.db.Tx(ctx, tenant.Global(), func(q *store.Queries) error { + var err error + exists, err = q.AnyUserExists(ctx) + return err + }) + return !exists, err + } + return false, nil +} + +type loginRequest struct { + Email string `json:"email" binding:"required,max=320"` + Password string `json:"password" binding:"required,max=1024"` +} + +func (s *Server) login(c *gin.Context) { + var req loginRequest + if !bind(c, &req) { + return + } + ctx := c.Request.Context() + email := normalizeEmail(req.Email) + if !s.limit(c, "login:ip:"+c.ClientIP(), 30, 15*time.Minute) || !s.limit(c, "login:email:"+email, 10, 15*time.Minute) { + return + } + + var user store.User + err := s.db.Tx(ctx, tenant.Global(), func(q *store.Queries) error { + var err error + user, err = q.GetUserByEmail(ctx, email) + return err + }) + if errors.Is(err, pgx.ErrNoRows) || (err == nil && user.PasswordHash == nil) { + auth.BurnPasswordCheck(req.Password) + invalidCredentials(c) + return + } + if err != nil { + internalError(c, err) + return + } + ok, rehash, err := auth.VerifyPassword(*user.PasswordHash, req.Password) + if err != nil { + internalError(c, err) + return + } + if !ok || user.DisabledAt != nil { + invalidCredentials(c) + return + } + if rehash { + s.rehashPassword(ctx, user.ID, req.Password) + } + + if user.TotpSecret != nil { + token := randomToken() + if err := s.rdb.Set(ctx, mfaKey(token), user.ID.String(), mfaTTL).Err(); err != nil { + internalError(c, err) + return + } + setCookie(c, mfaCookie, token, int(mfaTTL.Seconds())) + c.JSON(http.StatusOK, gin.H{"status": "totp_required"}) + return + } + s.finishLogin(c, user.ID) +} + +type totpRequest struct { + Code string `json:"code" binding:"required,len=6,numeric"` +} + +func (s *Server) loginTOTP(c *gin.Context) { + var req totpRequest + if !bind(c, &req) { + return + } + ctx := c.Request.Context() + token, _ := c.Cookie(mfaCookie) + if token == "" { + fail(c, http.StatusUnauthorized, "mfa_expired", "Anmeldung abgelaufen, bitte erneut anmelden.") + return + } + if !s.limit(c, "mfa:"+mfaKey(token), 5, mfaTTL) { + return + } + raw, err := s.rdb.Get(ctx, mfaKey(token)).Result() + if err != nil { + fail(c, http.StatusUnauthorized, "mfa_expired", "Anmeldung abgelaufen, bitte erneut anmelden.") + return + } + userID, err := uuid.Parse(raw) + if err != nil { + internalError(c, err) + return + } + ok, err := s.verifyUserTOTP(ctx, userID, req.Code) + if err != nil { + internalError(c, err) + return + } + if !ok { + fail(c, http.StatusUnauthorized, "invalid_code", "Code ungültig.") + return + } + _ = s.rdb.Del(ctx, mfaKey(token)).Err() + setCookie(c, mfaCookie, "", -1) + s.finishLogin(c, userID) +} + +func (s *Server) finishLogin(c *gin.Context, userID uuid.UUID) { + ctx := c.Request.Context() + var orgs []store.ListUserOrgsRow + err := s.db.Tx(ctx, tenant.User(userID), func(q *store.Queries) error { + var err error + orgs, err = q.ListUserOrgs(ctx, userID) + return err + }) + if err != nil { + internalError(c, err) + return + } + var org uuid.UUID + if len(orgs) > 0 { + org = orgs[0].ID + } + s.startSession(c, userID, org) +} + +func (s *Server) startSession(c *gin.Context, userID, orgID uuid.UUID) { + if old := auth.Current(c); old != nil { + _ = s.sessions.Delete(c.Request.Context(), old) + } + sess, err := s.sessions.Create(c.Request.Context(), userID, orgID) + if err != nil { + internalError(c, err) + return + } + auth.SetCookie(c, sess, int(s.cfg.SessionAbsolute.Seconds())) + c.JSON(http.StatusOK, gin.H{"status": "ok", "csrf_token": sess.CSRF}) +} + +func (s *Server) logout(c *gin.Context) { + if sess := auth.Current(c); sess != nil { + if err := s.sessions.Delete(c.Request.Context(), sess); err != nil { + internalError(c, err) + return + } + } + auth.ClearCookie(c) + c.Status(http.StatusNoContent) +} + +func (s *Server) rehashPassword(ctx context.Context, user uuid.UUID, pw string) { + hash, err := auth.HashPassword(pw) + if err != nil { + return + } + err = s.db.Tx(ctx, tenant.Global(), func(q *store.Queries) error { + return q.SetPasswordHash(ctx, store.SetPasswordHashParams{ID: user, PasswordHash: &hash}) + }) + if err != nil { + s.log.WarnContext(ctx, "password rehash failed", "error", err) + } +} + +func (s *Server) limit(c *gin.Context, key string, n int, window time.Duration) bool { + ok, err := s.limiter.Allow(c.Request.Context(), key, n, window) + if err != nil { + internalError(c, err) + return false + } + if !ok { + c.Header("Retry-After", "60") + fail(c, http.StatusTooManyRequests, "rate_limited", "Zu viele Versuche, bitte später erneut.") + return false + } + return true +} + +func mfaKey(token string) string { + sum := sha256.Sum256([]byte(token)) + return "login_mfa:" + hex.EncodeToString(sum[:]) +} + +func normalizeEmail(e string) string { return strings.ToLower(strings.TrimSpace(e)) } + +func invalidCredentials(c *gin.Context) { + fail(c, http.StatusUnauthorized, "invalid_credentials", "E-Mail oder Passwort falsch.") +} + +func isUniqueViolation(err error, constraint string) bool { + var pgErr *pgconn.PgError + return errors.As(err, &pgErr) && pgErr.Code == "23505" && pgErr.ConstraintName == constraint +} + +func isCheckViolation(err error) bool { + var pgErr *pgconn.PgError + return errors.As(err, &pgErr) && pgErr.Code == "23514" +} diff --git a/internal/api/me.go b/internal/api/me.go new file mode 100644 index 0000000..b7b60f7 --- /dev/null +++ b/internal/api/me.go @@ -0,0 +1,261 @@ +package api + +import ( + "bytes" + "context" + "encoding/base64" + "errors" + "image/png" + "net/http" + "time" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" + "github.com/pquerna/otp" + + "git.zb-server.de/ZB-Server/uptimesebi/internal/auth" + "git.zb-server.de/ZB-Server/uptimesebi/internal/store" + "git.zb-server.de/ZB-Server/uptimesebi/internal/tenant" +) + +const totpSetupTTL = 10 * time.Minute + +type orgView struct { + ID uuid.UUID `json:"id"` + Slug string `json:"slug"` + Name string `json:"name"` + IsOwner bool `json:"is_owner"` + RequireTOTP bool `json:"require_totp"` +} + +type meView struct { + ID uuid.UUID `json:"id"` + Email string `json:"email"` + DisplayName string `json:"display_name"` + TOTP bool `json:"totp_enabled"` + ActiveOrg uuid.UUID `json:"active_org"` + Orgs []orgView `json:"orgs"` + // Set when an org the user belongs to enforces TOTP and the user has none. + TOTPEnrollmentRequired bool `json:"totp_enrollment_required"` +} + +func (s *Server) loadMe(ctx context.Context, sess *auth.Session) (meView, error) { + var me meView + err := s.db.Tx(ctx, tenant.User(sess.UserID), func(q *store.Queries) error { + u, err := q.GetUser(ctx, sess.UserID) + if err != nil { + return err + } + orgs, err := q.ListUserOrgs(ctx, sess.UserID) + if err != nil { + return err + } + me = meView{ID: u.ID, Email: u.Email, DisplayName: u.DisplayName, TOTP: u.TotpSecret != nil, ActiveOrg: sess.OrgID, Orgs: []orgView{}} + for _, o := range orgs { + me.Orgs = append(me.Orgs, orgView{ID: o.ID, Slug: o.Slug, Name: o.Name, IsOwner: o.IsOwner, RequireTOTP: o.RequireTotp}) + if o.RequireTotp && !me.TOTP { + me.TOTPEnrollmentRequired = true + } + } + return nil + }) + return me, err +} + +func (s *Server) me(c *gin.Context) { + me, err := s.loadMe(c.Request.Context(), auth.Current(c)) + if err != nil { + internalError(c, err) + return + } + c.JSON(http.StatusOK, me) +} + +type switchOrgRequest struct { + OrgID uuid.UUID `json:"org_id" binding:"required"` +} + +func (s *Server) switchOrg(c *gin.Context) { + var req switchOrgRequest + if !bind(c, &req) { + return + } + sess := auth.Current(c) + me, err := s.loadMe(c.Request.Context(), sess) + if err != nil { + internalError(c, err) + return + } + for _, o := range me.Orgs { + if o.ID == req.OrgID { + if err := s.sessions.SwitchOrg(c.Request.Context(), sess, o.ID); err != nil { + internalError(c, err) + return + } + c.Status(http.StatusNoContent) + return + } + } + fail(c, http.StatusNotFound, "org_not_found", "Organisation nicht gefunden.") +} + +func totpSetupKey(user uuid.UUID) string { return "totp_setup:" + user.String() } + +func (s *Server) totpSetup(c *gin.Context) { + ctx := c.Request.Context() + me, err := s.loadMe(ctx, auth.Current(c)) + if err != nil { + internalError(c, err) + return + } + if me.TOTP { + fail(c, http.StatusConflict, "totp_already_enabled", "TOTP ist bereits aktiv.") + return + } + key, err := auth.NewTOTPKey(me.Email) + if err != nil { + internalError(c, err) + return + } + if err := s.rdb.Set(ctx, totpSetupKey(me.ID), key.Secret(), totpSetupTTL).Err(); err != nil { + internalError(c, err) + return + } + qr, err := qrDataURI(key) + if err != nil { + internalError(c, err) + return + } + c.JSON(http.StatusOK, gin.H{"secret": key.Secret(), "otpauth_url": key.URL(), "qr": qr}) +} + +func (s *Server) totpEnable(c *gin.Context) { + var req totpRequest + if !bind(c, &req) { + return + } + ctx := c.Request.Context() + sess := auth.Current(c) + secret, err := s.rdb.Get(ctx, totpSetupKey(sess.UserID)).Result() + if err != nil { + fail(c, http.StatusConflict, "totp_setup_expired", "Einrichtung abgelaufen, bitte neu starten.") + return + } + if !s.limit(c, "totp_enable:"+sess.UserID.String(), 10, totpSetupTTL) { + return + } + ok, err := s.totp.Verify(ctx, sess.UserID, secret, req.Code) + if err != nil { + internalError(c, err) + return + } + if !ok { + fail(c, http.StatusUnprocessableEntity, "invalid_code", "Code ungültig.") + return + } + sealedSecret := s.keys.Seal([]byte(secret), sess.UserID[:]) + err = s.db.Tx(ctx, tenant.User(sess.UserID), func(q *store.Queries) error { + return q.EnableTOTP(ctx, store.EnableTOTPParams{ID: sess.UserID, TotpSecret: sealedSecret}) + }) + if err != nil { + internalError(c, err) + return + } + _ = s.rdb.Del(ctx, totpSetupKey(sess.UserID)).Err() + c.Status(http.StatusNoContent) +} + +type totpDisableRequest struct { + Password string `json:"password" binding:"required,max=1024"` + Code string `json:"code" binding:"required,len=6,numeric"` +} + +func (s *Server) totpDisable(c *gin.Context) { + var req totpDisableRequest + if !bind(c, &req) { + return + } + ctx := c.Request.Context() + sess := auth.Current(c) + if !s.limit(c, "totp_disable:"+sess.UserID.String(), 5, 15*time.Minute) { + return + } + me, err := s.loadMe(ctx, sess) + if err != nil { + internalError(c, err) + return + } + for _, o := range me.Orgs { + if o.RequireTOTP { + fail(c, http.StatusConflict, "totp_required_by_org", "Eine deiner Organisationen verlangt TOTP.") + return + } + } + var user store.User + err = s.db.Tx(ctx, tenant.User(sess.UserID), func(q *store.Queries) error { + var err error + user, err = q.GetUser(ctx, sess.UserID) + return err + }) + if err != nil { + internalError(c, err) + return + } + if user.PasswordHash == nil { + fail(c, http.StatusConflict, "no_password", "Konto ohne Passwort.") + return + } + if ok, _, err := auth.VerifyPassword(*user.PasswordHash, req.Password); err != nil || !ok { + invalidCredentials(c) + return + } + if ok, err := s.verifyUserTOTP(ctx, sess.UserID, req.Code); err != nil { + internalError(c, err) + return + } else if !ok { + fail(c, http.StatusUnauthorized, "invalid_code", "Code ungültig.") + return + } + err = s.db.Tx(ctx, tenant.User(sess.UserID), func(q *store.Queries) error { + return q.DisableTOTP(ctx, sess.UserID) + }) + if err != nil { + internalError(c, err) + return + } + c.Status(http.StatusNoContent) +} + +var errNoTOTP = errors.New("api: user has no totp") + +func (s *Server) verifyUserTOTP(ctx context.Context, userID uuid.UUID, code string) (bool, error) { + var user store.User + err := s.db.Tx(ctx, tenant.User(userID), func(q *store.Queries) error { + var err error + user, err = q.GetUser(ctx, userID) + return err + }) + if err != nil { + return false, err + } + if user.TotpSecret == nil { + return false, errNoTOTP + } + secret, err := s.keys.Open(user.TotpSecret, userID[:]) + if err != nil { + return false, err + } + return s.totp.Verify(ctx, userID, string(secret), code) +} + +func qrDataURI(key *otp.Key) (string, error) { + img, err := key.Image(240, 240) + if err != nil { + return "", err + } + var buf bytes.Buffer + if err := png.Encode(&buf, img); err != nil { + return "", err + } + return "data:image/png;base64," + base64.StdEncoding.EncodeToString(buf.Bytes()), nil +} diff --git a/internal/api/pages.go b/internal/api/pages.go new file mode 100644 index 0000000..cc3a283 --- /dev/null +++ b/internal/api/pages.go @@ -0,0 +1,82 @@ +package api + +import ( + "net/http" + + "github.com/a-h/templ" + "github.com/gin-gonic/gin" + + "git.zb-server.de/ZB-Server/uptimesebi/internal/auth" + "git.zb-server.de/ZB-Server/uptimesebi/internal/web" +) + +func (s *Server) RegisterPages(r gin.IRouter) { + r.GET("/login", s.loginPage) + r.GET("/signup", s.signupPage) + r.GET("/", s.page("Übersicht", web.Dashboard)) + r.GET("/settings/security", s.page("Sicherheit", web.Security)) +} + +func pageFor(c *gin.Context, title string) web.Page { + p := web.Page{Title: title} + if sess := auth.Current(c); sess != nil { + p.CSRF = sess.CSRF + } + return p +} + +func (s *Server) loginPage(c *gin.Context) { + if auth.Current(c) != nil { + c.Redirect(http.StatusSeeOther, "/") + return + } + allowed, err := s.signupAllowed(c.Request.Context()) + if err != nil { + internalError(c, err) + return + } + web.Render(c, http.StatusOK, web.Login(pageFor(c, "Anmelden"), allowed)) +} + +func (s *Server) signupPage(c *gin.Context) { + allowed, err := s.signupAllowed(c.Request.Context()) + if err != nil { + internalError(c, err) + return + } + if !allowed { + c.Redirect(http.StatusSeeOther, "/login") + return + } + web.Render(c, http.StatusOK, web.Signup(pageFor(c, "Konto anlegen"))) +} + +func (s *Server) page(title string, view func(web.Page, web.Me) templ.Component) gin.HandlerFunc { + return func(c *gin.Context) { + sess := auth.Current(c) + if sess == nil { + c.Redirect(http.StatusSeeOther, "/login") + return + } + me, err := s.loadMe(c.Request.Context(), sess) + if err != nil { + internalError(c, err) + return + } + web.Render(c, http.StatusOK, view(pageFor(c, title), toWebMe(me))) + } +} + +func toWebMe(m meView) web.Me { + out := web.Me{ + Email: m.Email, + DisplayName: m.DisplayName, + TOTP: m.TOTP, + ActiveOrg: m.ActiveOrg.String(), + TOTPMissing: m.TOTPEnrollmentRequired, + } + for _, o := range m.Orgs { + out.Orgs = append(out.Orgs, web.Org{ID: o.ID.String(), Name: o.Name, Slug: o.Slug, IsOwner: o.IsOwner}) + } + return out +} diff --git a/internal/api/server.go b/internal/api/server.go new file mode 100644 index 0000000..7d99408 --- /dev/null +++ b/internal/api/server.go @@ -0,0 +1,101 @@ +package api + +import ( + "errors" + "log/slog" + "net/http" + + "github.com/gin-gonic/gin" + "github.com/go-playground/validator/v10" + "github.com/redis/go-redis/v9" + + "git.zb-server.de/ZB-Server/uptimesebi/internal/auth" + "git.zb-server.de/ZB-Server/uptimesebi/internal/config" + "git.zb-server.de/ZB-Server/uptimesebi/internal/sealed" + "git.zb-server.de/ZB-Server/uptimesebi/internal/tenant" +) + +type Server struct { + cfg config.API + log *slog.Logger + db *tenant.DB + rdb redis.UniversalClient + keys *sealed.Keyring + sessions *auth.Sessions + limiter *auth.Limiter + totp *auth.TOTPVerifier +} + +func New(cfg config.API, log *slog.Logger, db *tenant.DB, rdb redis.UniversalClient, keys *sealed.Keyring) *Server { + return &Server{ + cfg: cfg, + log: log, + db: db, + rdb: rdb, + keys: keys, + sessions: auth.NewSessions(rdb, cfg.SessionIdle, cfg.SessionAbsolute), + limiter: auth.NewLimiter(rdb), + totp: auth.NewTOTPVerifier(rdb), + } +} + +func (s *Server) Sessions() *auth.Sessions { return s.sessions } + +// Register mounts the JSON API. The caller must already have installed +// Sessions().Load() and auth.CSRF() on the engine. +func (s *Server) Register(r gin.IRouter) { + v1 := r.Group("/api/v1") + + a := v1.Group("/auth") + a.POST("/signup", s.signup) + a.POST("/login", s.login) + a.POST("/login/totp", s.loginTOTP) + a.POST("/logout", s.logout) + + me := v1.Group("/me", auth.Require()) + me.GET("", s.me) + me.POST("/org", s.switchOrg) + me.POST("/totp/setup", s.totpSetup) + me.POST("/totp/enable", s.totpEnable) + me.POST("/totp/disable", s.totpDisable) +} + +type apiError struct { + Error string `json:"error"` + Message string `json:"message"` + Fields map[string]string `json:"fields,omitempty"` +} + +func fail(c *gin.Context, status int, code, msg string) { + c.AbortWithStatusJSON(status, apiError{Error: code, Message: msg}) +} + +func internalError(c *gin.Context, err error) { + _ = c.Error(err) + c.AbortWithStatusJSON(http.StatusInternalServerError, apiError{Error: "internal", Message: "Interner Fehler."}) +} + +func bind(c *gin.Context, dst any) bool { + if err := c.ShouldBindJSON(dst); err != nil { + e := apiError{Error: "invalid_input", Message: "Eingaben prüfen."} + var ve validator.ValidationErrors + if errors.As(err, &ve) { + e.Fields = map[string]string{} + for _, f := range ve { + e.Fields[f.Field()] = f.Tag() + } + } + c.AbortWithStatusJSON(http.StatusBadRequest, e) + return false + } + return true +} + +func setCookie(c *gin.Context, name, value string, maxAge int) { + http.SetCookie(c.Writer, &http.Cookie{ + Name: name, Value: value, Path: "/", MaxAge: maxAge, + Secure: true, HttpOnly: true, SameSite: http.SameSiteLaxMode, + }) +} + +func randomToken() string { return auth.RandomToken() } diff --git a/internal/config/config.go b/internal/config/config.go index ab0ba76..3705616 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -35,9 +35,39 @@ type Sentinel struct { func (s Sentinel) Enabled() bool { return s.Master != "" } +type SignupMode string + +const ( + SignupOpen SignupMode = "open" + SignupFirstUser SignupMode = "first-user" + SignupClosed SignupMode = "closed" +) + type API struct { Common - Listen string `env:"UPTIME_API_LISTEN" envDefault:":8080"` + Listen string `env:"UPTIME_API_LISTEN" envDefault:":8080"` + SecretKeys string `env:"UPTIME_SECRET_KEYS"` + Signup SignupMode `env:"UPTIME_SIGNUP" envDefault:"first-user"` + SessionIdle time.Duration `env:"UPTIME_SESSION_IDLE" envDefault:"168h"` + SessionAbsolute time.Duration `env:"UPTIME_SESSION_ABSOLUTE" envDefault:"720h"` +} + +func (a API) Validate() error { + if err := a.Common.Validate(); err != nil { + return err + } + if a.SecretKeys == "" { + return fmt.Errorf("config: UPTIME_SECRET_KEYS is required (format id:base64-32-bytes)") + } + switch a.Signup { + case SignupOpen, SignupFirstUser, SignupClosed: + default: + return fmt.Errorf("config: unknown UPTIME_SIGNUP %q", a.Signup) + } + if a.SessionIdle <= 0 || a.SessionAbsolute < a.SessionIdle { + return fmt.Errorf("config: UPTIME_SESSION_ABSOLUTE must be >= UPTIME_SESSION_IDLE > 0") + } + return nil } type Checker struct { diff --git a/internal/web/assets/app.js b/internal/web/assets/app.js index 35d650f..3aa3820 100644 --- a/internal/web/assets/app.js +++ b/internal/web/assets/app.js @@ -1,8 +1,77 @@ (() => { - const token = document.querySelector('meta[name="csrf-token"]')?.content; - window.api = axios.create({ - baseURL: "/api/v1", - headers: token ? { "X-CSRF-Token": token } : {}, - withCredentials: true, + const meta = document.querySelector('meta[name="csrf-token"]'); + const api = axios.create({ baseURL: "/api/v1", withCredentials: true }); + api.interceptors.request.use((cfg) => { + if (meta?.content) cfg.headers["X-CSRF-Token"] = meta.content; + return cfg; + }); + window.api = api; + + const message = (err) => + err.response?.data?.message ?? "Verbindung fehlgeschlagen."; + + const showError = (form, text) => { + const el = form.querySelector("[data-error]"); + if (!el) return; + el.textContent = text; + el.classList.toggle("hidden", !text); + }; + + document.addEventListener("submit", async (ev) => { + const form = ev.target.closest("form[data-api]"); + if (!form) return; + ev.preventDefault(); + const button = form.querySelector("button[type=submit]"); + const body = Object.fromEntries( + [...new FormData(form)].filter(([k]) => { + const step = form.querySelector(`[name="${k}"]`)?.closest("[data-step]"); + return !step || !step.hidden; + }), + ); + const totpStep = form.querySelector('[data-step="totp"]'); + const url = totpStep && !totpStep.hidden ? `${form.dataset.api}/totp` : form.dataset.api; + + showError(form, ""); + button && (button.disabled = true); + try { + const res = await api.post(url, body); + if (res.data?.status === "totp_required" && totpStep) { + form.querySelector('[data-step="password"]').hidden = true; + totpStep.hidden = false; + totpStep.querySelector("input")?.focus(); + return; + } + location.assign(form.dataset.next || location.href); + } catch (err) { + showError(form, message(err)); + } finally { + button && (button.disabled = false); + } + }); + + document.addEventListener("click", async (ev) => { + if (ev.target.closest("[data-logout]")) { + await api.post("/auth/logout").catch(() => {}); + location.assign("/login"); + } + if (ev.target.closest("[data-totp-start]")) { + const root = ev.target.closest("[data-totp-setup]"); + try { + const { data } = await api.post("/me/totp/setup"); + root.querySelector("[data-totp-qr]").src = data.qr; + root.querySelector("[data-totp-secret]").textContent = data.secret; + root.querySelector("[data-totp-panel]").hidden = false; + ev.target.hidden = true; + } catch (err) { + alert(message(err)); + } + } + }); + + document.addEventListener("change", async (ev) => { + const sel = ev.target.closest("[data-switch-org]"); + if (!sel) return; + await api.post("/me/org", { org_id: sel.value }); + location.reload(); }); })(); diff --git a/internal/web/auth.templ b/internal/web/auth.templ new file mode 100644 index 0000000..912a2d7 --- /dev/null +++ b/internal/web/auth.templ @@ -0,0 +1,37 @@ +package web + +templ Login(p Page, signupAllowed bool) { + @authShell(p, "Anmelden") { +
+ @formError() +
+ @field("E-Mail", "email", "email", "username", true) + @field("Passwort", "password", "password", "current-password", true) +
+ + @submit("Weiter") +
+ if signupAllowed { +

Noch kein Konto? Registrieren

+ } + } +} + +templ Signup(p Page) { + @authShell(p, "Konto anlegen") { +
+ @formError() + @field("Name", "display_name", "text", "name", false) + @field("E-Mail", "email", "email", "username", true) + @field("Passwort (min. 12 Zeichen)", "password", "password", "new-password", true) +
+ @field("Organisation", "org_name", "text", "organization", true) + @field("Kurzname (a–z, 0–9, -)", "org_slug", "text", "off", true) + @submit("Anlegen") +
+

Zur Anmeldung

+ } +} diff --git a/internal/web/auth_templ.go b/internal/web/auth_templ.go new file mode 100644 index 0000000..3fac225 --- /dev/null +++ b/internal/web/auth_templ.go @@ -0,0 +1,176 @@ +// Code generated by templ - DO NOT EDIT. + +// templ: version: v0.3.1118 + +package web + +//lint:file-ignore SA4006 This context is only used if a nested component is present. + +import "github.com/a-h/templ" +import templruntime "github.com/a-h/templ/runtime" + +func Login(p Page, signupAllowed bool) templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { + return templ_7745c5c3_CtxErr + } + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + ctx = templ.ClearChildren(ctx) + templ_7745c5c3_Var2 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = formError().Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = field("E-Mail", "email", "email", "username", true).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = field("Passwort", "password", "password", "current-password", true).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = submit("Weiter").Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if signupAllowed { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "

Noch kein Konto? Registrieren

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + return nil + }) + templ_7745c5c3_Err = authShell(p, "Anmelden").Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return nil + }) +} + +func Signup(p Page) templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { + return templ_7745c5c3_CtxErr + } + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + ctx = templ.ClearChildren(ctx) + templ_7745c5c3_Var4 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = formError().Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = field("Name", "display_name", "text", "name", false).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = field("E-Mail", "email", "email", "username", true).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = field("Passwort (min. 12 Zeichen)", "password", "password", "new-password", true).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = field("Organisation", "org_name", "text", "organization", true).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = field("Kurzname (a–z, 0–9, -)", "org_slug", "text", "off", true).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = submit("Anlegen").Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "

Zur Anmeldung

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return nil + }) + templ_7745c5c3_Err = authShell(p, "Konto anlegen").Render(templ.WithChildren(ctx, templ_7745c5c3_Var4), templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return nil + }) +} + +var _ = templruntime.GeneratedTemplate diff --git a/internal/web/console.templ b/internal/web/console.templ new file mode 100644 index 0000000..5645c7b --- /dev/null +++ b/internal/web/console.templ @@ -0,0 +1,90 @@ +package web + +type Me struct { + Email string + DisplayName string + TOTP bool + ActiveOrg string + Orgs []Org + TOTPMissing bool +} + +type Org struct { + ID string + Name string + Slug string + IsOwner bool +} + +templ consoleShell(p Page, me Me) { + @Layout(p) { +
+
+ uptimesebi + if len(me.Orgs) > 1 { + + } else if len(me.Orgs) == 1 { + { me.Orgs[0].Name } + } + +
+
+ if me.TOTPMissing { +
+

+ Deine Organisation verlangt Zwei-Faktor-Anmeldung. + Jetzt einrichten +

+
+ } +
+ { children... } +
+ } +} + +templ Dashboard(p Page, me Me) { + @consoleShell(p, me) { +

Monitore

+

Noch keine Monitore angelegt.

+ } +} + +templ Security(p Page, me Me) { + @consoleShell(p, me) { +

Sicherheit

+
+

Zwei-Faktor (TOTP)

+ if me.TOTP { +

● aktiv

+
+ @formError() + @field("Passwort", "password", "password", "current-password", true) + @field("Aktueller Code", "code", "text", "one-time-code", true) + @submit("Deaktivieren") +
+ } else { +

● nicht eingerichtet

+
+ + +
+ } +
+ } +} diff --git a/internal/web/console_templ.go b/internal/web/console_templ.go new file mode 100644 index 0000000..0ff048f --- /dev/null +++ b/internal/web/console_templ.go @@ -0,0 +1,301 @@ +// Code generated by templ - DO NOT EDIT. + +// templ: version: v0.3.1118 + +package web + +//lint:file-ignore SA4006 This context is only used if a nested component is present. + +import "github.com/a-h/templ" +import templruntime "github.com/a-h/templ/runtime" + +type Me struct { + Email string + DisplayName string + TOTP bool + ActiveOrg string + Orgs []Org + TOTPMissing bool +} + +type Org struct { + ID string + Name string + Slug string + IsOwner bool +} + +func consoleShell(p Page, me Me) templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { + return templ_7745c5c3_CtxErr + } + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var1 := templ.GetChildren(ctx) + ctx = templ.ClearChildren(ctx) + templ_7745c5c3_Var2 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "
uptimesebi ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if len(me.Orgs) > 1 { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } else if len(me.Orgs) == 1 { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var5 string + templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(me.Orgs[0].Name) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/console.templ`, Line: 31, Col: 47} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if me.TOTPMissing { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "

Deine Organisation verlangt Zwei-Faktor-Anmeldung. Jetzt einrichten

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templ_7745c5c3_Var1.Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 14, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return nil + }) + templ_7745c5c3_Err = Layout(p).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return nil + }) +} + +func Dashboard(p Page, me Me) templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { + return templ_7745c5c3_CtxErr + } + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + ctx = templ.ClearChildren(ctx) + templ_7745c5c3_Var7 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 15, "

Monitore

Noch keine Monitore angelegt.

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return nil + }) + templ_7745c5c3_Err = consoleShell(p, me).Render(templ.WithChildren(ctx, templ_7745c5c3_Var7), templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return nil + }) +} + +func Security(p Page, me Me) templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { + return templ_7745c5c3_CtxErr + } + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + ctx = templ.ClearChildren(ctx) + templ_7745c5c3_Var9 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 16, "

Sicherheit

Zwei-Faktor (TOTP)

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if me.TOTP { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 17, "

● aktiv

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = formError().Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = field("Passwort", "password", "password", "current-password", true).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = field("Aktueller Code", "code", "text", "one-time-code", true).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = submit("Deaktivieren").Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 18, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } else { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 19, "

● nicht eingerichtet

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 21, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return nil + }) + templ_7745c5c3_Err = consoleShell(p, me).Render(templ.WithChildren(ctx, templ_7745c5c3_Var9), templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return nil + }) +} + +var _ = templruntime.GeneratedTemplate diff --git a/internal/web/index.templ b/internal/web/index.templ deleted file mode 100644 index e8d5885..0000000 --- a/internal/web/index.templ +++ /dev/null @@ -1,9 +0,0 @@ -package web - -templ Index() { - @Layout("uptimesebi") { -
-

uptimesebi

-
- } -} diff --git a/internal/web/index_templ.go b/internal/web/index_templ.go deleted file mode 100644 index e4f212e..0000000 --- a/internal/web/index_templ.go +++ /dev/null @@ -1,55 +0,0 @@ -// Code generated by templ - DO NOT EDIT. - -// templ: version: v0.3.1118 - -package web - -//lint:file-ignore SA4006 This context is only used if a nested component is present. - -import "github.com/a-h/templ" -import templruntime "github.com/a-h/templ/runtime" - -func Index() templ.Component { - return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { - templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context - if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { - return templ_7745c5c3_CtxErr - } - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) - if !templ_7745c5c3_IsBuffer { - defer func() { - templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) - if templ_7745c5c3_Err == nil { - templ_7745c5c3_Err = templ_7745c5c3_BufErr - } - }() - } - ctx = templ.InitializeContext(ctx) - ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Var2 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { - templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) - if !templ_7745c5c3_IsBuffer { - defer func() { - templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) - if templ_7745c5c3_Err == nil { - templ_7745c5c3_Err = templ_7745c5c3_BufErr - } - }() - } - ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "

uptimesebi

") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - return nil - }) - templ_7745c5c3_Err = Layout("uptimesebi").Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - return nil - }) -} - -var _ = templruntime.GeneratedTemplate diff --git a/internal/web/layout.templ b/internal/web/layout.templ index 0a74f49..14e93e0 100644 --- a/internal/web/layout.templ +++ b/internal/web/layout.templ @@ -1,12 +1,20 @@ package web -templ Layout(title string) { +type Page struct { + Title string + CSRF string +} + +templ Layout(p Page) { - { title } + if p.CSRF != "" { + + } + { p.Title } · uptimesebi @@ -16,3 +24,36 @@ templ Layout(title string) { } + +templ authShell(p Page, heading string) { + @Layout(p) { +
+

uptimesebi

+

{ heading }

+ { children... } +
+ } +} + +templ field(label, name, typ, autocomplete string, required bool) { + +} + +templ submit(label string) { + +} + +templ formError() { + +} diff --git a/internal/web/layout_templ.go b/internal/web/layout_templ.go index 63ee121..da45854 100644 --- a/internal/web/layout_templ.go +++ b/internal/web/layout_templ.go @@ -9,7 +9,12 @@ package web import "github.com/a-h/templ" import templruntime "github.com/a-h/templ/runtime" -func Layout(title string) templ.Component { +type Page struct { + Title string + CSRF string +} + +func Layout(p Page) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { @@ -27,20 +32,43 @@ func Layout(title string) templ.Component { ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<!doctype html><html lang=\"de\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var2 string - templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(title) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/layout.templ`, Line: 9, Col: 17} + if p.CSRF != "" { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "<meta name=\"csrf-token\" content=\"") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var2 string + templ_7745c5c3_Var2, templ_7745c5c3_Err = templruntime.NewAttributeExpression(p.CSRF).Resolve(ctx) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/layout.templ`, Line: 15, Col: 44} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var2) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\">") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "<title>") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "") + var templ_7745c5c3_Var3 string + templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(p.Title) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/layout.templ`, Line: 17, Col: 19} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, " · uptimesebi") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -48,7 +76,222 @@ func Layout(title string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return nil + }) +} + +func authShell(p Page, heading string) templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { + return templ_7745c5c3_CtxErr + } + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var4 := templ.GetChildren(ctx) + ctx = templ.ClearChildren(ctx) + templ_7745c5c3_Var5 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "

uptimesebi

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var6 string + templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(heading) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/layout.templ`, Line: 32, Col: 51} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templ_7745c5c3_Var4.Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return nil + }) + templ_7745c5c3_Err = Layout(p).Render(templ.WithChildren(ctx, templ_7745c5c3_Var5), templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return nil + }) +} + +func field(label, name, typ, autocomplete string, required bool) templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { + return templ_7745c5c3_CtxErr + } + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + ctx = templ.ClearChildren(ctx) + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return nil + }) +} + +func submit(label string) templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { + return templ_7745c5c3_CtxErr + } + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + ctx = templ.ClearChildren(ctx) + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 17, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return nil + }) +} + +func formError() templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { + return templ_7745c5c3_CtxErr + } + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + ctx = templ.ClearChildren(ctx) + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 19, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err }