2015-05-08 00:41:48 -05:00
|
|
|
package basicauth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2015-08-30 13:07:43 -05:00
|
|
|
"io/ioutil"
|
2015-05-08 00:41:48 -05:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-08-30 13:07:43 -05:00
|
|
|
"os"
|
2015-10-13 18:49:53 -05:00
|
|
|
"path/filepath"
|
2015-05-08 00:41:48 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBasicAuth(t *testing.T) {
|
|
|
|
|
|
|
|
rw := BasicAuth{
|
|
|
|
Next: middleware.HandlerFunc(contentHandler),
|
|
|
|
Rules: []Rule{
|
2015-08-30 13:07:43 -05:00
|
|
|
{Username: "test", Password: PlainMatcher("ttest"), Resources: []string{"/testing"}},
|
2015-05-08 00:41:48 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
from string
|
|
|
|
result int
|
|
|
|
cred string
|
|
|
|
}{
|
2015-05-08 02:41:17 -05:00
|
|
|
{"/testing", http.StatusUnauthorized, "ttest:test"},
|
2015-05-08 00:41:48 -05:00
|
|
|
{"/testing", http.StatusOK, "test:ttest"},
|
|
|
|
{"/testing", http.StatusUnauthorized, ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2015-05-08 02:41:17 -05:00
|
|
|
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)
|
|
|
|
}
|
2015-05-09 01:11:02 -05:00
|
|
|
if result == http.StatusUnauthorized {
|
|
|
|
headers := rec.Header()
|
|
|
|
if val, ok := headers["Www-Authenticate"]; ok {
|
|
|
|
if val[0] != "Basic" {
|
2015-05-24 21:52:34 -05:00
|
|
|
t.Errorf("Test %d, Www-Authenticate should be %s provided %s", i, "Basic", val[0])
|
2015-05-09 01:11:02 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Errorf("Test %d, should provide a header Www-Authenticate", i)
|
|
|
|
}
|
|
|
|
}
|
2015-05-08 00:41:48 -05:00
|
|
|
|
2015-05-09 01:11:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipleOverlappingRules(t *testing.T) {
|
|
|
|
rw := BasicAuth{
|
|
|
|
Next: middleware.HandlerFunc(contentHandler),
|
|
|
|
Rules: []Rule{
|
2015-08-30 13:07:43 -05:00
|
|
|
{Username: "t", Password: PlainMatcher("p1"), Resources: []string{"/t"}},
|
|
|
|
{Username: "t1", Password: PlainMatcher("p2"), Resources: []string{"/t/t"}},
|
2015-05-09 01:11:02 -05:00
|
|
|
},
|
|
|
|
}
|
2015-05-24 21:52:34 -05:00
|
|
|
|
2015-05-09 01:11:02 -05:00
|
|
|
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"},
|
2015-05-10 01:20:58 -05:00
|
|
|
{"/a", http.StatusOK, "t1:p2"},
|
|
|
|
{"/t/t", http.StatusUnauthorized, "t1:p3"},
|
|
|
|
{"/t", http.StatusUnauthorized, "t1:p2"},
|
2015-05-09 01:11:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2015-05-24 21:52:34 -05:00
|
|
|
|
2015-05-09 01:11:02 -05:00
|
|
|
req, err := http.NewRequest("GET", test.from, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Could not create HTTP request %v", i, err)
|
2015-05-08 00:41:48 -05:00
|
|
|
}
|
2015-05-09 01:11:02 -05:00
|
|
|
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(test.cred))
|
|
|
|
req.Header.Set("Authorization", auth)
|
2015-05-08 00:41:48 -05:00
|
|
|
|
2015-05-09 01:11:02 -05:00
|
|
|
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)
|
|
|
|
}
|
2015-05-24 21:52:34 -05:00
|
|
|
|
2015-05-08 00:41:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func contentHandler(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
fmt.Fprintf(w, r.URL.String())
|
2015-05-08 02:41:17 -05:00
|
|
|
return http.StatusOK, nil
|
2015-05-24 21:52:34 -05:00
|
|
|
}
|
2015-08-30 13:07:43 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2015-10-13 18:49:53 -05:00
|
|
|
defer os.Remove(htfh.Name())
|
2015-08-30 13:07:43 -05:00
|
|
|
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"}}
|
2015-10-13 18:49:53 -05:00
|
|
|
|
|
|
|
siteRoot := filepath.Dir(htfh.Name())
|
|
|
|
filename := filepath.Base(htfh.Name())
|
|
|
|
if rule.Password, err = GetHtpasswdMatcher(filename, rule.Username, siteRoot); err != nil {
|
2015-08-30 13:07:43 -05:00
|
|
|
t.Fatalf("GetHtpasswdMatcher(%q, %q): %v", htfh.Name(), rule.Username, err)
|
|
|
|
}
|
|
|
|
t.Logf("%d. username=%q password=%v", i, rule.Username, rule.Password)
|
|
|
|
if !rule.Password(htpasswdPasswd) || rule.Password(htpasswdPasswd+"!") {
|
|
|
|
t.Errorf("%d (%s) password does not match.", i, rule.Username)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|