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
63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
package gowebdav
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestEmptyAuth(t *testing.T) {
|
|
auth := NewEmptyAuth()
|
|
srv, _, _ := newAuthSrv(t, basicAuth)
|
|
defer srv.Close()
|
|
cli := NewAuthClient(srv.URL, auth)
|
|
if err := cli.Connect(); err == nil {
|
|
t.Fatalf("got nil want error")
|
|
}
|
|
}
|
|
|
|
func TestRedirectAuthWIP(t *testing.T) {
|
|
hasPassedAuthServer := false
|
|
authHandler := func(h http.Handler) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if user, passwd, ok := r.BasicAuth(); ok {
|
|
if user == "user" && passwd == "password" {
|
|
hasPassedAuthServer = true
|
|
w.WriteHeader(200)
|
|
return
|
|
}
|
|
}
|
|
w.Header().Set("Www-Authenticate", `Basic realm="x"`)
|
|
w.WriteHeader(401)
|
|
}
|
|
}
|
|
|
|
psrv, _, _ := newAuthSrv(t, authHandler)
|
|
defer psrv.Close()
|
|
|
|
dataHandler := func(h http.Handler) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
hasAuth := strings.Contains(r.Header.Get("Authorization"), "Basic dXNlcjpwYXNzd29yZA==")
|
|
|
|
if hasPassedAuthServer && hasAuth {
|
|
h.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
w.Header().Set("Www-Authenticate", `Basic realm="x"`)
|
|
http.Redirect(w, r, psrv.URL+"/", 302)
|
|
}
|
|
}
|
|
|
|
srv, _, _ := newAuthSrv(t, dataHandler)
|
|
defer srv.Close()
|
|
cli := NewClient(srv.URL, "user", "password")
|
|
data, err := cli.Read("/hello.txt")
|
|
if err != nil {
|
|
t.Logf("WIP got error=%v; want nil", err)
|
|
}
|
|
if bytes.Compare(data, []byte("hello gowebdav\n")) != 0 {
|
|
t.Logf("WIP got data=%v; want=hello gowebdav", data)
|
|
}
|
|
}
|