caddy/middleware/basicauth/basicauth.go

70 lines
1.7 KiB
Go
Raw Normal View History

2015-04-28 00:56:57 +08:00
// Package basicauth implements HTTP Basic Authentication.
2015-04-24 04:57:07 +08:00
package basicauth
import (
2015-05-30 13:08:01 +08:00
"crypto/subtle"
2015-04-24 04:57:07 +08:00
"net/http"
"github.com/mholt/caddy/middleware"
)
// BasicAuth is middleware to protect resources with a username and password.
// Note that HTTP Basic Authentication is not secure by itself and should
// not be used to protect important assets without HTTPS. Even then, the
// security of HTTP Basic Auth is disputed. Use discretion when deciding
// what to protect with BasicAuth.
type BasicAuth struct {
Next middleware.Handler
Rules []Rule
2015-04-24 04:57:07 +08:00
}
// ServeHTTP implements the middleware.Handler interface.
func (a BasicAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
2015-05-10 14:20:58 +08:00
var hasAuth bool
var isAuthenticated bool
2015-04-24 04:57:07 +08:00
for _, rule := range a.Rules {
for _, res := range rule.Resources {
if !middleware.Path(r.URL.Path).Matches(res) {
continue
}
// Path matches; parse auth header
username, password, ok := r.BasicAuth()
2015-05-10 14:20:58 +08:00
hasAuth = true
2015-04-24 04:57:07 +08:00
// Check credentials
2015-05-30 13:08:01 +08:00
if !ok ||
username != rule.Username ||
subtle.ConstantTimeCompare([]byte(password), []byte(rule.Password)) != 1 {
2015-05-10 14:20:58 +08:00
continue
2015-04-24 04:57:07 +08:00
}
2015-05-30 13:08:01 +08:00
// Flag set only on successful authentication
2015-05-10 14:20:58 +08:00
isAuthenticated = true
}
}
2015-05-25 10:52:34 +08:00
2015-05-10 14:20:58 +08:00
if hasAuth {
if !isAuthenticated {
w.Header().Set("WWW-Authenticate", "Basic")
return http.StatusUnauthorized, nil
2015-04-24 04:57:07 +08:00
}
2015-05-25 10:52:34 +08:00
// "It's an older code, sir, but it checks out. I was about to clear them."
return a.Next.ServeHTTP(w, r)
2015-04-24 04:57:07 +08:00
}
// Pass-thru when no paths match
return a.Next.ServeHTTP(w, r)
}
// Rule represents a BasicAuth rule. A username and password
// combination protect the associated resources, which are
// file or directory paths.
type Rule struct {
Username string
Password string
Resources []string
}