Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Don't store as settings, but as record

Oliver Güntherd1e76d8d202d
diff --git a/modules/auth_saml/app/controllers/saml/providers_controller.rb b/modules/auth_saml/app/controllers/saml/providers_controller.rb
index ec8b4b9f120..2e9e42b006d 100644
--- a/modules/auth_saml/app/controllers/saml/providers_controller.rb
+++ b/modules/auth_saml/app/controllers/saml/providers_controller.rb
@@ -7,12 +7,15 @@ module Saml
before_action :check_ee
before_action :find_provider, only: %i[show edit update destroy]
- def index; end
+ def index
+ @providers = Saml::Provider.all
+ end
+
def edit; end
def show; end
def new
- @provider = ::Saml::Provider.new(name: "saml")
+ @provider = ::Saml::Provider.new
end
def import
diff --git a/modules/auth_saml/app/models/saml/provider.rb b/modules/auth_saml/app/models/saml/provider.rb
index 3934e83fbdb..b4166f4b6bf 100644
--- a/modules/auth_saml/app/models/saml/provider.rb
+++ b/modules/auth_saml/app/models/saml/provider.rb
@@ -1,148 +1,76 @@
module Saml
- class Provider < OpenStruct
- DEFAULT_NAME_IDENTIFIER_FORMAT = 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress'.freeze
-
- include ActiveModel::Validations
- include ActiveModel::Conversion
-
- attr_accessor :readonly
- validates_presence_of :display_name
+ class Provider < ApplicationRecord
+ self.table_name = "saml_providers"
- def initialize(readonly: false, **attributes)
- self.readonly = readonly
- super(attributes)
+ DEFAULT_NAME_IDENTIFIER_FORMAT = 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress'.freeze
+ DEFAULT_MAIL_MAPPING = %w[mail email Email emailAddress emailaddress
+ urn:oid:0.9.2342.19200300.100.1.3 http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress].freeze
- set_default_attributes(attributes)
- end
+ DEFAULT_FIRSTNAME_MAPPING = %w[givenName givenname given_name given_name
+ urn:oid:2.5.4.42 http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname].freeze
- def id
- name
- end
+ DEFAULT_LASTNAME_MAPPING = %w[surname sur_name sn
+ urn:oid:2.5.4.4 http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname].freeze
- def name
- super.presence || "saml-#{uuid}"
- end
+ DEFAULT_REQUESTED_ATTRIBUTES = [
+ {
+ "name" => "mail",
+ "name_format" => "urn:oasis:names:tc:SAML:2.0:attrname-format:basic",
+ "friendly_name" => "Email address",
+ "is_required" => true
+ },
+ {
+ "name" => "givenName",
+ "name_format" => "urn:oasis:names:tc:SAML:2.0:attrname-format:basic",
+ "friendly_name" => "Given name",
+ "is_required" => true
+ },
+ {
+ "name" => "sn",
+ "name_format" => "urn:oasis:names:tc:SAML:2.0:attrname-format:basic",
+ "friendly_name" => "Family name",
+ "is_required" => true
+ }
+ ].freeze
- def sp_entity_id
- super.presence || issuer.presence || url_helpers.root_url
- end
+ store_attribute :options, :sp_entity_id, :string, default: -> { OpenProject::StaticRouting::StaticUrlHelpers.new.root_url }
+ store_attribute :options, :name_identifier_format, :string, default: -> { DEFAULT_NAME_IDENTIFIER_FORMAT }
+ store_attribute :options, :metadata_url, :string
+ store_attribute :options, :metadata_xml, :string
- def attribute_statements
- super.presence || {}
- end
+ store_attribute :options, :mapping_login, :json, default: -> { DEFAULT_MAIL_MAPPING }
+ store_attribute :options, :mapping_mail, :json, default: -> { DEFAULT_MAIL_MAPPING }
+ store_attribute :options, :mapping_firstname, :json, default: -> { DEFAULT_FIRSTNAME_MAPPING }
+ store_attribute :options, :mapping_lastname, :json, default: -> { DEFAULT_LASTNAME_MAPPING }
+ store_attribute :options, :mapping_uid, :json
- %w[login email first_name last_name].each do |accessor|
- define_method("#{accessor}_mapping") do
- value = attribute_statements[accessor]
- warn value
- if value.is_a?(Array)
- value.join(', ')
- else
- value
- end
- end
+ store_attribute :options, :request_attributes, :json, default: -> { DEFAULT_REQUESTED_ATTRIBUTES }
- define_method("#{accessor}_mapping=") do |newval|
- parsed = newval.split(/\s*,\s*/)
- attribute_statements[accessor] = parsed
- end
- end
+ attr_accessor :readonly
+ validates_presence_of :display_name
- def new_record?
- !persisted?
+ def slug
+ "saml-#{id}"
end
- def persisted?
- uuid.present?
+ def assertion_consumer_service_url
+ URI.join(url_helpers.root_url, "/auth/#{name}/callback").to_s
end
def limit_self_registration?
limit_self_registration
end
- def save
- return false unless valid?
-
- Setting.plugin_openproject_auth_saml = setting_with_provider
-
- true
- end
-
- def destroy
- Setting.plugin_openproject_auth_saml = setting_without_provider
-
- true
- end
-
- def setting_with_provider
- setting.deep_merge "providers" => { name => to_h.stringify_keys }
- end
-
def to_h
- super
- .reverse_merge(uuid: SecureRandom.uuid)
- .merge(derived_attributes)
- end
-
- def setting_without_provider
- setting.tap do |s|
- s["providers"].delete_if { |_, config| config['id'] == id }
- end
- end
-
- def setting
- Hash(Setting.plugin_openproject_auth_saml).tap do |h|
- h["providers"] ||= Hash.new
- end
+ options
+ .merge(
+ name: slug,
+ display_name:,
+ assertion_consumer_service_url:,
+ )
end
private
- def set_default_attributes(attributes)
- self.limit_self_registration = attributes.fetch(:limit_self_registration, false)
- self.name_identifier_format = attributes.fetch(:name_identifier_format, DEFAULT_NAME_IDENTIFIER_FORMAT)
- self.issuer = attributes.fetch(:issuer, url_helpers.root_url)
- self.request_attributes = attributes.fetch(:request_attributes, default_request_attributes)
- self.uuid ||= SecureRandom.hex(4)
- end
-
- def default_request_attributes
- [
- {
- "name" => "mail",
- "name_format" => "urn:oasis:names:tc:SAML:2.0:attrname-format:basic",
- "friendly_name" => "Email address",
- "is_required" => true
- },
- {
- "name" => "givenName",
- "name_format" => "urn:oasis:names:tc:SAML:2.0:attrname-format:basic",
- "friendly_name" => "Given name",
- "is_required" => true
- },
- {
- "name" => "sn",
- "name_format" => "urn:oasis:names:tc:SAML:2.0:attrname-format:basic",
- "friendly_name" => "Family name",
- "is_required" => true
- },
- {
- "name" => "uid",
- "name_format" => "urn:oasis:names:tc:SAML:2.0:attrname-format:basic",
- "friendly_name" => "Stable unique ID / login of the user",
- "is_required" => true
- }
- ]
- end
-
- def derived_attributes
- {
- assertion_consumer_service_url: URI.join(url_helpers.root_url, "/auth/#{name}/callback").to_s,
- }
- end
-
- def url_helpers
- @url_helpers ||= OpenProject::StaticRouting::StaticUrlHelpers.new
- end
end
end
diff --git a/modules/auth_saml/app/views/saml/providers/index.html.erb b/modules/auth_saml/app/views/saml/providers/index.html.erb
index 98c08284058..16c1bcfb4ef 100644
--- a/modules/auth_saml/app/views/saml/providers/index.html.erb
+++ b/modules/auth_saml/app/views/saml/providers/index.html.erb
@@ -20,4 +20,4 @@
%>
<% end %>
-<%= render ::Saml::Providers::TableComponent.new(rows: providers) %>
+<%= render ::Saml::Providers::TableComponent.new(rows: @providers) %>
diff --git a/modules/auth_saml/app/views/saml/providers/new.html.erb b/modules/auth_saml/app/views/saml/providers/new.html.erb
index 6973dcdd6c8..34ada5f0688 100644
--- a/modules/auth_saml/app/views/saml/providers/new.html.erb
+++ b/modules/auth_saml/app/views/saml/providers/new.html.erb
@@ -18,6 +18,4 @@
<% end %>
<% end %>
-<%= error_messages_for @provider %>
-
<%= render(Saml::Providers::ViewComponent.new(@provider, edit_state: :name)) %>
diff --git a/modules/auth_saml/config/locales/en.yml b/modules/auth_saml/config/locales/en.yml
index c0dd0a05ae8..5baa6aeea47 100644
--- a/modules/auth_saml/config/locales/en.yml
+++ b/modules/auth_saml/config/locales/en.yml
@@ -21,6 +21,8 @@ en:
invalid_url: "Provided metadata URL is invalid. Provide a HTTP(s) URL."
error: "Failed to retrieve the identity provider metadata: %{error}"
providers:
+ label_empty_title: "No SAML providers configured yet."
+ label_empty_description: "Add a provider to see them here."
label_automatic_configuration: Automatic configuration
label_metadata: Metadata
label_add_new: New SAML identity provider
diff --git a/modules/auth_saml/db/migrate/20240801105918_add_saml_provider.rb b/modules/auth_saml/db/migrate/20240801105918_add_saml_provider.rb
new file mode 100644
index 00000000000..2eac33eae9f
--- /dev/null
+++ b/modules/auth_saml/db/migrate/20240801105918_add_saml_provider.rb
@@ -0,0 +1,13 @@
+class AddSamlProvider < ActiveRecord::Migration[7.1]
+ def change
+ create_table :saml_providers do |t|
+ t.string :display_name, null: false, index: { unique: true }
+ t.boolean :available, null: false, default: true
+ t.boolean :limit_self_registration, null: false, default: false
+ t.jsonb :options, default: {}, null: false
+ t.references :creator, null: false, index: true, foreign_key: { to_table: :users }
+
+ t.timestamps
+ end
+ end
+end