ca40e2802e
The changes simplify the `req` method by moving the authentication-related code into the API. This makes it easy to add additional authentication methods. The API introduces an `Authorizer` that acts as an authenticator factory. The authentication flow itself is divided down into `Authorize` and `Verify` steps in order to encapsulate and control complex authentication challenges. The default `NewAutoAuth` negotiates the algorithms. Under the hood, it creates an authenticator shim per request, which delegates the authentication flow to our authenticators. The `NewEmptyAuth` and `NewPreemptiveAuth` authorizers allow you to have more control over algorithms and resources. The API also allows interception of the redirect mechanism by setting the `XInhibitRedirect` header. This closes: #15 #24 #38
36 lines
893 B
Go
36 lines
893 B
Go
package gowebdav
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestNewDigestAuth(t *testing.T) {
|
|
a := &DigestAuth{"user", "password", make(map[string]string, 0)}
|
|
|
|
ex := "DigestAuth login: user"
|
|
if a.String() != ex {
|
|
t.Error("expected: " + ex + " got: " + a.String())
|
|
}
|
|
|
|
if a.Clone() == a {
|
|
t.Error("expected a different instance")
|
|
}
|
|
|
|
if a.Close() != nil {
|
|
t.Error("expected close without errors")
|
|
}
|
|
}
|
|
|
|
func TestDigestAuthAuthorize(t *testing.T) {
|
|
a := &DigestAuth{"user", "password", make(map[string]string, 0)}
|
|
rq, _ := http.NewRequest("GET", "http://localhost/", nil)
|
|
a.Authorize(nil, rq, "/")
|
|
// TODO this is a very lazy test it cuts of cnonce
|
|
ex := `Digest username="user", realm="", nonce="", uri="/", nc=1, cnonce="`
|
|
if strings.Index(rq.Header.Get("Authorization"), ex) != 0 {
|
|
t.Error("got wrong Authorization header: " + rq.Header.Get("Authorization"))
|
|
}
|
|
}
|