Skip to content

Commit 5c69607

Browse files
committed
Add app access auth check
1 parent d76c373 commit 5c69607

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

internal/server/app_apis.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

internal/server/sso_auth.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
"net/http"
1010
"strings"
1111

12-
"github.com/openrundev/openrun/internal/types"
1312
"github.com/go-chi/chi"
1413
"github.com/gorilla/sessions"
1514
"github.com/markbates/goth"
1615
"github.com/markbates/goth/gothic"
16+
"github.com/openrundev/openrun/internal/types"
1717

1818
"github.com/markbates/goth/providers/amazon"
1919
"github.com/markbates/goth/providers/auth0"
@@ -282,6 +282,7 @@ func (s *SSOAuth) ValidateProviderName(provider string) bool {
282282
}
283283

284284
func (s *SSOAuth) ValidateAuthType(authType string) bool {
285+
authType = strings.TrimPrefix(authType, RBAC_AUTH_PREFIX)
285286
switch authType {
286287
case string(types.AppAuthnDefault), string(types.AppAuthnSystem), string(types.AppAuthnNone):
287288
return true

0 commit comments

Comments
 (0)