Skip to content

Commit 1a38d6e

Browse files
authored
Fix missing annotation for no-redirect, fix middleware name (#543)
* Ability to set no-redirects=true per path * Ability to set no-redirects=true per path * Fix missing annotation for no-redirect, fix middleware name
1 parent 5349c42 commit 1a38d6e

File tree

3 files changed

+2
-103
lines changed

3 files changed

+2
-103
lines changed

pkg/authorization/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type Resource struct {
3434
// WhiteListed permits the prefix through
3535
WhiteListed bool `json:"white-listed" yaml:"white-listed"`
3636
// NoRedirect overrides global no-redirect setting
37-
NoRedirect bool
37+
NoRedirect bool `json:"no-redirect" yaml:"no-redirect"`
3838
// RequireAnyRole indicates that ANY of the roles are required, the default is all
3939
RequireAnyRole bool `json:"require-any-role" yaml:"require-any-role"`
4040
// Headers required to access this url

pkg/keycloak/proxy/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ func (r *OauthProxy) CreateReverseProxy() error {
517517
r.Config.DefaultAllowedQueryParams,
518518
)
519519

520-
redToAuthMiddleware := gmiddleware.NewRedirectToAuthorizationMiddleware(
520+
redToAuthMiddleware := gmiddleware.RedirectToAuthorizationMiddleware(
521521
r.Log,
522522
r.Cm,
523523
r.Config.SkipTokenVerification,

pkg/proxy/middleware/oauth.go

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -328,107 +328,6 @@ func AuthenticationMiddleware(
328328
//
329329
//nolint:cyclop
330330
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(
432331
logger *zap.Logger,
433332
cookManager *cookie.Manager,
434333
skipTokenVerification bool,

0 commit comments

Comments
 (0)