Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
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
|
|
}
|