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
58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
package gowebdav
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
// ErrAuthChanged must be returned from the Verify method as an error
|
|
// to trigger a re-authentication / negotiation with a new authenticator.
|
|
var ErrAuthChanged = errors.New("authentication failed, change algorithm")
|
|
|
|
// ErrTooManyRedirects will be used as return error if a request exceeds 10 redirects.
|
|
var ErrTooManyRedirects = errors.New("stopped after 10 redirects")
|
|
|
|
// StatusError implements error and wraps
|
|
// an erroneous status code.
|
|
type StatusError struct {
|
|
Status int
|
|
}
|
|
|
|
func (se StatusError) Error() string {
|
|
return fmt.Sprintf("%d", se.Status)
|
|
}
|
|
|
|
// IsErrCode returns true if the given error
|
|
// is an os.PathError wrapping a StatusError
|
|
// with the given status code.
|
|
func IsErrCode(err error, code int) bool {
|
|
if pe, ok := err.(*os.PathError); ok {
|
|
se, ok := pe.Err.(StatusError)
|
|
return ok && se.Status == code
|
|
}
|
|
return false
|
|
}
|
|
|
|
// IsErrNotFound is shorthand for IsErrCode
|
|
// for status 404.
|
|
func IsErrNotFound(err error) bool {
|
|
return IsErrCode(err, 404)
|
|
}
|
|
|
|
func NewPathError(op string, path string, statusCode int) error {
|
|
return &os.PathError{
|
|
Op: op,
|
|
Path: path,
|
|
Err: StatusError{statusCode},
|
|
}
|
|
}
|
|
|
|
func NewPathErrorErr(op string, path string, err error) error {
|
|
return &os.PathError{
|
|
Op: op,
|
|
Path: path,
|
|
Err: err,
|
|
}
|
|
}
|