diff --git a/opencloud/pkg/init/init.go b/opencloud/pkg/init/init.go index 4e0a1f3179..140a5a1aa8 100644 --- a/opencloud/pkg/init/init.go +++ b/opencloud/pkg/init/init.go @@ -281,6 +281,7 @@ func CreateConfig(insecure, forceOverwrite, diff bool, configPath, adminPassword cfg.Frontend.Archiver = _insecureService cfg.Frontend.OCDav = _insecureService cfg.Graph.Spaces = _insecureService + cfg.Webfinger = _insecureService cfg.Graph.Events = _insecureEvents cfg.Notifications.Notifications.Events = _insecureEvents cfg.Search.Events = _insecureEvents diff --git a/opencloud/pkg/init/structs.go b/opencloud/pkg/init/structs.go index 10efb88c96..95ecfa3981 100644 --- a/opencloud/pkg/init/structs.go +++ b/opencloud/pkg/init/structs.go @@ -46,6 +46,7 @@ type OpenCloudConfig struct { AuthService AuthService `yaml:"auth_service"` Clientlog Clientlog `yaml:"clientlog"` Activitylog Activitylog `yaml:"activitylog"` + Webfinger InsecureService `yaml:"webfinger"` } // Activitylog is the configuration for the activitylog service diff --git a/pkg/middleware/oidc.go b/pkg/middleware/oidc.go index 1db971ba42..d521ddef4b 100644 --- a/pkg/middleware/oidc.go +++ b/pkg/middleware/oidc.go @@ -39,10 +39,14 @@ func OidcAuth(opts ...Option) func(http.Handler) http.Handler { // Initialize a provider by specifying the issuer URL. // it will fetch the keys from the issuer using the .well-known // endpoint - return goidc.NewProvider( + p, err := goidc.NewProvider( context.WithValue(context.Background(), oauth2.HTTPClient, &opt.HttpClient), opt.OidcIssuer, ) + if err != nil { + return nil, err + } + return p, nil } var provider OIDCProvider initializeProviderLock := sync.Mutex{} diff --git a/pkg/middleware/oidc_test.go b/pkg/middleware/oidc_test.go new file mode 100644 index 0000000000..cd6c89d97c --- /dev/null +++ b/pkg/middleware/oidc_test.go @@ -0,0 +1,57 @@ +package middleware + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/opencloud-eu/opencloud/pkg/log" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestOidcAuth(t *testing.T) { + // the self-signed certificate of the test server is rejected by the default + // http client, so the provider initialization always fails + srv := httptest.NewTLSServer(http.NotFoundHandler()) + defer srv.Close() + + newHandler := func(called *bool) http.Handler { + next := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + *called = true + w.WriteHeader(http.StatusOK) + }) + return OidcAuth( + WithLogger(log.NewLogger()), + WithOidcIssuer(srv.URL), + WithHttpClient(http.Client{}), + )(next) + } + + t.Run("failed provider initialization is retried and never reaches next handler", func(t *testing.T) { + nextCalled := false + handler := newHandler(&nextCalled) + + for range 2 { + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.Header.Set("Authorization", "Bearer x") + rec := httptest.NewRecorder() + + require.NotPanics(t, func() { handler.ServeHTTP(rec, req) }) + assert.Equal(t, http.StatusInternalServerError, rec.Code) + } + assert.False(t, nextCalled) + }) + + t.Run("request without authorization header passes through", func(t *testing.T) { + nextCalled := false + handler := newHandler(&nextCalled) + + req := httptest.NewRequest(http.MethodGet, "/", nil) + rec := httptest.NewRecorder() + + handler.ServeHTTP(rec, req) + assert.Equal(t, http.StatusOK, rec.Code) + assert.True(t, nextCalled) + }) +}