Exemplo n.º 1
0
func main() {
	auth := authenticater.NewBasicAuth()
	auth.AddPrincipal("foo", "bar")
	http.HandleFunc("/", authenticater.WrapAuth(auth,
		func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("Hello"))
		}))
	http.ListenAndServe(":8080", nil)
}
Exemplo n.º 2
0
func TestTargetWithoutAuth(t *testing.T) {
	ba := auth.NewBasicAuth()
	ba.AddPrincipal("foo", "foo")
	server := newServer(&http.Server{}, ba, nil)

	recorder := httptest.NewRecorder()
	req, err := http.NewRequest("GET", "/target/foo", bytes.NewReader([]byte("")))
	if err != nil {
		t.Fatal(err)
	}

	server.http.Handler.ServeHTTP(recorder, req)

	if recorder.Code != http.StatusUnauthorized {
		t.Fatal("Wrong Response Code: ", recorder.Code)
	}
}
Exemplo n.º 3
0
func TestTargetWithMultiplePasswords(t *testing.T) {
	ba := auth.NewBasicAuth()
	ba.AddPrincipal("user", "pass1")
	ba.AddPrincipal("user", "pass2")
	server := newServer(&http.Server{}, ba, newHashRing(1, nil))

	recorder := httptest.NewRecorder()

	for i := 1; i <= 2; i++ {
		req, err := http.NewRequest("GET", "/target/foo", bytes.NewReader([]byte("foo")))
		req.SetBasicAuth("user", fmt.Sprintf("pass%d", i))
		if err != nil {
			t.Fatal(err)
		}

		server.serveTarget(recorder, req)

		if recorder.Code == http.StatusForbidden {
			t.Fatal("Provided proper credentials, was forbidden.")
		}
	}
}