class Rack::Auth::Config

class Config provide Yaml config mapping for Rack::Auth::Module the class map ldap configurations values @note this class is not provide to be used standalone

Public Class Methods

new(options = { file: './ldap.yml' }) click to toggle source

initializer for Config class @param [Hash<Symbol>] options initialisation options @option options [Symbol] :file The YAML filename (default to ./ldap.yml, the config.ru path) @return [Config] object himself

# File lib/rack/auth/ldap.rb, line 22
def initialize(options = { file: './ldap.yml' })
  @values = defaults
  options.merge!(file: './ldap.yml') { |_key, oldval, _newval| oldval }
  target = ENV['RACK_ENV'] || 'test'
  config_values = load_yaml(::File.expand_path(options[:file], Dir.pwd))[target]
  debug = ::File.open('/tmp/test.txt', 'a+')
  debug.puts ENV.fetch('RACK_ENV', nil)
  debug.close
  config_values.keys.each do |key|
    config_values[key.to_sym] = config_values.delete(key)
  end
  @values.merge! config_values
  @values.keys.each do |meth|
    bloc = proc { @values[meth] }
    self.class.send :define_method, meth, &bloc
  end
end

Private Instance Methods

defaults() click to toggle source

private method with default configuration values for LDAP @return [Hash<Symbol>] the default values of LDAP configuration

# File lib/rack/auth/ldap.rb, line 54
def defaults
  {
    hostname: 'localhost',
    basedn: 'dc=domain,dc=tld',
    rootdn: '',
    passdn: '',
    auth: false,
    port: 389,
    scope: :subtree,
    username_ldap_attribute: 'uid',
    ldaps: false,
    starttls: false,
    tls_options: nil,
    debug: false
  }
end
load_yaml(file) click to toggle source
# File lib/rack/auth/ldap.rb, line 42
def load_yaml(file)
  raise "Could not load ldap configuration. No such file - #{file}" unless ::File.exist?(file)

  ::YAML.load ::ERB.new(IO.read(file)).result, aliases: true
rescue ::Psych::SyntaxError => e
  raise "YAML syntax error occurred while parsing #{file}. " \
        'Please note that YAML must be consistently indented using spaces. Tabs are not allowed. ' \
        "Error: #{e.message}"
end