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

112 lines
3.0 KiB
Go
Raw Normal View History

package middleware
import (
"fmt"
2020-11-30 17:19:03 +01:00
"net/http"
"strings"
accounts "github.com/owncloud/ocis/accounts/pkg/proto/v0"
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/ocis-pkg/oidc"
)
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
oidcIss := options.OIDCIss
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,
accountsClient: options.AccountsClient,
}
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)
2020-11-26 10:33:46 +01:00
account, ok := h.getAccount(req)
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
2020-11-26 10:33:46 +01:00
if !ok {
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: account.Id,
Iss: oidcIss,
}
next.ServeHTTP(w, req.WithContext(oidc.NewContext(req.Context(), claims)))
},
)
}
}
type basicAuth struct {
logger log.Logger
enabled bool
accountsClient accounts.AccountsService
}
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 != ""
}
2020-11-26 14:46:44 +01:00
func (m basicAuth) getAccount(req *http.Request) (*accounts.Account, bool) {
login, password, _ := req.BasicAuth()
account, status := getAccount(
m.logger,
m.accountsClient,
fmt.Sprintf(
"login eq '%s' and password eq '%s'",
strings.ReplaceAll(login, "'", "''"),
strings.ReplaceAll(password, "'", "''"),
),
)
return account, status == 0
}