diff --git a/Readme.md b/Readme.md index 88dd723..5c234c2 100644 --- a/Readme.md +++ b/Readme.md @@ -7,3 +7,9 @@ |------|-------------|---------| | USERSTORE_SQLITE_PATH | Path to the Sqlite Database | `none` | | PORT | Port for the Webserver | 8080 | +| SMTP_SERVER | SMTP Server to send mails | `none` | +| SMTP_PORT | SMTP Port | 587 | +| SMTP_USERNAME | SMTP Username | `none` | +| SMTP_PASSWORD | SMTP Password | `none` | +| SMTP_FROM | Mail address used as from for all mails | `none` | +| WEB_PUBLIC_REGISTRATION | Enabled the registration for anyone on the webgui | False | diff --git a/main.go b/main.go index 51373e7..e0a6325 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "fmt" "html/template" + "git.keks.cloud/kekskurse/miniauth/pkg/smtpclient" "git.keks.cloud/kekskurse/miniauth/pkg/userstore" "git.keks.cloud/kekskurse/miniauth/pkg/web" "github.com/gin-gonic/gin" @@ -18,13 +19,15 @@ var templatesFS embed.FS func main() { cnf := config() - router := setupRouter() + router := setupRouter(cnf) router.Run(fmt.Sprintf(":%v", cnf.Port)) } type gloableConfig struct { - UserStoreConfig userstore.Config `env:", prefix=USERSTORE_"` - Port int `env:"PORT, default=8080"` + UserStoreConfig userstore.Config `env:", prefix=USERSTORE_"` + Port int `env:"PORT, default=8080"` + SMTPConfig smtpclient.SMTPConfig `env:", prefix=SMTP_"` + WebConfig web.WebConfig `env:", prefix=WEB_"` } func config() gloableConfig { @@ -44,12 +47,12 @@ func loadTemplates() *template.Template { return tmpl } -func setupRouter() *gin.Engine { +func setupRouter(cfg gloableConfig) *gin.Engine { router := gin.Default() router.SetHTMLTemplate(loadTemplates()) routesWeb := router.Group("/web") - webServer := web.NewWeb() + webServer := web.NewWeb(cfg.WebConfig) webServer.RegisterRoutes(routesWeb) router.GET("/", func(c *gin.Context) {}) diff --git a/main_test.go b/main_test.go index 7dcc93c..91ac37c 100644 --- a/main_test.go +++ b/main_test.go @@ -9,7 +9,8 @@ import ( ) func TestStartpageEmpty(t *testing.T) { - router := setupRouter() + gc := gloableConfig{} + router := setupRouter(gc) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/", nil) router.ServeHTTP(w, req) @@ -19,7 +20,8 @@ func TestStartpageEmpty(t *testing.T) { } func TestPingRoute(t *testing.T) { - router := setupRouter() + gc := gloableConfig{} + router := setupRouter(gc) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/ping", nil) router.ServeHTTP(w, req) diff --git a/pkg/smtpclient/smtpclient.go b/pkg/smtpclient/smtpclient.go index e390d62..97ca453 100644 --- a/pkg/smtpclient/smtpclient.go +++ b/pkg/smtpclient/smtpclient.go @@ -13,11 +13,11 @@ import ( ) type SMTPConfig struct { - Server string - Port int - Username string - Password string - From string + Server string `env:"SERVER"` + Port int `env:"PORT, default=587"` + Username string `env:"USERNAME"` + Password string `env:"PASSWORD"` + From string `env:"FROM"` } type SMTPClient struct { diff --git a/pkg/web/web.go b/pkg/web/web.go index 9958149..c4bb1eb 100644 --- a/pkg/web/web.go +++ b/pkg/web/web.go @@ -6,10 +6,17 @@ import ( "github.com/gin-gonic/gin" ) -type Web struct{} +type WebConfig struct { + PublicRegistration bool `env:"PUBLIC_REGISTRATION, default=0"` +} -func NewWeb() Web { +type Web struct { + config WebConfig +} + +func NewWeb(config WebConfig) Web { w := Web{} + w.config = config return w } @@ -19,5 +26,9 @@ func (w Web) RegisterRoutes(routing *gin.RouterGroup) error { } func (w Web) GetRegisterPage(c *gin.Context) { + if !w.config.PublicRegistration { + c.HTML(403, "msg.html", gin.H{"msg": "Public registration disabled"}) + return + } c.HTML(http.StatusOK, "register.html", nil) } diff --git a/pkg/web/web_test.go b/pkg/web/web_test.go new file mode 100644 index 0000000..2b40287 --- /dev/null +++ b/pkg/web/web_test.go @@ -0,0 +1,39 @@ +package web + +import ( + "html/template" + "net/http" + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/assert" +) + +func TestRegistrationDisabled(t *testing.T) { + webConfig := WebConfig{} + webConfig.PublicRegistration = false + + web := NewWeb(webConfig) + + router := gin.New() + loadTemplates(router) + grpup := router.Group("/web") + web.RegisterRoutes(grpup) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/web/register", nil) + router.ServeHTTP(w, req) + + assert.Equal(t, 403, w.Code) + content := string(w.Body.Bytes()) + assert.Contains(t, content, "Public registration disabled") +} + +func loadTemplates(r *gin.Engine) { + tmpl, err := template.ParseGlob("../../templates/*.html") + if err != nil { + panic(err) // Handle Fehler entsprechend + } + r.SetHTMLTemplate(tmpl) +} diff --git a/templates/msg.html b/templates/msg.html new file mode 100644 index 0000000..3b6fb97 --- /dev/null +++ b/templates/msg.html @@ -0,0 +1,17 @@ + +<!DOCTYPE html> +<html lang="de"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Message</title> + <script src="https://cdn.tailwindcss.com"></script> +</head> +<body class="bg-gray-100 flex items-center justify-center min-h-screen"> + <div class="bg-white p-8 shadow-xl w-full max-w-md"> + <h2 class="text-2xl font-semibold text-gray-900 text-center mb-6">Message</h2> + <p>{{ .msg }}</p> + </div> +</body> +</html> +