Class: OmniAuth::LDAP::Adaptor

Inherits:
Object
  • Object
show all
Defined in:
lib/omniauth-ldap/adaptor.rb

Defined Under Namespace

Classes: AuthenticationError, ConfigurationError, ConnectionError, LdapError

Constant Summary collapse

VALID_ADAPTER_CONFIGURATION_KEYS =
[:host, :port, :method, :bind_dn, :password, :try_sasl, :sasl_mechanisms, :uid, :base, :allow_anonymous, :filter]
MUST_HAVE_KEYS =

A list of needed keys. Possible alternatives are specified using sub-lists.

[:host, :port, :method, [:uid, :filter], :base]
METHOD =
{
  :ssl => :simple_tls,
  :tls => :start_tls,
  :plain => nil,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = {}) ⇒ Adaptor

Returns a new instance of Adaptor.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/omniauth-ldap/adaptor.rb', line 40

def initialize(configuration={})
  Adaptor.validate(configuration)
  @configuration = configuration.dup
  @configuration[:allow_anonymous] ||= false
  @logger = @configuration.delete(:logger)
  VALID_ADAPTER_CONFIGURATION_KEYS.each do |name|
    instance_variable_set("@#{name}", @configuration[name])
  end
  method = ensure_method(@method)
  config = {
    :host => @host,
    :port => @port,
    :base => @base
  }
  @bind_method = @try_sasl ? :sasl : (@allow_anonymous||!@bind_dn||!@password ? :anonymous : :simple)


  @auth = sasl_auths({:username => @bind_dn, :password => @password}).first if @bind_method == :sasl
  @auth ||= { :method => @bind_method,
              :username => @bind_dn,
              :password => @password
            }
  config[:auth] = @auth
  @connection = Net::LDAP.new(config)
  @connection.encryption(method)
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



28
29
30
# File 'lib/omniauth-ldap/adaptor.rb', line 28

def auth
  @auth
end

#baseObject (readonly)

Returns the value of attribute base.



28
29
30
# File 'lib/omniauth-ldap/adaptor.rb', line 28

def base
  @base
end

#bind_dnObject

Returns the value of attribute bind_dn.



27
28
29
# File 'lib/omniauth-ldap/adaptor.rb', line 27

def bind_dn
  @bind_dn
end

#connectionObject (readonly)

Returns the value of attribute connection.



28
29
30
# File 'lib/omniauth-ldap/adaptor.rb', line 28

def connection
  @connection
end

#filterObject (readonly)

Returns the value of attribute filter.



28
29
30
# File 'lib/omniauth-ldap/adaptor.rb', line 28

def filter
  @filter
end

#passwordObject

Returns the value of attribute password.



27
28
29
# File 'lib/omniauth-ldap/adaptor.rb', line 27

def password
  @password
end

#uidObject (readonly)

Returns the value of attribute uid.



28
29
30
# File 'lib/omniauth-ldap/adaptor.rb', line 28

def uid
  @uid
end

Class Method Details

.validate(configuration = {}) ⇒ Object

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/omniauth-ldap/adaptor.rb', line 29

def self.validate(configuration={})
  message = []
  MUST_HAVE_KEYS.each do |names|
    names = [names].flatten
    missing_keys = names.select{|name| configuration[name].nil?}
    if missing_keys == names
      message << names.join(' or ')
    end
  end
  raise ArgumentError.new(message.join(",") +" MUST be provided") unless message.empty?
end

Instance Method Details

#bind_as(args = {}) ⇒ Object

:base => “dc=yourcompany, dc=com”, :filter => “(mail=#user)”, :password => psw



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/omniauth-ldap/adaptor.rb', line 70

def bind_as(args = {})
  result = false
  @connection.open do |me|
    rs = me.search args
    if rs and rs.first and dn = rs.first.dn
      password = args[:password]
      method = args[:method] || @method
      password = password.call if password.respond_to?(:call)
      if method == 'sasl'
      result = rs.first if me.bind(sasl_auths({:username => dn, :password => password}).first)
      else
      result = rs.first if me.bind(:method => :simple, :username => dn,
                          :password => password)
      end
    end
  end
  result
end