mirror of
https://github.com/h44z/wg-portal.git
synced 2026-11-20 12:43:26 +01:00
sec: ensure that IsAdmin flag in the session is always up to date
This commit is contained in:
@@ -109,7 +109,7 @@ func (e AuthEndpoint) RegisterRoutes(g *routegroup.Bundle) {
|
||||
apiGroup := g.Mount("/auth")
|
||||
|
||||
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}/callback", e.handleOauthCallbackGet())
|
||||
|
||||
@@ -18,7 +18,7 @@ const (
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -47,6 +47,12 @@ func (h AuthenticationHandler) LoggedIn(scopes ...Scope) func(next http.Handler)
|
||||
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...) {
|
||||
// Abort the request with the appropriate error code
|
||||
respond.JSON(w, http.StatusForbidden,
|
||||
@@ -54,14 +60,6 @@ func (h AuthenticationHandler) LoggedIn(scopes ...Scope) func(next http.Handler)
|
||||
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{
|
||||
Id: domain.UserIdentifier(session.UserIdentifier),
|
||||
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.
|
||||
// If the user is not logged in, the context user id is set to domain.CtxUnknownUserId.
|
||||
// InfoOnly adds current user info to the context without requiring a login.
|
||||
// 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 {
|
||||
return func(next http.Handler) http.Handler {
|
||||
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
|
||||
|
||||
if !session.LoggedIn {
|
||||
if !h.refreshSession(r.Context(), &session) {
|
||||
newContext = domain.SetUserInfo(r.Context(), domain.DefaultContextUserInfo())
|
||||
} else {
|
||||
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.
|
||||
// It must run after LoggedIn has refreshed the session role.
|
||||
func (h AuthenticationHandler) UserIdMatch(idParameter string) func(next http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -15,8 +15,8 @@ type stubV0UserAuthenticator struct {
|
||||
defaultPasswordInUse bool
|
||||
}
|
||||
|
||||
func (s stubV0UserAuthenticator) IsUserValid(_ context.Context, _ domain.UserIdentifier) bool {
|
||||
return s.valid
|
||||
func (s stubV0UserAuthenticator) GetValidUser(_ context.Context, id domain.UserIdentifier) (*domain.User, bool) {
|
||||
return &domain.User{Identifier: id}, s.valid
|
||||
}
|
||||
|
||||
func (s stubV0UserAuthenticator) IsDefaultPasswordInUse(_ context.Context) bool {
|
||||
|
||||
@@ -310,21 +310,27 @@ func (a *Authenticator) GetExternalLoginProviders(_ context.Context) []domain.Lo
|
||||
|
||||
// IsUserValid checks if a user is valid and not locked or disabled.
|
||||
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
|
||||
user, err := a.users.GetUser(ctx, id)
|
||||
if err != nil {
|
||||
return false
|
||||
if err != nil || user == nil {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if user.IsDisabled() {
|
||||
return false
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if user.IsLocked() {
|
||||
return false
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return true
|
||||
return user, true
|
||||
}
|
||||
|
||||
// region password authentication
|
||||
|
||||
Reference in New Issue
Block a user