sec: ensure that IsAdmin flag in the session is always up to date

This commit is contained in:
Christoph
2026-11-09 21:46:54 +01:00
parent 8485ea9774
commit 440a619aea
4 changed files with 43 additions and 20 deletions
@@ -109,7 +109,7 @@ func (e AuthEndpoint) RegisterRoutes(g *routegroup.Bundle) {
apiGroup := g.Mount("/auth") apiGroup := g.Mount("/auth")
apiGroup.HandleFunc("GET /providers", e.handleExternalLoginProvidersGet()) apiGroup.HandleFunc("GET /providers", e.handleExternalLoginProvidersGet())
apiGroup.HandleFunc("GET /session", e.handleSessionInfoGet()) apiGroup.With(e.authenticator.InfoOnly()).HandleFunc("GET /session", e.handleSessionInfoGet())
apiGroup.HandleFunc("GET /login/{provider}/init", e.handleOauthInitiateGet()) apiGroup.HandleFunc("GET /login/{provider}/init", e.handleOauthInitiateGet())
apiGroup.HandleFunc("GET /login/{provider}/callback", e.handleOauthCallbackGet()) apiGroup.HandleFunc("GET /login/{provider}/callback", e.handleOauthCallbackGet())
@@ -18,7 +18,7 @@ const (
) )
type UserAuthenticator interface { type UserAuthenticator interface {
IsUserValid(ctx context.Context, id domain.UserIdentifier) bool GetValidUser(ctx context.Context, id domain.UserIdentifier) (*domain.User, bool)
IsDefaultPasswordInUse(ctx context.Context) bool IsDefaultPasswordInUse(ctx context.Context) bool
} }
@@ -47,6 +47,12 @@ func (h AuthenticationHandler) LoggedIn(scopes ...Scope) func(next http.Handler)
return return
} }
if !h.refreshSession(r.Context(), &session) {
respond.JSON(w, http.StatusUnauthorized,
model.Error{Code: http.StatusUnauthorized, Message: "session no longer available"})
return
}
if !UserHasScopes(session, scopes...) { if !UserHasScopes(session, scopes...) {
// Abort the request with the appropriate error code // Abort the request with the appropriate error code
respond.JSON(w, http.StatusForbidden, respond.JSON(w, http.StatusForbidden,
@@ -54,14 +60,6 @@ func (h AuthenticationHandler) LoggedIn(scopes ...Scope) func(next http.Handler)
return return
} }
// Check if logged-in user is still valid
if !h.authenticator.IsUserValid(r.Context(), domain.UserIdentifier(session.UserIdentifier)) {
h.session.DestroyData(r.Context())
respond.JSON(w, http.StatusUnauthorized,
model.Error{Code: http.StatusUnauthorized, Message: "session no longer available"})
return
}
ctx := context.WithValue(r.Context(), domain.CtxUserInfo, &domain.ContextUserInfo{ ctx := context.WithValue(r.Context(), domain.CtxUserInfo, &domain.ContextUserInfo{
Id: domain.UserIdentifier(session.UserIdentifier), Id: domain.UserIdentifier(session.UserIdentifier),
IsAdmin: session.IsAdmin, IsAdmin: session.IsAdmin,
@@ -74,8 +72,8 @@ func (h AuthenticationHandler) LoggedIn(scopes ...Scope) func(next http.Handler)
} }
} }
// InfoOnly only checks if the user is logged in and adds the user id to the context. // InfoOnly adds current user info to the context without requiring a login.
// If the user is not logged in, the context user id is set to domain.CtxUnknownUserId. // If the user is not logged in or no longer valid, the context user id is set to domain.CtxUnknownUserId.
func (h AuthenticationHandler) InfoOnly() func(next http.Handler) http.Handler { func (h AuthenticationHandler) InfoOnly() func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -83,7 +81,7 @@ func (h AuthenticationHandler) InfoOnly() func(next http.Handler) http.Handler {
var newContext context.Context var newContext context.Context
if !session.LoggedIn { if !h.refreshSession(r.Context(), &session) {
newContext = domain.SetUserInfo(r.Context(), domain.DefaultContextUserInfo()) newContext = domain.SetUserInfo(r.Context(), domain.DefaultContextUserInfo())
} else { } else {
newContext = domain.SetUserInfo(r.Context(), &domain.ContextUserInfo{ newContext = domain.SetUserInfo(r.Context(), &domain.ContextUserInfo{
@@ -100,7 +98,26 @@ func (h AuthenticationHandler) InfoOnly() func(next http.Handler) http.Handler {
} }
} }
// refreshSession validates the account and refreshes its role before authorization.
func (h AuthenticationHandler) refreshSession(ctx context.Context, session *SessionData) bool {
if !session.LoggedIn {
return false
}
user, valid := h.authenticator.GetValidUser(ctx, domain.UserIdentifier(session.UserIdentifier))
if !valid {
h.session.DestroyData(ctx)
return false
}
if session.IsAdmin != user.IsAdmin {
session.IsAdmin = user.IsAdmin
h.session.SetData(ctx, *session)
}
return true
}
// UserIdMatch checks if the user id in the session matches the user id in the request. If not, the request is aborted. // UserIdMatch checks if the user id in the session matches the user id in the request. If not, the request is aborted.
// It must run after LoggedIn has refreshed the session role.
func (h AuthenticationHandler) UserIdMatch(idParameter string) func(next http.Handler) http.Handler { func (h AuthenticationHandler) UserIdMatch(idParameter string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -15,8 +15,8 @@ type stubV0UserAuthenticator struct {
defaultPasswordInUse bool defaultPasswordInUse bool
} }
func (s stubV0UserAuthenticator) IsUserValid(_ context.Context, _ domain.UserIdentifier) bool { func (s stubV0UserAuthenticator) GetValidUser(_ context.Context, id domain.UserIdentifier) (*domain.User, bool) {
return s.valid return &domain.User{Identifier: id}, s.valid
} }
func (s stubV0UserAuthenticator) IsDefaultPasswordInUse(_ context.Context) bool { func (s stubV0UserAuthenticator) IsDefaultPasswordInUse(_ context.Context) bool {
+11 -5
View File
@@ -310,21 +310,27 @@ func (a *Authenticator) GetExternalLoginProviders(_ context.Context) []domain.Lo
// IsUserValid checks if a user is valid and not locked or disabled. // IsUserValid checks if a user is valid and not locked or disabled.
func (a *Authenticator) IsUserValid(ctx context.Context, id domain.UserIdentifier) bool { func (a *Authenticator) IsUserValid(ctx context.Context, id domain.UserIdentifier) bool {
_, valid := a.GetValidUser(ctx, id)
return valid
}
// GetValidUser returns the current user if the account exists and is not locked or disabled.
func (a *Authenticator) GetValidUser(ctx context.Context, id domain.UserIdentifier) (*domain.User, bool) {
ctx = domain.SetUserInfo(ctx, domain.SystemAdminContextUserInfo()) // switch to admin user context ctx = domain.SetUserInfo(ctx, domain.SystemAdminContextUserInfo()) // switch to admin user context
user, err := a.users.GetUser(ctx, id) user, err := a.users.GetUser(ctx, id)
if err != nil { if err != nil || user == nil {
return false return nil, false
} }
if user.IsDisabled() { if user.IsDisabled() {
return false return nil, false
} }
if user.IsLocked() { if user.IsLocked() {
return false return nil, false
} }
return true return user, true
} }
// region password authentication // region password authentication