93 lines
2.9 KiB
Go
93 lines
2.9 KiB
Go
package repository
|
|
|
|
// Reference: M13 §5 — SAML IdP configuration repository
|
|
// Per-account SAML IdP configuration for multi-tenant identity isolation.
|
|
// Each account (tenant) can configure its own SAML IdP, enabling enterprise
|
|
// customers to bring their own IdP while maintaining identity isolation.
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// SAMLIdPConfigRepo manages per-account SAML IdP configuration in the database.
|
|
type SAMLIdPConfigRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewSAMLIdPConfigRepo creates a SAML IdP configuration repository.
|
|
func NewSAMLIdPConfigRepo(db *gorm.DB) *SAMLIdPConfigRepo {
|
|
return &SAMLIdPConfigRepo{db: db}
|
|
}
|
|
|
|
// GetActiveByAccount retrieves the active SAML IdP config for an account.
|
|
// Returns nil if no active config exists for the account.
|
|
func (r *SAMLIdPConfigRepo) GetActiveByAccount(accountID uint) (*model.SAMLIdPConfig, error) {
|
|
var config model.SAMLIdPConfig
|
|
err := r.db.Where("account_id = ? AND active = ?", accountID, true).First(&config).Error
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &config, nil
|
|
}
|
|
|
|
// GetByAccount retrieves any SAML IdP config for an account (including inactive).
|
|
func (r *SAMLIdPConfigRepo) GetByAccount(accountID uint) (*model.SAMLIdPConfig, error) {
|
|
var config model.SAMLIdPConfig
|
|
err := r.db.Where("account_id = ?", accountID).First(&config).Error
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &config, nil
|
|
}
|
|
|
|
// Create creates a new SAML IdP config for an account.
|
|
func (r *SAMLIdPConfigRepo) Create(config *model.SAMLIdPConfig) error {
|
|
return r.db.Create(config).Error
|
|
}
|
|
|
|
// Update updates an existing SAML IdP config.
|
|
func (r *SAMLIdPConfigRepo) Update(config *model.SAMLIdPConfig) error {
|
|
return r.db.Save(config).Error
|
|
}
|
|
|
|
// Delete soft-deletes an SAML IdP config.
|
|
func (r *SAMLIdPConfigRepo) Delete(accountID uint) error {
|
|
return r.db.Where("account_id = ?", accountID).Delete(&model.SAMLIdPConfig{}).Error
|
|
}
|
|
|
|
// ListByAccount retrieves all SAML IdP configs for an account (including history).
|
|
func (r *SAMLIdPConfigRepo) ListByAccount(accountID uint) ([]model.SAMLIdPConfig, error) {
|
|
var configs []model.SAMLIdPConfig
|
|
err := r.db.Where("account_id = ?", accountID).Find(&configs).Error
|
|
return configs, err
|
|
}
|
|
|
|
// GetAttributeMapping parses the JSON attribute mapping string into a struct.
|
|
func (r *SAMLIdPConfigRepo) GetAttributeMapping(config *model.SAMLIdPConfig) (*model.SAMLAttributeMapping, error) {
|
|
if config.AttributeMapping == "" {
|
|
// Default mapping
|
|
return &model.SAMLAttributeMapping{
|
|
UID: "nameid",
|
|
Email: "email",
|
|
FirstName: "givenName",
|
|
LastName: "surname",
|
|
}, nil
|
|
}
|
|
|
|
var mapping model.SAMLAttributeMapping
|
|
if err := json.Unmarshal([]byte(config.AttributeMapping), &mapping); err != nil {
|
|
return nil, err
|
|
}
|
|
return &mapping, nil
|
|
}
|