@@ -465,11 +465,15 @@ func (s *Server) authenticateAndServeApp(w http.ResponseWriter, r *http.Request,
465465 }
466466
467467 userId := ""
468- appAuthString := string (appAuth )
469- if appAuth == types .AppAuthnNone {
468+
469+ // Remove the RBAC_AUTH_PREFIX rbac: prefix
470+ strippedAuthStr := strings .TrimPrefix (string (appAuth ), RBAC_AUTH_PREFIX )
471+ strippedAuth := types .AppAuthnType (strippedAuthStr )
472+
473+ if strippedAuth == types .AppAuthnNone {
470474 // No authentication required
471475 userId = types .ANONYMOUS_USER
472- } else if appAuth == types .AppAuthnSystem {
476+ } else if strippedAuth == types .AppAuthnSystem {
473477 // Use system admin user for authentication
474478 authStatus := s .authHandler .authenticate (r .Header .Get ("Authorization" ))
475479 if ! authStatus {
@@ -478,27 +482,27 @@ func (s *Server) authenticateAndServeApp(w http.ResponseWriter, r *http.Request,
478482 return
479483 }
480484 userId = types .ADMIN_USER // not using the actual user id, just a admin placeholder
481- } else if appAuthString == "cert" || strings .HasPrefix (appAuthString , "cert_" ) {
485+ } else if strippedAuthStr == "cert" || strings .HasPrefix (strippedAuthStr , "cert_" ) {
482486 // Use client certificate authentication
483487 if s .config .Https .DisableClientCerts {
484488 http .Error (w , "Client certificates are disabled in openrun.config, update https.disable_client_certs" , http .StatusInternalServerError )
485489 return
486490 }
487- err = s .verifyClientCerts (r , appAuthString )
491+ err = s .verifyClientCerts (r , strippedAuthStr )
488492 if err != nil {
489493 http .Error (w , err .Error (), http .StatusUnauthorized )
490494 return
491495 }
492- userId = appAuthString
496+ userId = strippedAuthStr
493497 } else {
494498 // Use SSO auth
495- if ! s .ssoAuth .ValidateProviderName (appAuthString ) {
496- http .Error (w , "Unsupported authentication provider: " + appAuthString , http .StatusInternalServerError )
499+ if ! s .ssoAuth .ValidateProviderName (strippedAuthStr ) {
500+ http .Error (w , "Unsupported authentication provider: " + strippedAuthStr , http .StatusInternalServerError )
497501 return
498502 }
499503
500504 // Redirect to the auth provider if not logged in
501- userId , err = s .ssoAuth .CheckAuth (w , r , appAuthString , true )
505+ userId , err = s .ssoAuth .CheckAuth (w , r , strippedAuthStr , true )
502506 if err != nil {
503507 http .Error (w , err .Error (), http .StatusInternalServerError )
504508 }
@@ -507,8 +511,19 @@ func (s *Server) authenticateAndServeApp(w http.ResponseWriter, r *http.Request,
507511 }
508512 }
509513
514+ s .Trace ().Msgf ("Authenticated user %s, doing authorization check" , userId )
515+ authorized , err := s .rbacManager .AuthorizeAccess (userId , app .AppEntry .AppPathDomain (), string (appAuth ), types .PermissionAccess )
516+ if err != nil {
517+ http .Error (w , err .Error (), http .StatusInternalServerError )
518+ return
519+ }
520+ if ! authorized {
521+ s .Warn ().Msgf ("User %s is not authorized to access app %s" , userId , app .AppEntry .AppPathDomain ())
522+ http .Error (w , fmt .Sprintf ("Unauthorized : %s does not have access to %s" , userId , app .AppEntry .AppPathDomain ()), http .StatusUnauthorized )
523+ return
524+ }
525+
510526 // Create a new context with the user ID
511- s .Trace ().Msgf ("Authenticated user %s" , userId )
512527 ctx := context .WithValue (r .Context (), types .USER_ID , userId )
513528 ctx = context .WithValue (ctx , types .APP_ID , string (app .Id ))
514529
0 commit comments