diff --git a/middleware_test.go b/middleware_test.go index 13f9f3cb864cbdf8b8e14ecaa11d260300ce4154..fb151be42ecff8dccb41ed8c03b4cced4f10588c 100644 --- a/middleware_test.go +++ b/middleware_test.go @@ -326,6 +326,30 @@ func TestOauthRequests(t *testing.T) { newFakeProxy(cfg).RunTests(t, requests) } +func TestMethodExclusions(t *testing.T) { + cfg := newFakeKeycloakConfig() + cfg.Resources = []*Resource{ + { + URL: "/post", + Methods: []string{http.MethodPost, http.MethodPut}, + }, + } + requests := []fakeRequest{ + { // we should get a 401 + URI: "/post", + Method: http.MethodPost, + ExpectedCode: http.StatusUnauthorized, + }, + { // we should be permitted + URI: "/post", + Method: http.MethodGet, + ExpectedProxy: true, + ExpectedCode: http.StatusOK, + }, + } + newFakeProxy(cfg).RunTests(t, requests) +} + func TestStrangeRoutingError(t *testing.T) { cfg := newFakeKeycloakConfig() cfg.Resources = []*Resource{ diff --git a/server.go b/server.go index 339695f705165f969250baa00656a67db85f03eb..c9efd15b638ac029a3e27dd369d5fb9f414953ca 100644 --- a/server.go +++ b/server.go @@ -226,18 +226,16 @@ func (r *oauthProxy) createReverseProxy() error { r.authenticationMiddleware(x), r.admissionMiddleware(x), r.headersMiddleware(r.config.AddClaims)) - e.MethodNotAllowed(emptyHandler) - switch x.WhiteListed { - case false: - for _, m := range x.Methods { + + for _, m := range x.Methods { + if !x.WhiteListed { e.MethodFunc(m, x.URL, emptyHandler) + continue } - default: - for _, m := range x.Methods { - engine.MethodFunc(m, x.URL, emptyHandler) - } + engine.MethodFunc(m, x.URL, emptyHandler) } } + for name, value := range r.config.MatchClaims { r.log.Info("token must contain", zap.String("claim", name), zap.String("value", value)) }