Skip to content

Commit

Permalink
Configuration mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverguenther committed Oct 15, 2024
1 parent 16e03af commit 2bc4704
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# See COPYRIGHT and LICENSE files for more details.
#++

module Saml
module OpenIDConnect
class ConfigurationMapper
attr_reader :configuration

Expand All @@ -36,60 +36,38 @@ def initialize(configuration)

def call!
options = mapped_options(configuration.deep_stringify_keys)

{
"options" => options,
"slug" => options.delete("name"),
"display_name" => options.delete("display_name") || "SAML"
"display_name" => options.delete("display_name") || "OpenID Connect",
"oidc_provider" => "custom",
"client_id" => options["identifier"],
"client_secret" => options["secret"],
"issuer" => options["issuer"],
"authorization_endpoint" => options["authorization_endpoint"],
"token_endpoint" => options["token_endpoint"],
"userinfo_endpoint" => options["userinfo_endpoint"],
"end_session_endpoint" => options["end_session_endpoint"],
"jwks_uri" => options["jwks_uri"]
}
end

private

def mapped_options(options)
options["idp_sso_service_url"] ||= options.delete("idp_sso_target_url")
options["idp_slo_service_url"] ||= options.delete("idp_slo_target_url")
options["sp_entity_id"] ||= options.delete("issuer")

build_idp_cert(options)
extract_security_options(options)
extract_mapping(options)

options.compact
end

def extract_mapping(options)
return unless options["attribute_statements"]

options["mapping_login"] = extract_mapping_attribute(options, "login")
options["mapping_mail"] = extract_mapping_attribute(options, "email")
options["mapping_firstname"] = extract_mapping_attribute(options, "first_name")
options["mapping_lastname"] = extract_mapping_attribute(options, "last_name")
options["mapping_uid"] = extract_mapping_attribute(options, "uid")
end

def extract_mapping_attribute(options, key)
value = options["attribute_statements"][key]

if value.present?
Array(value).join("\n")
end
end

def build_idp_cert(options)
if options["idp_cert"]
options["idp_cert"] = OneLogin::RubySaml::Utils.format_cert(options["idp_cert"])
elsif options["idp_cert_multi"]
options["idp_cert"] = options["idp_cert_multi"]["signing"]
.map { |cert| OneLogin::RubySaml::Utils.format_cert(cert) }
.join("\n")
end
end

def extract_security_options(options)
return unless options["security"]
return unless options["attribute_map"]

options.merge! options["security"].slice("authn_requests_signed", "want_assertions_signed",
"want_assertions_encrypted", "digest_method", "signature_method")
options["mapping_login"] = options["attribute_map"]["login"]
options["mapping_mail"] = options["attribute_map"]["email"]
options["mapping_firstname"] = options["attribute_map"]["first_name"]
options["mapping_lastname"] = options["attribute_map"]["last_name"]
options["mapping_uid"] = options["attribute_map"]["uid"]
end
end
end
21 changes: 4 additions & 17 deletions modules/openid_connect/app/services/openid_connect/sync_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,21 @@ class SyncService

def initialize(name, configuration)
@name = name
@provider_attributes =
{
"slug" => name,
"oidc_provider" => "custom",
"display_name" => configuration["display_name"],
"client_id" => configuration["identifier"],
"client_secret" => configuration["secret"],
"issuer" => configuration["issuer"],
"authorization_endpoint" => configuration["authorization_endpoint"],
"token_endpoint" => configuration["token_endpoint"],
"userinfo_endpoint" => configuration["userinfo_endpoint"],
"end_session_endpoint" => configuration["end_session_endpoint"],
"jwks_uri" => configuration["jwks_uri"]
}
@configuration = ::OpenIDConnect::ConfigurationMapper.new(configuration).call!
end

def call
def call # rubocop:disable Metrics/AbcSize
provider = ::OpenIDConnect::Provider.find_by(slug: name)
if provider
::OpenIDConnect::Providers::UpdateService
.new(model: provider, user: User.system)
.call(@provider_attributes)
.call(@configuration)
.on_success { |call| call.message = "Successfully updated OpenID provider #{name}." }
.on_failure { |call| call.message = "Failed to update OpenID provider: #{call.message}" }
else
::OpenIDConnect::Providers::CreateService
.new(user: User.system)
.call(@provider_attributes)
.call(@configuration)
.on_success { |call| call.message = "Successfully created OpenID provider #{name}." }
.on_failure { |call| call.message = "Failed to create OpenID provider: #{call.message}" }
end
Expand Down

0 comments on commit 2bc4704

Please sign in to comment.