Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

feat(sso): implement SAML session storage for Single Logout support

rcourtmanda6ee7b1a63b
diff --git a/internal/api/saml_handlers.go b/internal/api/saml_handlers.go
index f1dbdb052e..5b7a37c878 100644
--- a/internal/api/saml_handlers.go
+++ b/internal/api/saml_handlers.go
@@ -3,6 +3,7 @@ package api
import (
"context"
"encoding/json"
+ "fmt"
"net/http"
"strings"
"sync"
@@ -385,16 +386,73 @@ type SAMLSessionInfo struct {
// establishSAMLSession creates a session for a SAML-authenticated user
func (r *Router) establishSAMLSession(w http.ResponseWriter, req *http.Request, username string, samlInfo *SAMLSessionInfo) error {
- // Use the existing session mechanism
- // For SAML, we don't have refresh tokens, so we pass nil for OIDC tokens
- return r.establishOIDCSession(w, req, username, nil)
+ token := generateSessionToken()
+ if token == "" {
+ return fmt.Errorf("failed to generate session token")
+ }
+
+ userAgent := req.Header.Get("User-Agent")
+ clientIP := GetClientIP(req)
+
+ // Convert SAMLSessionInfo to SAMLTokenInfo for storage
+ var samlTokens *SAMLTokenInfo
+ if samlInfo != nil {
+ samlTokens = &SAMLTokenInfo{
+ ProviderID: samlInfo.ProviderID,
+ NameID: samlInfo.NameID,
+ SessionIndex: samlInfo.SessionIndex,
+ }
+ }
+
+ // Create session with SAML info for SLO support
+ GetSessionStore().CreateSAMLSession(token, 24*time.Hour, userAgent, clientIP, username, samlTokens)
+
+ if username != "" {
+ TrackUserSession(username, token)
+ }
+
+ csrfToken := generateCSRFToken(token)
+ isSecure, sameSitePolicy := getCookieSettings(req)
+
+ http.SetCookie(w, &http.Cookie{
+ Name: "pulse_session",
+ Value: token,
+ Path: "/",
+ HttpOnly: true,
+ Secure: isSecure,
+ SameSite: sameSitePolicy,
+ MaxAge: 86400,
+ })
+
+ http.SetCookie(w, &http.Cookie{
+ Name: "pulse_csrf",
+ Value: csrfToken,
+ Path: "/",
+ Secure: isSecure,
+ SameSite: sameSitePolicy,
+ MaxAge: 86400,
+ })
+
+ return nil
}
// getSAMLSessionInfo retrieves SAML session info from the current session
func (r *Router) getSAMLSessionInfo(req *http.Request) *SAMLSessionInfo {
- // TODO: Implement retrieval of SAML session info from session storage
- // For now, return nil which causes fallback to regular logout
- return nil
+ cookie, err := req.Cookie("pulse_session")
+ if err != nil || cookie.Value == "" {
+ return nil
+ }
+
+ samlInfo := GetSessionStore().GetSAMLSessionInfo(cookie.Value)
+ if samlInfo == nil {
+ return nil
+ }
+
+ return &SAMLSessionInfo{
+ ProviderID: samlInfo.ProviderID,
+ NameID: samlInfo.NameID,
+ SessionIndex: samlInfo.SessionIndex,
+ }
}
// clearSession clears the current session
diff --git a/internal/api/session_store.go b/internal/api/session_store.go
index e12fd86d92..e9478e7c1b 100644
--- a/internal/api/session_store.go
+++ b/internal/api/session_store.go
@@ -40,6 +40,10 @@ type sessionPersisted struct {
OIDCIssuer string `json:"oidc_issuer,omitempty"`
OIDCClientID string `json:"oidc_client_id,omitempty"`
OIDCTokenRefreshing bool `json:"-"` // transient, not persisted
+ // SAML session fields for Single Logout (SLO) support
+ SAMLProviderID string `json:"saml_provider_id,omitempty"`
+ SAMLNameID string `json:"saml_name_id,omitempty"`
+ SAMLSessionIndex string `json:"saml_session_index,omitempty"`
}
// SessionData represents a user session
@@ -56,6 +60,10 @@ type SessionData struct {
OIDCIssuer string `json:"oidc_issuer,omitempty"` // IdP issuer URL
OIDCClientID string `json:"oidc_client_id,omitempty"` // OIDC client ID
OIDCTokenRefreshing bool `json:"-"` // Prevents concurrent refresh attempts
+ // SAML session fields for Single Logout (SLO) support
+ SAMLProviderID string `json:"saml_provider_id,omitempty"` // SAML IdP provider ID
+ SAMLNameID string `json:"saml_name_id,omitempty"` // SAML NameID from assertion
+ SAMLSessionIndex string `json:"saml_session_index,omitempty"` // SAML SessionIndex for SLO
}
// NewSessionStore creates a new persistent session store
@@ -145,6 +153,57 @@ func (s *SessionStore) CreateOIDCSession(token string, duration time.Duration, u
s.saveUnsafe()
}
+// SAMLTokenInfo contains SAML session information for Single Logout support
+type SAMLTokenInfo struct {
+ ProviderID string // SAML IdP provider ID
+ NameID string // SAML NameID from assertion
+ SessionIndex string // SAML SessionIndex for SLO
+}
+
+// CreateSAMLSession creates a new session with SAML session information
+func (s *SessionStore) CreateSAMLSession(token string, duration time.Duration, userAgent, ip, username string, saml *SAMLTokenInfo) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+
+ key := sessionHash(token)
+ session := &SessionData{
+ Username: username,
+ ExpiresAt: time.Now().Add(duration),
+ CreatedAt: time.Now(),
+ UserAgent: userAgent,
+ IP: ip,
+ OriginalDuration: duration,
+ }
+
+ if saml != nil {
+ session.SAMLProviderID = saml.ProviderID
+ session.SAMLNameID = saml.NameID
+ session.SAMLSessionIndex = saml.SessionIndex
+ }
+
+ s.sessions[key] = session
+
+ // Save immediately for important operations
+ s.saveUnsafe()
+}
+
+// GetSAMLSessionInfo returns SAML-specific session info for the given token
+func (s *SessionStore) GetSAMLSessionInfo(token string) *SAMLTokenInfo {
+ s.mu.RLock()
+ defer s.mu.RUnlock()
+
+ session, exists := s.sessions[sessionHash(token)]
+ if !exists || session.SAMLProviderID == "" {
+ return nil
+ }
+
+ return &SAMLTokenInfo{
+ ProviderID: session.SAMLProviderID,
+ NameID: session.SAMLNameID,
+ SessionIndex: session.SAMLSessionIndex,
+ }
+}
+
// GetSession returns a copy of the session data for the given token
func (s *SessionStore) GetSession(token string) *SessionData {
s.mu.RLock()
@@ -293,6 +352,9 @@ func (s *SessionStore) saveUnsafe() {
OIDCAccessTokenExp: session.OIDCAccessTokenExp,
OIDCIssuer: session.OIDCIssuer,
OIDCClientID: session.OIDCClientID,
+ SAMLProviderID: session.SAMLProviderID,
+ SAMLNameID: session.SAMLNameID,
+ SAMLSessionIndex: session.SAMLSessionIndex,
})
}
@@ -350,6 +412,9 @@ func (s *SessionStore) load() {
OIDCAccessTokenExp: entry.OIDCAccessTokenExp,
OIDCIssuer: entry.OIDCIssuer,
OIDCClientID: entry.OIDCClientID,
+ SAMLProviderID: entry.SAMLProviderID,
+ SAMLNameID: entry.SAMLNameID,
+ SAMLSessionIndex: entry.SAMLSessionIndex,
}
}
log.Info().Int("loaded", len(s.sessions)).Int("total", len(persisted)).Msg("Sessions loaded from disk (hashed format)")