Как настроить конфигурацию во время выполнения приложения метода в ruby на рельсах - PullRequest
0 голосов
/ 14 апреля 2020

Эта ниже конфигурация должна быть загружена во время выполнения приложения. Если я могу преобразовать этот файл в файл YAML, его легко загрузить, но в этой конфигурации мало методов и лямбда-функций, что затрудняет его преобразование.

Конфигурация

  SamlIdp.configure do |config|   
  config.attributes = {
    'User Id' => {
      'name' => 'id',
      'name_format' => 'urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified',
      'getter' => lambda { |principal|
        principal[:user_id]
      }
    }
    }
  }

  service_providers = {
    "https://app-dev.fourkites.com/dynamicyard/saml/SSO" => {
      metadata_url: "https://app-dev.fourkites.com/dynamicyard/saml/SSO",
      # We now validate AssertionConsumerServiceURL will match the MetadataURL set above.
      # *If* it's not going to match your Metadata URL's Host, then set this so we can validate the host using this list
      response_hosts: ['https://app-dev.fourkites.com']
    }
}


  # `identifier` is the entity_id or issuer of the Service Provider,
  # settings is an IncomingMetadata object which has a to_h method that needs to be persisted
  config.service_provider.metadata_persister = ->(identifier, settings) {
    fname = identifier.to_s.gsub(/\/|:/, '_')
    FileUtils.mkdir_p(Rails.root.join('cache', 'saml', 'metadata').to_s)
    File.open Rails.root.join("cache/saml/metadata/#{fname}"), 'r+b' do |f|
      Marshal.dump settings.to_h, f
    end
  }

  # `identifier` is the entity_id or issuer of the Service Provider,
  # `service_provider` is a ServiceProvider object. Based on the `identifier` or the
  # `service_provider` you should return the settings.to_h from above
  config.service_provider.persisted_metadata_getter = ->(identifier, _service_provider) {
    fname = identifier.to_s.gsub(/\/|:/, '_')
    FileUtils.mkdir_p(Rails.root.join('cache', 'saml', 'metadata').to_s)
    full_filename = Rails.root.join("cache/saml/metadata/#{fname}")
    if File.file?(full_filename)
      File.open full_filename, 'rb' do |f|
        Marshal.load f
      end
    end
  }

  # Find ServiceProvider metadata_url and fingerprint based on our settings
  config.service_provider.finder = ->(issuer_or_entity_id) do
    service_providers[issuer_or_entity_id]
  end
end
...