@@ -328,107 +328,6 @@ func AuthenticationMiddleware(
328
328
//
329
329
//nolint:cyclop
330
330
func RedirectToAuthorizationMiddleware (
331
- logger * zap.Logger ,
332
- noRedirects bool ,
333
- cookManager * cookie.Manager ,
334
- skipTokenVerification bool ,
335
- noProxy bool ,
336
- baseURI string ,
337
- oAuthURI string ,
338
- allowedQueryParams map [string ]string ,
339
- defaultAllowedQueryParams map [string ]string ,
340
- ) func (http.Handler ) http.Handler {
341
- return func (next http.Handler ) http.Handler {
342
- return http .HandlerFunc (func (wrt http.ResponseWriter , req * http.Request ) {
343
- scope , assertOk := req .Context ().Value (constant .ContextScopeName ).(* models.RequestScope )
344
- if ! assertOk {
345
- logger .Error (apperrors .ErrAssertionFailed .Error ())
346
- return
347
- }
348
-
349
- scope .Logger .Debug ("redirecttoauthorization middleware" )
350
-
351
- if scope .AccessDenied {
352
- if noRedirects {
353
- wrt .WriteHeader (http .StatusUnauthorized )
354
- return
355
- }
356
-
357
- // step: add a state referrer to the authorization page
358
- uuid := cookManager .DropStateParameterCookie (req , wrt )
359
- authQuery := "?state=" + uuid
360
-
361
- if len (allowedQueryParams ) > 0 {
362
- query := ""
363
- for key , val := range allowedQueryParams {
364
- if param := req .URL .Query ().Get (key ); param != "" {
365
- if val != "" {
366
- if val != param {
367
- wrt .WriteHeader (http .StatusForbidden )
368
- }
369
- }
370
- query += fmt .Sprintf ("&%s=%s" , key , param )
371
- } else {
372
- if val , ok := defaultAllowedQueryParams [key ]; ok {
373
- query += fmt .Sprintf ("&%s=%s" , key , val )
374
- }
375
- }
376
- }
377
- authQuery += query
378
- }
379
-
380
- // step: if verification is switched off, we can't authorization
381
- if skipTokenVerification {
382
- logger .Error (
383
- "refusing to redirection to authorization endpoint, " +
384
- "skip token verification switched on" ,
385
- )
386
-
387
- wrt .WriteHeader (http .StatusForbidden )
388
- return
389
- }
390
-
391
- url := utils .WithOAuthURI (baseURI , oAuthURI )(constant .AuthorizationURL + authQuery )
392
-
393
- if noProxy && ! noRedirects {
394
- xForwardedHost := req .Header .Get (constant .HeaderXForwardedHost )
395
- xProto := req .Header .Get (constant .HeaderXForwardedProto )
396
-
397
- if xForwardedHost == "" || xProto == "" {
398
- logger .Error (apperrors .ErrForwardAuthMissingHeaders .Error ())
399
-
400
- wrt .WriteHeader (http .StatusForbidden )
401
- return
402
- }
403
-
404
- url = fmt .Sprintf (
405
- "%s://%s%s" ,
406
- xProto ,
407
- xForwardedHost ,
408
- url ,
409
- )
410
- }
411
-
412
- logger .Debug ("redirecting to url" , zap .String ("url" , url ))
413
-
414
- core .RedirectToURL (
415
- logger ,
416
- url ,
417
- wrt ,
418
- req ,
419
- http .StatusSeeOther ,
420
- )
421
- } else {
422
- next .ServeHTTP (wrt , req )
423
- }
424
- })
425
- }
426
- }
427
-
428
- // RedirectToAuthorizationMiddleware redirects the user to authorization handler
429
- //
430
- //nolint:cyclop
431
- func NewRedirectToAuthorizationMiddleware (
432
331
logger * zap.Logger ,
433
332
cookManager * cookie.Manager ,
434
333
skipTokenVerification bool ,
0 commit comments