Skip to content

Commit

Permalink
feat(triggers): basic authentication (Cloudbox#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-rots authored Jul 28, 2020
1 parent ff94f00 commit e4d676b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
29 changes: 26 additions & 3 deletions cmd/autoscan/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,31 @@ import (
"github.com/cloudbox/autoscan"
"github.com/cloudbox/autoscan/processor"
"github.com/cloudbox/autoscan/targets/plex"
"github.com/cloudbox/autoscan/triggers"
"github.com/cloudbox/autoscan/triggers/radarr"
"github.com/cloudbox/autoscan/triggers/sonarr"
"github.com/natefinch/lumberjack"
)

type config struct {
// General configuration
Port int `yaml:"port"`
MaxRetries int `yaml:"retries"`
Anchors []string `yaml:"anchors"`
Triggers struct {

// Authentication for autoscan.HTTPTrigger
Auth struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
} `yaml:"authentication"`

// autoscan.HTTPTrigger
Triggers struct {
Radarr []radarr.Config `yaml:"radarr"`
Sonarr []sonarr.Config `yaml:"sonarr"`
} `yaml:"triggers"`

// autoscan.Target
Targets struct {
Plex []plex.Config `yaml:"plex"`
} `yaml:"targets"`
Expand Down Expand Up @@ -151,6 +163,13 @@ func main() {
Msg("Failed initialising processor")
}

// Set authentication. If none and running at least one webhook -> warn user.
authHandler := triggers.WithAuth(c.Auth.Username, c.Auth.Password)
if (c.Auth.Username == "" || c.Auth.Password == "") &&
len(c.Triggers.Radarr)+len(c.Triggers.Sonarr) > 0 {
log.Warn().Msg("Webhooks running without authentication")
}

// triggers
for _, t := range c.Triggers.Radarr {
trigger, err := radarr.New(t)
Expand All @@ -160,7 +179,9 @@ func main() {
Str("trigger", t.Name).
Msg("Failed initialising trigger")
}
mux.Handle("/triggers/"+t.Name, trigger(proc.Add))

logHandler := triggers.WithLogger(autoscan.GetLogger(t.Verbosity))
mux.Handle("/triggers/"+t.Name, logHandler(authHandler(trigger(proc.Add))))
}

for _, t := range c.Triggers.Sonarr {
Expand All @@ -171,7 +192,9 @@ func main() {
Str("trigger", t.Name).
Msg("Failed initialising trigger")
}
mux.Handle("/triggers/"+t.Name, trigger(proc.Add))

logHandler := triggers.WithLogger(autoscan.GetLogger(t.Verbosity))
mux.Handle("/triggers/"+t.Name, logHandler(authHandler(trigger(proc.Add))))
}

go func() {
Expand Down
23 changes: 23 additions & 0 deletions triggers/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,26 @@ func WithLogger(logger zerolog.Logger) func(http.Handler) http.Handler {
return c.Then(next)
}
}

func WithAuth(username, password string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
// Don't check for auth if username or password is missing.
if username == "" || password == "" {
return next
}

return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
l := hlog.FromRequest(r)

user, pass, ok := r.BasicAuth()
if ok && user == username && pass == password {
l.Trace().Msg("Successful authentication")
next.ServeHTTP(rw, r)
return
}

l.Warn().Msg("Invalid authentication")
rw.WriteHeader(http.StatusUnauthorized)
})
}
}
11 changes: 3 additions & 8 deletions triggers/radarr/radarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import (
"path"
"strconv"

"github.com/rs/zerolog/hlog"

"github.com/cloudbox/autoscan"
"github.com/cloudbox/autoscan/triggers"
"github.com/rs/zerolog/hlog"
)

type Config struct {
Expand All @@ -27,15 +25,12 @@ func New(c Config) (autoscan.HTTPTrigger, error) {
return nil, err
}

log := autoscan.GetLogger(c.Verbosity)
logHandler := triggers.WithLogger(log)

trigger := func(callback autoscan.ProcessorFunc) http.Handler {
return logHandler(handler{
return handler{
callback: callback,
priority: c.Priority,
rewrite: rewriter,
})
}
}

return trigger, nil
Expand Down
8 changes: 2 additions & 6 deletions triggers/sonarr/sonarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strconv"

"github.com/cloudbox/autoscan"
"github.com/cloudbox/autoscan/triggers"
"github.com/rs/zerolog/hlog"
)

Expand All @@ -26,15 +25,12 @@ func New(c Config) (autoscan.HTTPTrigger, error) {
return nil, err
}

log := autoscan.GetLogger(c.Verbosity)
logHandler := triggers.WithLogger(log)

trigger := func(callback autoscan.ProcessorFunc) http.Handler {
return logHandler(handler{
return handler{
callback: callback,
priority: c.Priority,
rewrite: rewriter,
})
}
}

return trigger, nil
Expand Down

0 comments on commit e4d676b

Please sign in to comment.