feat: add saml model & controller [CW-2958] (#12289)
This PR adds the foundation for account-level SAML SSO configuration in Chatwoot Enterprise. It introduces a new `AccountSamlSettings` model and management API that allows accounts to configure their own SAML identity providers independently, this also includes the certificate generation flow The implementation includes a new controller (`Api::V1::Accounts::SamlSettingsController`) that provides CRUD operations for SAML configuration The feature is properly gated behind the 'saml' feature flag and includes administrator-only authorization via Pundit policies.
This commit is contained in:
59
enterprise/app/models/account_saml_settings.rb
Normal file
59
enterprise/app/models/account_saml_settings.rb
Normal file
@@ -0,0 +1,59 @@
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: account_saml_settings
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# certificate :text
|
||||
# role_mappings :json
|
||||
# sso_url :string
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# account_id :bigint not null
|
||||
# idp_entity_id :string
|
||||
# sp_entity_id :string
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_account_saml_settings_on_account_id (account_id)
|
||||
#
|
||||
class AccountSamlSettings < ApplicationRecord
|
||||
belongs_to :account
|
||||
|
||||
validates :account_id, presence: true
|
||||
validates :sso_url, presence: true
|
||||
validates :certificate, presence: true
|
||||
validates :idp_entity_id, presence: true
|
||||
|
||||
before_validation :set_sp_entity_id, if: :sp_entity_id_needs_generation?
|
||||
|
||||
def saml_enabled?
|
||||
sso_url.present? && certificate.present?
|
||||
end
|
||||
|
||||
def certificate_fingerprint
|
||||
return nil if certificate.blank?
|
||||
|
||||
begin
|
||||
cert = OpenSSL::X509::Certificate.new(certificate)
|
||||
OpenSSL::Digest::SHA1.new(cert.to_der).hexdigest
|
||||
.upcase.gsub(/(.{2})(?=.)/, '\1:')
|
||||
rescue OpenSSL::X509::CertificateError
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_sp_entity_id
|
||||
base_url = GlobalConfigService.load('FRONTEND_URL', 'http://localhost:3000')
|
||||
self.sp_entity_id = "#{base_url}/saml/sp/#{account_id}"
|
||||
end
|
||||
|
||||
def sp_entity_id_needs_generation?
|
||||
sp_entity_id.blank?
|
||||
end
|
||||
|
||||
def installation_name
|
||||
GlobalConfigService.load('INSTALLATION_NAME', 'Chatwoot')
|
||||
end
|
||||
end
|
||||
@@ -27,4 +27,8 @@ module Enterprise::Account
|
||||
def unmark_for_deletion
|
||||
custom_attributes.delete('marked_for_deletion_at') && custom_attributes.delete('marked_for_deletion_reason') && save
|
||||
end
|
||||
|
||||
def saml_enabled?
|
||||
saml_settings&.saml_enabled? || false
|
||||
end
|
||||
end
|
||||
|
||||
@@ -13,5 +13,7 @@ module Enterprise::Concerns::Account
|
||||
|
||||
has_many :copilot_threads, dependent: :destroy_async
|
||||
has_many :voice_channels, dependent: :destroy_async, class_name: '::Channel::Voice'
|
||||
|
||||
has_one :saml_settings, dependent: :destroy_async, class_name: 'AccountSamlSettings'
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user