125 lines
3.8 KiB
Go
125 lines
3.8 KiB
Go
package repository
|
|
|
|
// Reference: M13 §6 — SSO Session repository
|
|
// Database persistence layer for SSO sessions. While the Redis-backed
|
|
// SSOSessionStore handles fast lookup and SLO propagation, this repo
|
|
// provides durable storage for audit trails and session history.
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// SSOSessionRepo manages SSO session records in the database.
|
|
type SSOSessionRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewSSOSessionRepo creates an SSO session repository.
|
|
func NewSSOSessionRepo(db *gorm.DB) *SSOSessionRepo {
|
|
return &SSOSessionRepo{db: db}
|
|
}
|
|
|
|
// Create persists a new SSO session record.
|
|
func (r *SSOSessionRepo) Create(session *model.SSOSession) error {
|
|
return r.db.Create(session).Error
|
|
}
|
|
|
|
// GetByID retrieves an SSO session by its primary key ID.
|
|
func (r *SSOSessionRepo) GetByID(id uint) (*model.SSOSession, error) {
|
|
var session model.SSOSession
|
|
err := r.db.First(&session, id).Error
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &session, nil
|
|
}
|
|
|
|
// GetBySessionID retrieves an SSO session by the unique session_id field.
|
|
func (r *SSOSessionRepo) GetBySessionID(sessionID string) (*model.SSOSession, error) {
|
|
var session model.SSOSession
|
|
err := r.db.Where("session_id = ?", sessionID).First(&session).Error
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &session, nil
|
|
}
|
|
|
|
// GetActiveByUser retrieves all active SSO sessions for a user.
|
|
func (r *SSOSessionRepo) GetActiveByUser(userID uint) ([]model.SSOSession, error) {
|
|
var sessions []model.SSOSession
|
|
err := r.db.Where("user_id = ? AND status = ? AND ended_at IS NULL", userID, "active").
|
|
Order("created_at DESC").
|
|
Find(&sessions).Error
|
|
return sessions, err
|
|
}
|
|
|
|
// GetActiveByIdP retrieves all active SSO sessions for a specific IdP entity ID.
|
|
func (r *SSOSessionRepo) GetActiveByIdP(idpEntityID string) ([]model.SSOSession, error) {
|
|
var sessions []model.SSOSession
|
|
err := r.db.Where("idp_entity_id = ? AND status = ? AND ended_at IS NULL", idpEntityID, "active").
|
|
Order("created_at DESC").
|
|
Find(&sessions).Error
|
|
return sessions, err
|
|
}
|
|
|
|
// TerminateSession marks an SSO session as terminated.
|
|
func (r *SSOSessionRepo) TerminateSession(sessionID string) error {
|
|
now := time.Now()
|
|
return r.db.Model(&model.SSOSession{}).
|
|
Where("session_id = ? AND status = ?", sessionID, "active").
|
|
Updates(map[string]interface{}{
|
|
"status": "terminated",
|
|
"ended_at": now,
|
|
}).Error
|
|
}
|
|
|
|
// TerminateUserSessions terminates all active SSO sessions for a user.
|
|
func (r *SSOSessionRepo) TerminateUserSessions(userID uint) (int64, error) {
|
|
now := time.Now()
|
|
result := r.db.Model(&model.SSOSession{}).
|
|
Where("user_id = ? AND status = ?", userID, "active").
|
|
Updates(map[string]interface{}{
|
|
"status": "terminated",
|
|
"ended_at": now,
|
|
})
|
|
return result.RowsAffected, result.Error
|
|
}
|
|
|
|
// TerminateIdPSessions terminates all active SSO sessions for an IdP.
|
|
func (r *SSOSessionRepo) TerminateIdPSessions(idpEntityID string) (int64, error) {
|
|
now := time.Now()
|
|
result := r.db.Model(&model.SSOSession{}).
|
|
Where("idp_entity_id = ? AND status = ?", idpEntityID, "active").
|
|
Updates(map[string]interface{}{
|
|
"status": "terminated",
|
|
"ended_at": now,
|
|
})
|
|
return result.RowsAffected, result.Error
|
|
}
|
|
|
|
// UpdateLastSeen updates the last_seen_at timestamp for a session.
|
|
func (r *SSOSessionRepo) UpdateLastSeen(sessionID string) error {
|
|
return r.db.Model(&model.SSOSession{}).
|
|
Where("session_id = ?", sessionID).
|
|
Update("last_seen_at", time.Now()).Error
|
|
}
|
|
|
|
// CountActiveByUser returns the number of active SSO sessions for a user.
|
|
func (r *SSOSessionRepo) CountActiveByUser(userID uint) (int64, error) {
|
|
var count int64
|
|
err := r.db.Model(&model.SSOSession{}).
|
|
Where("user_id = ? AND status = ? AND ended_at IS NULL", userID, "active").
|
|
Count(&count).Error
|
|
return count, err
|
|
}
|