proxy is now aware of webdav responses

This commit is contained in:
A.Unger
2021-07-13 15:38:44 +02:00
parent bfdcc0180c
commit 7ab586b2ab
5 changed files with 112 additions and 83 deletions
+11 -79
View File
@@ -1,7 +1,6 @@
package middleware
import (
"encoding/xml"
"fmt"
"net/http"
"strings"
@@ -9,6 +8,7 @@ import (
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/ocis-pkg/oidc"
"github.com/owncloud/ocis/proxy/pkg/user/backend"
"github.com/owncloud/ocis/proxy/pkg/webdav"
)
const publicFilesEndpoint = "/remote.php/dav/public-files/"
@@ -69,12 +69,17 @@ func BasicAuth(optionSetters ...Option) func(next http.Handler) http.Handler {
w.WriteHeader(http.StatusUnauthorized)
b, err := Marshal(exception{
code: SabredavPermissionDenied,
message: "Authentication error",
})
if webdav.IsWebdavRequest(req) {
b, err := webdav.Marshal(webdav.Exception{
Code: webdav.SabredavPermissionDenied,
Message: "Authentication error",
})
HandleWebdavError(w, b, err)
webdav.HandleWebdavError(w, b, err)
return
}
w.WriteHeader(http.StatusUnauthorized)
return
}
@@ -106,76 +111,3 @@ func (m basicAuth) isBasicAuth(req *http.Request) bool {
_, _, ok := req.BasicAuth()
return m.enabled && ok
}
type code int
const (
// SabredavBadRequest maps to HTTP 400
SabredavBadRequest code = iota
// SabredavMethodNotAllowed maps to HTTP 405
SabredavMethodNotAllowed
// SabredavNotAuthenticated maps to HTTP 401
SabredavNotAuthenticated
// SabredavPreconditionFailed maps to HTTP 412
SabredavPreconditionFailed
// SabredavPermissionDenied maps to HTTP 403
SabredavPermissionDenied
// SabredavNotFound maps to HTTP 404
SabredavNotFound
// SabredavConflict maps to HTTP 409
SabredavConflict
)
var (
codesEnum = []string{
"Sabre\\DAV\\Exception\\BadRequest",
"Sabre\\DAV\\Exception\\MethodNotAllowed",
"Sabre\\DAV\\Exception\\NotAuthenticated",
"Sabre\\DAV\\Exception\\PreconditionFailed",
"Sabre\\DAV\\Exception\\PermissionDenied",
"Sabre\\DAV\\Exception\\NotFound",
"Sabre\\DAV\\Exception\\Conflict",
}
)
type exception struct {
code code
message string
header string
}
// Marshal just calls the xml marshaller for a given exception.
func Marshal(e exception) ([]byte, error) {
xmlstring, err := xml.Marshal(&errorXML{
Xmlnsd: "DAV",
Xmlnss: "http://sabredav.org/ns",
Exception: codesEnum[e.code],
Message: e.message,
Header: e.header,
})
if err != nil {
return []byte(""), err
}
return []byte(xml.Header + string(xmlstring)), err
}
// http://www.webdav.org/specs/rfc4918.html#ELEMENT_error
type errorXML struct {
XMLName xml.Name `xml:"d:error"`
Xmlnsd string `xml:"xmlns:d,attr"`
Xmlnss string `xml:"xmlns:s,attr"`
Exception string `xml:"s:exception"`
Message string `xml:"s:message"`
InnerXML []byte `xml:",innerxml"`
Header string `xml:"s:header,omitempty"`
}
func HandleWebdavError(w http.ResponseWriter, b []byte, err error) {
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
_, err = w.Write(b)
if err != nil {
}
}