Files
ocis/proxy/pkg/middleware/basic_auth.go
T

119 lines
3.6 KiB
Go
Raw Normal View History

package middleware
import (
"fmt"
2021-07-13 14:29:26 +02:00
"net/http"
"strings"
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/ocis-pkg/oidc"
"github.com/owncloud/ocis/proxy/pkg/user/backend"
2021-07-13 15:38:44 +02:00
"github.com/owncloud/ocis/proxy/pkg/webdav"
)
const publicFilesEndpoint = "/remote.php/dav/public-files/"
2020-11-16 14:01:32 +01:00
// BasicAuth provides a middleware to check if BasicAuth is provided
func BasicAuth(optionSetters ...Option) func(next http.Handler) http.Handler {
options := newOptions(optionSetters...)
2020-11-26 10:33:46 +01:00
logger := options.Logger
2020-11-18 12:48:31 +01:00
if options.EnableBasicAuth {
options.Logger.Warn().Msg("basic auth enabled, use only for testing or development")
}
2020-11-26 10:33:46 +01:00
h := basicAuth{
logger: logger,
enabled: options.EnableBasicAuth,
userProvider: options.UserProvider,
2020-11-26 10:33:46 +01:00
}
return func(next http.Handler) http.Handler {
2020-11-26 10:33:46 +01:00
return http.HandlerFunc(
func(w http.ResponseWriter, req *http.Request) {
if h.isPublicLink(req) || !h.isBasicAuth(req) || h.isOIDCTokenAuth(req) {
2020-12-01 16:57:36 +01:00
if !h.isPublicLink(req) {
2020-12-02 15:51:39 +01:00
userAgentAuthenticateLockIn(w, req, options.CredentialsByUserAgent, "basic")
2020-12-01 16:57:36 +01:00
}
2020-11-26 10:33:46 +01:00
next.ServeHTTP(w, req)
return
}
2020-12-02 15:51:39 +01:00
removeSuperfluousAuthenticate(w)
login, password, _ := req.BasicAuth()
user, _, err := h.userProvider.Authenticate(req.Context(), login, password)
2020-12-02 15:31:17 +01:00
// touch is a user agent locking guard, when touched changes to true it indicates the User-Agent on the
// request is configured to support only one challenge, it it remains untouched, there are no considera-
// tions and we should write all available authentication challenges to the response.
touch := false
if err != nil {
2020-12-02 15:31:17 +01:00
for k, v := range options.CredentialsByUserAgent {
if strings.Contains(k, req.UserAgent()) {
2020-12-02 15:51:39 +01:00
removeSuperfluousAuthenticate(w)
2020-12-02 15:31:17 +01:00
w.Header().Add("Www-Authenticate", fmt.Sprintf("%v realm=\"%s\", charset=\"UTF-8\"", strings.Title(v), req.Host))
touch = true
break
2020-12-01 17:10:04 +01:00
}
}
2020-12-02 15:31:17 +01:00
// if the request is not bound to any user agent, write all available challenges
if !touch {
writeSupportedAuthenticateHeader(w, req)
}
2021-07-13 14:29:26 +02:00
// if the request is a PROPFIND return a WebDAV error code.
// TODO: The proxy has to be smart enough to detect when a request is directed towards a webdav server
// and react accordingly.
2020-11-26 10:33:46 +01:00
w.WriteHeader(http.StatusUnauthorized)
2021-07-13 14:29:26 +02:00
2021-07-13 15:38:44 +02:00
if webdav.IsWebdavRequest(req) {
b, err := webdav.Marshal(webdav.Exception{
Code: webdav.SabredavPermissionDenied,
Message: "Authentication error",
})
2021-07-13 14:29:26 +02:00
2021-07-13 15:38:44 +02:00
webdav.HandleWebdavError(w, b, err)
return
}
2020-11-26 10:33:46 +01:00
return
}
2021-07-21 15:37:12 +00:00
// fake oidc claims
claims := map[string]interface{}{
oidc.OwncloudUUID: user.Id.OpaqueId,
oidc.Iss: user.Id.Idp,
oidc.PreferredUsername: user.Username,
oidc.Email: user.Mail,
2020-11-26 10:33:46 +01:00
}
next.ServeHTTP(w, req.WithContext(oidc.NewContext(req.Context(), claims)))
},
)
}
}
type basicAuth struct {
logger log.Logger
enabled bool
userProvider backend.UserBackend
}
2020-11-26 14:46:44 +01:00
func (m basicAuth) isPublicLink(req *http.Request) bool {
2020-11-26 10:33:46 +01:00
login, _, ok := req.BasicAuth()
return ok && login == "public" && strings.HasPrefix(req.URL.Path, publicFilesEndpoint)
}
// The token auth endpoint uses basic auth for clients, see https://openid.net/specs/openid-connect-basic-1_0.html#TokenRequest
// > The Client MUST authenticate to the Token Endpoint using the HTTP Basic method, as described in 2.3.1 of OAuth 2.0.
func (m basicAuth) isOIDCTokenAuth(req *http.Request) bool {
return req.URL.Path == "/konnect/v1/token"
}
2020-11-26 14:46:44 +01:00
func (m basicAuth) isBasicAuth(req *http.Request) bool {
2021-07-13 14:33:24 +02:00
_, _, ok := req.BasicAuth()
return m.enabled && ok
2020-11-26 10:33:46 +01:00
}