fix(webfinger): fix panic in OidcAuth middleware

This commit is contained in:
Alex Ababii committed 2026-11-09 14:33:55 +01:00
1 parent 2013f811c3
commit c71ca6f699
2 files changed
+62 -1

No files matched your search

+5 -1
View File
@@ -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{}
+57
View File
@@ -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)
})
}