Support basic auth for the http endpoint
This commit is contained in:
parent
93d004925b
commit
5cf4add40b
2 changed files with 29 additions and 0 deletions
16
config.go
16
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")
|
||||
|
|
13
main.go
13
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 {
|
||||
|
|
Loading…
Reference in a new issue