Support basic auth for the http endpoint

This commit is contained in:
Thorben Günther 2022-10-10 14:04:51 +02:00
parent 93d004925b
commit 5cf4add40b
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED
2 changed files with 29 additions and 0 deletions

View file

@ -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")

13
main.go
View file

@ -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 {