2015-04-27 11:56:57 -05:00
|
|
|
// Package basicauth implements HTTP Basic Authentication.
|
2015-04-23 15:57:07 -05:00
|
|
|
package basicauth
|
|
|
|
|
|
|
|
import (
|
2015-08-30 13:07:43 -05:00
|
|
|
"bufio"
|
2015-05-30 00:08:01 -05:00
|
|
|
"crypto/subtle"
|
2015-08-30 13:07:43 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-04-23 15:57:07 -05:00
|
|
|
"net/http"
|
2015-08-30 13:07:43 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2015-04-23 15:57:07 -05:00
|
|
|
|
2015-08-30 13:07:43 -05:00
|
|
|
"github.com/jimstudt/http-authentication/basic"
|
2015-04-23 15:57:07 -05:00
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
)
|
|
|
|
|
2015-05-04 07:53:54 -05:00
|
|
|
// 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 {
|
2015-09-05 17:04:30 -05:00
|
|
|
Next middleware.Handler
|
|
|
|
SiteRoot string
|
|
|
|
Rules []Rule
|
2015-04-23 15:57:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP implements the middleware.Handler interface.
|
|
|
|
func (a BasicAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
2015-05-10 01:20:58 -05:00
|
|
|
|
|
|
|
var hasAuth bool
|
|
|
|
var isAuthenticated bool
|
|
|
|
|
2015-04-23 15:57:07 -05: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 01:20:58 -05:00
|
|
|
hasAuth = true
|
2015-04-23 15:57:07 -05:00
|
|
|
|
|
|
|
// Check credentials
|
2015-05-30 00:08:01 -05:00
|
|
|
if !ok ||
|
|
|
|
username != rule.Username ||
|
2015-08-30 13:07:43 -05:00
|
|
|
!rule.Password(password) {
|
|
|
|
//subtle.ConstantTimeCompare([]byte(password), []byte(rule.Password)) != 1 {
|
2015-05-10 01:20:58 -05:00
|
|
|
continue
|
2015-04-23 15:57:07 -05:00
|
|
|
}
|
2015-05-30 00:08:01 -05:00
|
|
|
|
|
|
|
// Flag set only on successful authentication
|
2015-05-10 01:20:58 -05:00
|
|
|
isAuthenticated = true
|
|
|
|
}
|
|
|
|
}
|
2015-05-24 21:52:34 -05:00
|
|
|
|
2015-05-10 01:20:58 -05:00
|
|
|
if hasAuth {
|
|
|
|
if !isAuthenticated {
|
|
|
|
w.Header().Set("WWW-Authenticate", "Basic")
|
|
|
|
return http.StatusUnauthorized, nil
|
2015-04-23 15:57:07 -05:00
|
|
|
}
|
2015-05-24 21:52:34 -05: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-23 15:57:07 -05: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
|
2015-08-30 13:07:43 -05:00
|
|
|
Password func(string) bool
|
2015-04-23 15:57:07 -05:00
|
|
|
Resources []string
|
|
|
|
}
|
2015-08-30 13:07:43 -05:00
|
|
|
|
2015-11-02 14:28:50 -05:00
|
|
|
// PasswordMatcher determines whether a password matches a rule.
|
2015-08-30 13:07:43 -05:00
|
|
|
type PasswordMatcher func(pw string) bool
|
|
|
|
|
|
|
|
var (
|
|
|
|
htpasswords map[string]map[string]PasswordMatcher
|
|
|
|
htpasswordsMu sync.Mutex
|
|
|
|
)
|
|
|
|
|
2015-11-02 14:28:50 -05:00
|
|
|
// GetHtpasswdMatcher matches password rules.
|
2015-09-05 17:04:30 -05:00
|
|
|
func GetHtpasswdMatcher(filename, username, siteRoot string) (PasswordMatcher, error) {
|
|
|
|
filename = filepath.Join(siteRoot, filename)
|
2015-08-30 13:07:43 -05:00
|
|
|
htpasswordsMu.Lock()
|
|
|
|
if htpasswords == nil {
|
|
|
|
htpasswords = make(map[string]map[string]PasswordMatcher)
|
|
|
|
}
|
|
|
|
pm := htpasswords[filename]
|
|
|
|
if pm == nil {
|
|
|
|
fh, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("open %q: %v", filename, err)
|
|
|
|
}
|
|
|
|
defer fh.Close()
|
|
|
|
pm = make(map[string]PasswordMatcher)
|
|
|
|
if err = parseHtpasswd(pm, fh); err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing htpasswd %q: %v", fh.Name(), err)
|
|
|
|
}
|
|
|
|
htpasswords[filename] = pm
|
|
|
|
}
|
|
|
|
htpasswordsMu.Unlock()
|
|
|
|
if pm[username] == nil {
|
|
|
|
return nil, fmt.Errorf("username %q not found in %q", username, filename)
|
|
|
|
}
|
|
|
|
return pm[username], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseHtpasswd(pm map[string]PasswordMatcher, r io.Reader) error {
|
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := strings.TrimSpace(scanner.Text())
|
|
|
|
if line == "" || strings.IndexByte(line, '#') == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
i := strings.IndexByte(line, ':')
|
|
|
|
if i <= 0 {
|
|
|
|
return fmt.Errorf("malformed line, no color: %q", line)
|
|
|
|
}
|
|
|
|
user, encoded := line[:i], line[i+1:]
|
|
|
|
for _, p := range basic.DefaultSystems {
|
|
|
|
matcher, err := p(encoded)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if matcher != nil {
|
|
|
|
pm[user] = matcher.MatchesPassword
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return scanner.Err()
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:35:34 -05:00
|
|
|
// PlainMatcher returns a PasswordMatcher that does a constant-time
|
|
|
|
// byte-wise comparison.
|
2015-08-30 13:07:43 -05:00
|
|
|
func PlainMatcher(passw string) PasswordMatcher {
|
|
|
|
return func(pw string) bool {
|
|
|
|
return subtle.ConstantTimeCompare([]byte(pw), []byte(passw)) == 1
|
|
|
|
}
|
|
|
|
}
|