@@ -17,7 +17,6 @@ limitations under the License.
1717package auth
1818
1919import (
20- "bytes"
2120 "context"
2221 "encoding/json"
2322 "fmt"
@@ -31,6 +30,7 @@ import (
3130 "go.opencensus.io/plugin/ochttp"
3231 corev1listers "k8s.io/client-go/listers/core/v1"
3332 "knative.dev/eventing/pkg/eventingtls"
33+ "knative.dev/eventing/pkg/utils"
3434 "knative.dev/pkg/configmap"
3535 "knative.dev/pkg/network"
3636 "knative.dev/pkg/tracing/propagation/tracecontextb3"
@@ -160,7 +160,7 @@ func (v *Verifier) verifyAuthN(ctx context.Context, audience *string, req *http.
160160// verifyAuthZ verifies if the given idToken is allowed by the resources eventPolicyStatus
161161func (v * Verifier ) verifyAuthZ (ctx context.Context , features feature.Flags , idToken * IDToken , resourceNamespace string , policyRefs []duckv1.AppliedEventPolicyRef , req * http.Request , resp http.ResponseWriter ) error {
162162 if len (policyRefs ) > 0 {
163- req , err := copyRequest (req )
163+ req , err := utils . CopyRequest (req )
164164 if err != nil {
165165 resp .WriteHeader (http .StatusInternalServerError )
166166 return fmt .Errorf ("failed to copy request body: %w" , err )
@@ -332,35 +332,6 @@ func (v *Verifier) getKubernetesOIDCDiscovery(features feature.Flags, client *ht
332332 return openIdConfig , nil
333333}
334334
335- // copyRequest makes a copy of the http request which can be consumed as needed, leaving the original request
336- // able to be consumed as well.
337- func copyRequest (req * http.Request ) (* http.Request , error ) {
338- // check if we actually need to copy the body, otherwise we can return the original request
339- if req .Body == nil || req .Body == http .NoBody {
340- return req , nil
341- }
342-
343- var buf bytes.Buffer
344- if _ , err := buf .ReadFrom (req .Body ); err != nil {
345- return nil , fmt .Errorf ("failed to read request body while copying it: %w" , err )
346- }
347-
348- if err := req .Body .Close (); err != nil {
349- return nil , fmt .Errorf ("failed to close original request body ready while copying request: %w" , err )
350- }
351-
352- // set the original request body to be readable again
353- req .Body = io .NopCloser (& buf )
354-
355- // return a new request with a readable body and same headers as the original
356- // we don't need to set any other fields as cloudevents only uses the headers
357- // and body to construct the Message/Event.
358- return & http.Request {
359- Header : req .Header ,
360- Body : io .NopCloser (bytes .NewReader (buf .Bytes ())),
361- }, nil
362- }
363-
364335type openIDMetadata struct {
365336 Issuer string `json:"issuer"`
366337 JWKSURI string `json:"jwks_uri"`
0 commit comments