Skip to content

Commit

Permalink
add enable basic auth option
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
  • Loading branch information
butonic committed Sep 29, 2020
1 parent 60bc955 commit c5cfc10
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 24 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/add-basic-auth-option.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: Add basic auth option

We added a new `enable-basic-auth` option and `PROXY_ENABLE_BASIC_AUTH` environment variable that can be set to `true` to make the proxy verify the basic auth header with the accounts service. This should only be used for testing and development and is disabled by default.

https://github.com/owncloud/ocis/pull/627
https://github.com/owncloud/product/issues/198
6 changes: 6 additions & 0 deletions proxy/changelog/unreleased/add-basic-auth-option.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: Add basic auth option

We added a new `enable-basic-auth` option and `PROXY_ENABLE_BASIC_AUTH` environment variable that can be set to `true` to make the proxy verify the basic auth header with the accounts service. This should only be used for testing and development and is disabled by default.

https://github.com/owncloud/ocis/pull/627
https://github.com/owncloud/product/issues/198
1 change: 1 addition & 0 deletions proxy/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ func loadMiddlewares(ctx context.Context, l log.Logger, cfg *config.Config) alic
middleware.TokenManagerConfig(cfg.TokenManager),
middleware.AccountsClient(accounts),
middleware.SettingsRoleService(roles),
middleware.EnableBasicAuth(cfg.EnableBasicAuth),
)

// the connection will be established in a non blocking fashion
Expand Down
27 changes: 14 additions & 13 deletions proxy/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,20 @@ type Reva struct {

// Config combines all available configuration parts.
type Config struct {
File string
Log Log
Debug Debug
HTTP HTTP
Service Service
Tracing Tracing
Asset Asset
Policies []Policy
OIDC OIDC
TokenManager TokenManager
PolicySelector *PolicySelector `mapstructure:"policy_selector"`
Reva Reva
PreSignedURL PreSignedURL
File string
Log Log
Debug Debug
HTTP HTTP
Service Service
Tracing Tracing
Asset Asset
Policies []Policy
OIDC OIDC
TokenManager TokenManager
PolicySelector *PolicySelector `mapstructure:"policy_selector"`
Reva Reva
PreSignedURL PreSignedURL
EnableBasicAuth bool
}

// OIDC is the config for the OpenID-Connect middleware. If set the proxy will try to authenticate every request
Expand Down
9 changes: 9 additions & 0 deletions proxy/pkg/flagset/flagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
Usage: "--presignedurl-allow-method GET [--presignedurl-allow-method POST]",
EnvVars: []string{"PRESIGNEDURL_ALLOWED_METHODS"},
},

// Basic auth
&cli.BoolFlag{
Name: "enable-basic-auth",
Value: false,
Usage: "enable basic authentication",
EnvVars: []string{"PROXY_ENABLE_BASIC_AUTH"},
Destination: &cfg.EnableBasicAuth,
},
}

}
Expand Down
29 changes: 19 additions & 10 deletions proxy/pkg/middleware/account_uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,31 @@ func AccountUUID(opts ...Option) func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := opt.Logger
claims := oidc.FromContext(r.Context())
if claims == nil {
next.ServeHTTP(w, r)
return
}

var account *acc.Account
var status int
if claims.Email != "" {
switch {
case claims == nil:
login, password, ok := r.BasicAuth()
if opt.EnableBasicAuth && ok {
l.Warn().Msg("basic auth enabled, use only for testing or development")
account, status = getAccount(l, opt.AccountsClient, fmt.Sprintf("login eq '%s' and password eq '%s'", strings.ReplaceAll(login, "'", "''"), strings.ReplaceAll(password, "'", "''")))
// fake claims for the subsequent code flow
claims = &oidc.StandardClaims{
Iss: opt.OIDCIss,
}
} else {
next.ServeHTTP(w, r)
return
}
case claims.Email != "":
account, status = getAccount(l, opt.AccountsClient, fmt.Sprintf("mail eq '%s'", strings.ReplaceAll(claims.Email, "'", "''")))
} else if claims.PreferredUsername != "" {
case claims.PreferredUsername != "":
account, status = getAccount(l, opt.AccountsClient, fmt.Sprintf("preferred_name eq '%s'", strings.ReplaceAll(claims.PreferredUsername, "'", "''")))
} else if claims.OcisID != "" {
case claims.OcisID != "":
account, status = getAccount(l, opt.AccountsClient, fmt.Sprintf("id eq '%s'", strings.ReplaceAll(claims.OcisID, "'", "''")))
} else {
default:
// TODO allow lookup by custom claim, eg an id ... or sub
l.Error().Err(err).Msgf("Could not lookup account, no mail or preferred_username claim set")
l.Error().Err(err).Msg("Could not lookup account, no mail or preferred_username claim set")
w.WriteHeader(http.StatusInternalServerError)
}
if status != 0 || account == nil {
Expand Down
12 changes: 11 additions & 1 deletion proxy/pkg/middleware/options.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package middleware

import (
settings "github.com/owncloud/ocis/settings/pkg/proto/v0"
"net/http"

settings "github.com/owncloud/ocis/settings/pkg/proto/v0"

gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
acc "github.com/owncloud/ocis/accounts/pkg/proto/v0"
"github.com/owncloud/ocis/ocis-pkg/log"
Expand Down Expand Up @@ -36,6 +37,8 @@ type Options struct {
Store storepb.StoreService
// PreSignedURLConfig to configure the middleware
PreSignedURLConfig config.PreSignedURL
// EnableBasicAuth to allow basic auth
EnableBasicAuth bool
}

// newOptions initializes the available default options.
Expand Down Expand Up @@ -118,3 +121,10 @@ func PreSignedURLConfig(cfg config.PreSignedURL) Option {
o.PreSignedURLConfig = cfg
}
}

// EnableBasicAuth provides a function to set the EnableBasicAuth config
func EnableBasicAuth(enableBasicAuth bool) Option {
return func(o *Options) {
o.EnableBasicAuth = enableBasicAuth
}
}

0 comments on commit c5cfc10

Please sign in to comment.