v2http: refactor http basic auth

refactor http basic auth code to combine basic auth extraction and validation
This commit is contained in:
rob boll
2015-11-23 16:10:08 -05:00
committed by Gyu-Ho Lee
parent 7ea8860670
commit 42454f9ed8
2 changed files with 99 additions and 26 deletions

View File

@ -440,6 +440,14 @@ func mustAuthRequest(method, username, password string) *http.Request {
return req
}
func unauthedRequest(method string) *http.Request {
req, err := http.NewRequest(method, "path", strings.NewReader(""))
if err != nil {
panic("Cannot make request: " + err.Error())
}
return req
}
func TestPrefixAccess(t *testing.T) {
var table = []struct {
key string
@ -701,3 +709,66 @@ func TestPrefixAccess(t *testing.T) {
}
}
}
func TestUserFromBasicAuth(t *testing.T) {
sec := &mockAuthStore{
users: map[string]*auth.User{
"user": {
User: "user",
Roles: []string{"root"},
Password: "password",
},
},
roles: map[string]*auth.Role{
"root": {
Role: "root",
},
},
}
var table = []struct {
username string
req *http.Request
userExists bool
}{
{
// valid user, valid pass
username: "user",
req: mustAuthRequest("GET", "user", "password"),
userExists: true,
},
{
// valid user, bad pass
username: "user",
req: mustAuthRequest("GET", "user", "badpass"),
userExists: false,
},
{
// valid user, no pass
username: "user",
req: mustAuthRequest("GET", "user", ""),
userExists: false,
},
{
// missing user
username: "missing",
req: mustAuthRequest("GET", "missing", "badpass"),
userExists: false,
},
{
// no basic auth
req: unauthedRequest("GET"),
userExists: false,
},
}
for i, tt := range table {
user := userFromBasicAuth(sec, tt.req)
if tt.userExists == (user == nil) {
t.Errorf("#%d: userFromBasicAuth doesn't match (expected %v)", i, tt.userExists)
}
if user != nil && (tt.username != user.User) {
t.Errorf("#%d: userFromBasicAuth username doesn't match (expected %s, got %s)", i, tt.username, user.User)
}
}
}