2016-06-05 22:51:56 -05:00
|
|
|
package basicauth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBasicAuth(t *testing.T) {
|
2017-02-17 17:37:58 -05:00
|
|
|
var i int
|
|
|
|
// This handler is registered for tests in which the only authorized user is
|
|
|
|
// "okuser"
|
|
|
|
upstreamHandler := func(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
remoteUser, _ := r.Context().Value(httpserver.CtxKey("remote_user")).(string)
|
|
|
|
if remoteUser != "okuser" {
|
|
|
|
t.Errorf("Test %d: expecting remote user 'okuser', got '%s'", i, remoteUser)
|
|
|
|
}
|
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
2016-06-05 22:51:56 -05:00
|
|
|
rw := BasicAuth{
|
2017-02-17 17:37:58 -05:00
|
|
|
Next: httpserver.HandlerFunc(upstreamHandler),
|
2016-06-05 22:51:56 -05:00
|
|
|
Rules: []Rule{
|
2017-01-03 19:40:07 -05:00
|
|
|
{Username: "okuser", Password: PlainMatcher("okpass"), Resources: []string{"/testing"}},
|
2016-06-05 22:51:56 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-02-17 17:37:58 -05:00
|
|
|
type testType struct {
|
2017-01-03 19:40:07 -05:00
|
|
|
from string
|
|
|
|
result int
|
|
|
|
user string
|
|
|
|
password string
|
2017-02-17 17:37:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []testType{
|
2017-01-03 19:40:07 -05:00
|
|
|
{"/testing", http.StatusOK, "okuser", "okpass"},
|
|
|
|
{"/testing", http.StatusUnauthorized, "baduser", "okpass"},
|
|
|
|
{"/testing", http.StatusUnauthorized, "okuser", "badpass"},
|
|
|
|
{"/testing", http.StatusUnauthorized, "OKuser", "okpass"},
|
|
|
|
{"/testing", http.StatusUnauthorized, "OKuser", "badPASS"},
|
|
|
|
{"/testing", http.StatusUnauthorized, "", "okpass"},
|
|
|
|
{"/testing", http.StatusUnauthorized, "okuser", ""},
|
|
|
|
{"/testing", http.StatusUnauthorized, "", ""},
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
|
2017-02-17 17:37:58 -05:00
|
|
|
var test testType
|
|
|
|
for i, test = range tests {
|
2016-06-05 22:51:56 -05:00
|
|
|
req, err := http.NewRequest("GET", test.from, nil)
|
|
|
|
if err != nil {
|
2017-01-03 19:40:07 -05:00
|
|
|
t.Fatalf("Test %d: Could not create HTTP request: %v", i, err)
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
2017-01-03 19:40:07 -05:00
|
|
|
req.SetBasicAuth(test.user, test.password)
|
2016-06-05 22:51:56 -05:00
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
result, err := rw.ServeHTTP(rec, req)
|
|
|
|
if err != nil {
|
2017-01-03 19:40:07 -05:00
|
|
|
t.Fatalf("Test %d: Could not ServeHTTP: %v", i, err)
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
if result != test.result {
|
2017-01-03 19:40:07 -05:00
|
|
|
t.Errorf("Test %d: Expected status code %d but was %d",
|
2016-06-05 22:51:56 -05:00
|
|
|
i, test.result, result)
|
|
|
|
}
|
2017-01-03 19:40:07 -05:00
|
|
|
if test.result == http.StatusUnauthorized {
|
2016-06-05 22:51:56 -05:00
|
|
|
headers := rec.Header()
|
|
|
|
if val, ok := headers["Www-Authenticate"]; ok {
|
2017-01-03 19:40:07 -05:00
|
|
|
if got, want := val[0], "Basic realm=\"Restricted\""; got != want {
|
|
|
|
t.Errorf("Test %d: Www-Authenticate header should be '%s', got: '%s'", i, want, got)
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
} else {
|
2017-01-03 19:40:07 -05:00
|
|
|
t.Errorf("Test %d: response should have a 'Www-Authenticate' header", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if got, want := req.Header.Get("Authorization"), ""; got != want {
|
|
|
|
t.Errorf("Test %d: Expected Authorization header to be stripped from request after successful authentication, but is: %s", i, got)
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipleOverlappingRules(t *testing.T) {
|
|
|
|
rw := BasicAuth{
|
|
|
|
Next: httpserver.HandlerFunc(contentHandler),
|
|
|
|
Rules: []Rule{
|
|
|
|
{Username: "t", Password: PlainMatcher("p1"), Resources: []string{"/t"}},
|
|
|
|
{Username: "t1", Password: PlainMatcher("p2"), Resources: []string{"/t/t"}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
from string
|
|
|
|
result int
|
|
|
|
cred string
|
|
|
|
}{
|
|
|
|
{"/t", http.StatusOK, "t:p1"},
|
|
|
|
{"/t/t", http.StatusOK, "t:p1"},
|
|
|
|
{"/t/t", http.StatusOK, "t1:p2"},
|
|
|
|
{"/a", http.StatusOK, "t1:p2"},
|
|
|
|
{"/t/t", http.StatusUnauthorized, "t1:p3"},
|
|
|
|
{"/t", http.StatusUnauthorized, "t1:p2"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", test.from, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Could not create HTTP request %v", i, err)
|
|
|
|
}
|
|
|
|
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(test.cred))
|
|
|
|
req.Header.Set("Authorization", auth)
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
result, err := rw.ServeHTTP(rec, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Could not ServeHTTP %v", i, err)
|
|
|
|
}
|
|
|
|
if result != test.result {
|
|
|
|
t.Errorf("Test %d: Expected Header '%d' but was '%d'",
|
|
|
|
i, test.result, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func contentHandler(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
fmt.Fprintf(w, r.URL.String())
|
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHtpasswd(t *testing.T) {
|
|
|
|
htpasswdPasswd := "IedFOuGmTpT8"
|
|
|
|
htpasswdFile := `sha1:{SHA}dcAUljwz99qFjYR0YLTXx0RqLww=
|
|
|
|
md5:$apr1$l42y8rex$pOA2VJ0x/0TwaFeAF9nX61`
|
|
|
|
|
|
|
|
htfh, err := ioutil.TempFile("", "basicauth-")
|
|
|
|
if err != nil {
|
|
|
|
t.Skipf("Error creating temp file (%v), will skip htpassword test")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove(htfh.Name())
|
|
|
|
if _, err = htfh.Write([]byte(htpasswdFile)); err != nil {
|
|
|
|
t.Fatalf("write htpasswd file %q: %v", htfh.Name(), err)
|
|
|
|
}
|
|
|
|
htfh.Close()
|
|
|
|
|
|
|
|
for i, username := range []string{"sha1", "md5"} {
|
|
|
|
rule := Rule{Username: username, Resources: []string{"/testing"}}
|
|
|
|
|
|
|
|
siteRoot := filepath.Dir(htfh.Name())
|
|
|
|
filename := filepath.Base(htfh.Name())
|
|
|
|
if rule.Password, err = GetHtpasswdMatcher(filename, rule.Username, siteRoot); err != nil {
|
|
|
|
t.Fatalf("GetHtpasswdMatcher(%q, %q): %v", htfh.Name(), rule.Username, err)
|
|
|
|
}
|
|
|
|
t.Logf("%d. username=%q", i, rule.Username)
|
|
|
|
if !rule.Password(htpasswdPasswd) || rule.Password(htpasswdPasswd+"!") {
|
|
|
|
t.Errorf("%d (%s) password does not match.", i, rule.Username)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|