To support multiple Identity Providers (IdPs) or dynamic application domains, you can implement a custom settings adapter. This allows you to return specific SAML settings based on the idp_entity_id and the current request object.
Implementation Steps:
- Create an adapter class with a class method
self.settings(idp_entity_id, request). - The
settings method must return a hash of configuration settings for the corresponding IdP. - Register the adapter in
config/initializers/devise.rb using config.idp_settings_adapter = YourAdapterClass.
Settings returned by the adapter will override the default settings defined in config/initializers/devise.rb.
Entity ID Detection
By default, the gem uses config.idp_entity_id_reader to find the Issuer in the SAML request. You can implement a custom reader by defining a class with an .entity_id method. Note that if you use encrypted assertions, your custom reader must be able to decrypt the response to identify the entity ID.
class IdPSettingsAdapter
def self.settings(idp_entity_id, request)
case idp_entity_id
when "http://www.example_idp_entity_id.com"
{
assertion_consumer_service_url: "#{request.protocol}#{request.host_with_port}/users/saml/auth",
assertion_consumer_service_binding: "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST",
name_identifier_format: "urn:oasis:names:tc:SAML:2.0:nameid-format:transient",
sp_entity_id: "#{request.protocol}#{request.host_with_port}/saml/metadata",
idp_entity_id: "http://www.example_idp_entity_id.com",
authn_context: "",
idp_slo_service_url: "http://example_idp_slo_service_url.com",
idp_sso_service_url: "http://example_idp_sso_service_url.com",
idp_cert: "example_idp_cert"
}
else
{}
end
end
end