mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-11-26 10:07:34 -05:00
Merge pull request #3938 from maki5/fix/webfinger_panic
fix(webfinger): fix panic in OidcAuth middleware
This commit is contained in:
4 files changed
+64
-1
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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{}
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user