From 5cf4add40be66ff63afc66a1f0339153532d433a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Mon, 10 Oct 2022 14:04:51 +0200 Subject: [PATCH] Support basic auth for the http endpoint --- config.go | 16 ++++++++++++++++ main.go | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/config.go b/config.go index 7f9032e..3108a24 100644 --- a/config.go +++ b/config.go @@ -9,6 +9,8 @@ import ( type config struct { HTTPAddress string LogLevel string + User string + Password string ntfy ntfyConfig } @@ -43,6 +45,20 @@ func readConfig(path string) (*config, error) { } } + d = cfg.Get("user") + if d != nil { + if err := d.ParseParams(&config.User); err != nil { + return nil, err + } + } + + d = cfg.Get("password") + if d != nil { + if err := d.ParseParams(&config.Password); err != nil { + return nil, err + } + } + ntfyDir := cfg.Get("ntfy") if ntfyDir == nil { return nil, fmt.Errorf("%q directive missing", "ntfy") diff --git a/main.go b/main.go index 2810ef2..e0094de 100644 --- a/main.go +++ b/main.go @@ -34,6 +34,19 @@ type alert struct { } func (rcv *receiver) handleWebhooks(w http.ResponseWriter, r *http.Request) { + if rcv.cfg.User != "" && rcv.cfg.Password != "" { + user, pass, ok := r.BasicAuth() + if !ok { + rcv.logger.Error("basic auth failure") + return + } + + if user != rcv.cfg.User || pass != rcv.cfg.Password { + rcv.logger.Info("basic auth: wrong user or password") + return + } + } + defer r.Body.Close() if r.Method != http.MethodPost {