@@ -195,26 +195,14 @@ func (p *Proxy) RoundTrip(req *http.Request) (*http.Response, error) {
195
195
196
196
// withAuthenticateRequest adds the proxy authentication handler to a chain.
197
197
func (p * Proxy ) withAuthenticateRequest (handler http.Handler ) http.Handler {
198
+ tokenReviewHandler := p .withTokenReview (handler )
199
+
198
200
return http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
199
201
// Auth request and handle unauthed
200
202
info , ok , err := p .oidcRequestAuther .AuthenticateRequest (req )
201
203
if err != nil {
202
- if ! p .config .TokenReview {
203
- p .handleError (rw , req , errUnauthorized )
204
- return
205
- }
206
-
207
- // Attempt to passthrough request if valid token
208
- if p .reviewToken (rw , req ) {
209
- // Set no impersonation headers and re-add removed headers.
210
- req = req .WithContext (context .WithNoImpersonation (req .Context ()))
211
-
212
- handler .ServeHTTP (rw , req )
213
- return
214
- }
215
-
216
- // Token review failed so error
217
- p .handleError (rw , req , errUnauthorized )
204
+ // Since we have failed OIDC auth, we will try a token review, if enabled.
205
+ tokenReviewHandler .ServeHTTP (rw , req )
218
206
return
219
207
}
220
208
@@ -232,6 +220,30 @@ func (p *Proxy) withAuthenticateRequest(handler http.Handler) http.Handler {
232
220
})
233
221
}
234
222
223
+ // withTokenReview will attempt a token review on the incoming request, if
224
+ // enabled.
225
+ func (p * Proxy ) withTokenReview (handler http.Handler ) http.Handler {
226
+ return http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
227
+ // If token review is not enabled then error.
228
+ if ! p .config .TokenReview {
229
+ p .handleError (rw , req , errUnauthorized )
230
+ return
231
+ }
232
+
233
+ // Attempt to passthrough request if valid token
234
+ if ! p .reviewToken (rw , req ) {
235
+ // Token review failed so error
236
+ p .handleError (rw , req , errUnauthorized )
237
+ return
238
+ }
239
+
240
+ // Set no impersonation headers and re-add removed headers.
241
+ req = req .WithContext (context .WithNoImpersonation (req .Context ()))
242
+
243
+ handler .ServeHTTP (rw , req )
244
+ })
245
+ }
246
+
235
247
// withImpersonateRequest adds the impersonation request handler to the chain.
236
248
func (p * Proxy ) withImpersonateRequest (handler http.Handler ) http.Handler {
237
249
return http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
@@ -317,21 +329,21 @@ func (p *Proxy) reviewToken(rw http.ResponseWriter, req *http.Request) bool {
317
329
req .RemoteAddr )
318
330
319
331
ok , err := p .tokenReviewer .Review (req )
332
+ if err != nil {
333
+ klog .Errorf ("unable to authenticate the request via TokenReview due to an error (%s): %s" ,
334
+ req .RemoteAddr , err )
335
+ return false
336
+ }
320
337
321
- // No error and ok so passthrough the request
322
- if err == nil && ok {
338
+ if ! ok {
323
339
klog .V (4 ).Infof ("passing request with valid token through (%s)" ,
324
340
req .RemoteAddr )
325
341
326
- return true
327
- }
328
-
329
- if err != nil {
330
- klog .Errorf ("unable to authenticate the request via TokenReview due to an error (%s): %s" ,
331
- req .RemoteAddr , err )
342
+ return false
332
343
}
333
344
334
- return false
345
+ // No error and ok so passthrough the request
346
+ return true
335
347
}
336
348
337
349
func (p * Proxy ) hasImpersonation (header http.Header ) bool {
0 commit comments