refactor middlewares and reduce technical complexity

restructure server command and remove cfg.OIDC.Issuer switch, oidc middleware detects now if it should used or not
fix #761
This commit is contained in:
Florian Schade
2020-11-16 13:42:23 +01:00
committed by Benedikt Kulmann
parent 2d5401d9db
commit f8aa1a5e08
18 changed files with 955 additions and 642 deletions

View File

@@ -0,0 +1,71 @@
package middleware
import (
"fmt"
accounts "github.com/owncloud/ocis/accounts/pkg/proto/v0"
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/ocis-pkg/oidc"
"net/http"
"strings"
)
func BasicAuth(optionSetters ...Option) func(next http.Handler) http.Handler {
options := newOptions(optionSetters...)
return func(next http.Handler) http.Handler {
return &basicAuth{
next: next,
logger: options.Logger,
enabled: options.EnableBasicAuth,
accountsClient: options.AccountsClient,
oidcIss: options.OIDCIss,
}
}
}
type basicAuth struct {
next http.Handler
logger log.Logger
enabled bool
accountsClient accounts.AccountsService
oidcIss string
}
func (m basicAuth) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if !m.shouldServe(req) {
m.next.ServeHTTP(w, req)
return
}
m.logger.Warn().Msg("basic auth enabled, use only for testing or development")
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, "'", "''")))
if status != 0 {
w.WriteHeader(http.StatusUnauthorized)
return
}
claims := &oidc.StandardClaims{
OcisID: account.Id,
Iss: m.oidcIss,
}
m.next.ServeHTTP(w, req.WithContext(oidc.NewContext(req.Context(), claims)))
}
func (m basicAuth) shouldServe(req *http.Request) bool {
login, password, ok := req.BasicAuth()
if ok && login == "public" && strings.HasPrefix(req.URL.Path, "/remote.php/dav/public-files/") {
return true
}
if m.enabled && ok && login != "" && password != "" {
return true
}
return false
}