Class: OmniAuth::Strategies::LDAP

Inherits:
Object
  • Object
show all
Includes:
OmniAuth::Strategy
Defined in:
lib/omniauth/strategies/ldap.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, name, host, port, base, options = {}) ⇒ LDAP

Returns a new instance of LDAP.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/omniauth/strategies/ldap.rb', line 9

def initialize(app, name, host, port, base, options = {})
  @options = options
  @base = base
  @identifier_key = options[:identifier_key] || "uid"

  @ldap = Net::LDAP.new(:host => host, :port => port)        
  if options[:username] && options[:password]
    @ldap.auth options[:username], options[:password]
  end

  super(app, name)
end

Instance Method Details

#auth_hash(entry) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/omniauth/strategies/ldap.rb', line 44

def auth_hash(entry)
  OmniAuth::Utils.deep_merge(super(), {
    'uid' => (entry.send @identifier_key)[0],
    'strategy' => self.class.to_s,                                    
    'user_info' => {             
                                 'name' => entry_attr(entry, :name),
                                 'displayName' => entry_attr(entry, :displayName),
                                 'uid' =>  entry_attr(entry, :uid),
                                 'email' => entry_attr(entry, :mail) || entry_attr(entry, :email)
    }
  })
end

#callback_phaseObject



58
59
60
# File 'lib/omniauth/strategies/ldap.rb', line 58

def callback_phase
  @app.call(env)
end

#entry_attr(entry, key) ⇒ Object



62
63
64
# File 'lib/omniauth/strategies/ldap.rb', line 62

def entry_attr(entry, key)
  (entry.attribute_names.member?(key) && entry.send(key) && (entry.send key)[0]) || nil
end

#request_phaseObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/omniauth/strategies/ldap.rb', line 23

def request_phase
  return fail!(:missing_information) unless (request[:identifier] && request[:password])
  

  result = @ldap.bind_as(:base => @base,
                         :filter => "(#{@identifier_key}=#{request[:identifier]})",
                         :password => request[:password])
  

  if result
    env['REQUEST_METHOD'] = 'GET'
    env['PATH_INFO'] = request.path + '/callback'
    request['auth'] = auth_hash(result.first)
    @app.call(env)
  else
    fail!(:invalid_credentials)
  end

end