2015-04-21 21:57:12 +02:00
|
|
|
package basic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/distribution/registry/auth"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBasicAccessController(t *testing.T) {
|
|
|
|
|
|
|
|
testRealm := "The-Shire"
|
2015-06-04 18:02:13 +02:00
|
|
|
testUsers := []string{"bilbo", "frodo", "MiShil", "DeokMan"}
|
|
|
|
testPasswords := []string{"baggins", "baggins", "새주", "공주님"}
|
2015-06-04 17:46:34 +02:00
|
|
|
testHtpasswdContent := `bilbo:{SHA}5siv5c0SHx681xU6GiSx9ZQryqs=
|
|
|
|
frodo:$2y$05$926C3y10Quzn/LnqQH86VOEVh/18T6RnLaS.khre96jLNL/7e.K5W
|
|
|
|
MiShil:$2y$05$0oHgwMehvoe8iAWS8I.7l.KoECXrwVaC16RPfaSCU5eVTFrATuMI2
|
|
|
|
DeokMan:공주님`
|
2015-04-21 21:57:12 +02:00
|
|
|
|
|
|
|
tempFile, err := ioutil.TempFile("", "htpasswd-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("could not create temporary htpasswd file")
|
|
|
|
}
|
|
|
|
if _, err = tempFile.WriteString(testHtpasswdContent); err != nil {
|
|
|
|
t.Fatal("could not write temporary htpasswd file")
|
|
|
|
}
|
|
|
|
|
|
|
|
options := map[string]interface{}{
|
|
|
|
"realm": testRealm,
|
|
|
|
"path": tempFile.Name(),
|
|
|
|
}
|
|
|
|
|
|
|
|
accessController, err := newAccessController(options)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("error creating access controller")
|
|
|
|
}
|
|
|
|
|
|
|
|
tempFile.Close()
|
2015-06-04 18:02:13 +02:00
|
|
|
|
2015-06-04 17:46:34 +02:00
|
|
|
var userNumber = 0
|
2015-06-04 18:02:13 +02:00
|
|
|
|
2015-04-21 21:57:12 +02:00
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := context.WithValue(nil, "http.request", r)
|
|
|
|
authCtx, err := accessController.Authorized(ctx)
|
|
|
|
if err != nil {
|
|
|
|
switch err := err.(type) {
|
|
|
|
case auth.Challenge:
|
|
|
|
err.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected error authorizing request: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
userInfo, ok := authCtx.Value("auth.user").(auth.UserInfo)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("basic accessController did not set auth.user context")
|
|
|
|
}
|
|
|
|
|
2015-06-04 17:46:34 +02:00
|
|
|
if userInfo.Name != testUsers[userNumber] {
|
|
|
|
t.Fatalf("expected user name %q, got %q", testUsers[userNumber], userInfo.Name)
|
2015-04-21 21:57:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}))
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
CheckRedirect: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", server.URL, nil)
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error during GET: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Request should not be authorized
|
|
|
|
if resp.StatusCode != http.StatusUnauthorized {
|
|
|
|
t.Fatalf("unexpected non-fail response status: %v != %v", resp.StatusCode, http.StatusUnauthorized)
|
|
|
|
}
|
|
|
|
|
2015-06-04 17:46:34 +02:00
|
|
|
for i := 0; i < len(testUsers); i++ {
|
|
|
|
userNumber = i
|
|
|
|
req, _ = http.NewRequest("GET", server.URL, nil)
|
2015-06-04 18:02:13 +02:00
|
|
|
sekrit := testUsers[i] + ":" + testPasswords[i]
|
2015-06-04 17:46:34 +02:00
|
|
|
credential := "Basic " + base64.StdEncoding.EncodeToString([]byte(sekrit))
|
2015-04-21 21:57:12 +02:00
|
|
|
|
2015-06-04 17:46:34 +02:00
|
|
|
req.Header.Set("Authorization", credential)
|
|
|
|
resp, err = client.Do(req)
|
2015-06-04 18:02:13 +02:00
|
|
|
|
2015-06-04 17:46:34 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error during GET: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2015-04-21 21:57:12 +02:00
|
|
|
|
2015-06-04 17:46:34 +02:00
|
|
|
// Request should be authorized
|
|
|
|
if resp.StatusCode != http.StatusNoContent {
|
|
|
|
t.Fatalf("unexpected non-success response status: %v != %v for %s %s %s", resp.StatusCode, http.StatusNoContent, testUsers[i], testPasswords[i], credential)
|
|
|
|
}
|
2015-04-21 21:57:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|