diff options
author | Matthew Holt <[email protected]> | 2015-05-04 11:04:17 -0600 |
---|---|---|
committer | Matthew Holt <[email protected]> | 2015-05-04 11:04:37 -0600 |
commit | 6029973bdc9314ae32e59a91bc49199c5b865365 (patch) | |
tree | 75977b921b518f4fe37a9032d029c65603f6316d /config/setup/basicauth.go | |
parent | 995edf05661c42399bb1469a657860e2ec8e7bf7 (diff) | |
download | caddy-6029973bdc9314ae32e59a91bc49199c5b865365.tar.gz caddy-6029973bdc9314ae32e59a91bc49199c5b865365.zip |
Major refactoring of middleware and parser in progress
Diffstat (limited to 'config/setup/basicauth.go')
-rw-r--r-- | config/setup/basicauth.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/config/setup/basicauth.go b/config/setup/basicauth.go new file mode 100644 index 000000000..6d1ece108 --- /dev/null +++ b/config/setup/basicauth.go @@ -0,0 +1,53 @@ +package setup + +import ( + "github.com/mholt/caddy/middleware" + "github.com/mholt/caddy/middleware/basicauth" +) + +// BasicAuth configures a new BasicAuth middleware instance. +func BasicAuth(c *Controller) (middleware.Middleware, error) { + rules, err := basicAuthParse(c) + if err != nil { + return nil, err + } + + basic := basicauth.BasicAuth{Rules: rules} + + return func(next middleware.Handler) middleware.Handler { + basic.Next = next + return basic + }, nil +} + +func basicAuthParse(c *Controller) ([]basicauth.Rule, error) { + var rules []basicauth.Rule + + for c.Next() { + var rule basicauth.Rule + + args := c.RemainingArgs() + + switch len(args) { + case 2: + rule.Username = args[0] + rule.Password = args[1] + for c.NextBlock() { + rule.Resources = append(rule.Resources, c.Val()) + if c.NextArg() { + return rules, c.Errf("Expecting only one resource per line (extra '%s')", c.Val()) + } + } + case 3: + rule.Resources = append(rule.Resources, args[0]) + rule.Username = args[1] + rule.Password = args[2] + default: + return rules, c.ArgErr() + } + + rules = append(rules, rule) + } + + return rules, nil +} |