2017-09-23 00:56:58 -05:00
|
|
|
// Copyright 2015 Light Code Labs, LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-07-05 13:49:25 -05:00
|
|
|
// Package basicauth implements HTTP Basic Authentication for Caddy.
|
|
|
|
//
|
|
|
|
// This is useful for simple protections on a website, like requiring
|
|
|
|
// a password to access an admin interface. This package assumes a
|
|
|
|
// fairly small threat model.
|
2016-06-05 22:51:56 -05:00
|
|
|
package basicauth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2017-02-17 17:37:58 -05:00
|
|
|
"context"
|
2016-07-05 13:49:25 -05:00
|
|
|
"crypto/sha1"
|
2016-06-05 22:51:56 -05:00
|
|
|
"crypto/subtle"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/jimstudt/http-authentication/basic"
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 httpserver.Handler
|
|
|
|
SiteRoot string
|
|
|
|
Rules []Rule
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP implements the httpserver.Handler interface.
|
|
|
|
func (a BasicAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-01-03 19:40:07 -05:00
|
|
|
var protected, isAuthenticated bool
|
2017-03-09 15:20:14 -05:00
|
|
|
var realm string
|
2016-06-05 22:51:56 -05:00
|
|
|
|
|
|
|
for _, rule := range a.Rules {
|
|
|
|
for _, res := range rule.Resources {
|
|
|
|
if !httpserver.Path(r.URL.Path).Matches(res) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:40:07 -05:00
|
|
|
// path matches; this endpoint is protected
|
|
|
|
protected = true
|
2017-03-09 15:20:14 -05:00
|
|
|
realm = rule.Realm
|
2017-01-03 19:40:07 -05:00
|
|
|
|
|
|
|
// parse auth header
|
2016-06-05 22:51:56 -05:00
|
|
|
username, password, ok := r.BasicAuth()
|
|
|
|
|
2017-01-03 19:40:07 -05:00
|
|
|
// check credentials
|
2016-06-05 22:51:56 -05:00
|
|
|
if !ok ||
|
|
|
|
username != rule.Username ||
|
|
|
|
!rule.Password(password) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:40:07 -05:00
|
|
|
// by this point, authentication was successful
|
2016-06-05 22:51:56 -05:00
|
|
|
isAuthenticated = true
|
2017-01-03 19:40:07 -05:00
|
|
|
|
2017-03-13 10:22:46 -05:00
|
|
|
// let upstream middleware (e.g. fastcgi and cgi) know about authenticated
|
|
|
|
// user; this replaces the request with a wrapped instance
|
|
|
|
r = r.WithContext(context.WithValue(r.Context(),
|
|
|
|
httpserver.RemoteUserCtxKey, username))
|
2018-01-07 09:44:49 -05:00
|
|
|
|
|
|
|
// Provide username to be used in log by replacer
|
|
|
|
repl := httpserver.NewReplacer(r, nil, "-")
|
|
|
|
repl.Set("user", username)
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:40:07 -05:00
|
|
|
if protected && !isAuthenticated {
|
|
|
|
// browsers show a message that says something like:
|
|
|
|
// "The website says: <realm>"
|
|
|
|
// which is kinda dumb, but whatever.
|
2017-03-09 15:20:14 -05:00
|
|
|
if realm == "" {
|
|
|
|
realm = "Restricted"
|
|
|
|
}
|
|
|
|
w.Header().Set("WWW-Authenticate", "Basic realm=\""+realm+"\"")
|
2017-01-03 19:40:07 -05:00
|
|
|
return http.StatusUnauthorized, nil
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
|
2016-09-05 11:20:34 -05:00
|
|
|
// Pass-through when no paths match
|
2016-06-05 22:51:56 -05:00
|
|
|
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 func(string) bool
|
|
|
|
Resources []string
|
2017-03-09 15:20:14 -05:00
|
|
|
Realm string // See RFC 1945 and RFC 2617, default: "Restricted"
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PasswordMatcher determines whether a password matches a rule.
|
|
|
|
type PasswordMatcher func(pw string) bool
|
|
|
|
|
|
|
|
var (
|
|
|
|
htpasswords map[string]map[string]PasswordMatcher
|
|
|
|
htpasswordsMu sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetHtpasswdMatcher matches password rules.
|
|
|
|
func GetHtpasswdMatcher(filename, username, siteRoot string) (PasswordMatcher, error) {
|
|
|
|
filename = filepath.Join(siteRoot, filename)
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PlainMatcher returns a PasswordMatcher that does a constant-time
|
2016-07-05 13:49:25 -05:00
|
|
|
// byte comparison against the password passw.
|
2016-06-05 22:51:56 -05:00
|
|
|
func PlainMatcher(passw string) PasswordMatcher {
|
2016-07-05 13:49:25 -05:00
|
|
|
// compare hashes of equal length instead of actual password
|
|
|
|
// to avoid leaking password length
|
|
|
|
passwHash := sha1.New()
|
|
|
|
passwHash.Write([]byte(passw))
|
|
|
|
passwSum := passwHash.Sum(nil)
|
2016-06-05 22:51:56 -05:00
|
|
|
return func(pw string) bool {
|
2016-07-05 13:49:25 -05:00
|
|
|
pwHash := sha1.New()
|
|
|
|
pwHash.Write([]byte(pw))
|
|
|
|
pwSum := pwHash.Sum(nil)
|
|
|
|
return subtle.ConstantTimeCompare([]byte(pwSum), []byte(passwSum)) == 1
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
}
|