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

95 lines
2.8 KiB
Go
Raw Normal View History

package middleware
import (
"fmt"
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/ocis-pkg/oidc"
"github.com/owncloud/ocis/proxy/pkg/user/backend"
"net/http"
"strings"
)
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) {
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)
}
2020-11-26 10:33:46 +01:00
w.WriteHeader(http.StatusUnauthorized)
return
}
claims := &oidc.StandardClaims{
OcisID: user.Id.OpaqueId,
Iss: user.Id.Idp,
PreferredUsername: user.Username,
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)
}
2020-11-26 14:46:44 +01:00
func (m basicAuth) isBasicAuth(req *http.Request) bool {
2020-11-26 10:33:46 +01:00
login, password, ok := req.BasicAuth()
return m.enabled && ok && login != "" && password != ""
}