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.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/omniauth/strategies/ldap.rb', line 11

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

  @auth = nil
  @host = host
  @port = port

  if options[:username] && options[:password]
    @auth = {:method => :simple,
      :username => options[:username],
      :password => options[:password]}
  end

  super(app, name)
end

Instance Method Details

#auth_hashObject



92
93
94
95
96
97
98
99
# File 'lib/omniauth/strategies/ldap.rb', line 92

def auth_hash
  OmniAuth::Utils.deep_merge(super(), {
                               'uid' => @user_info["uid"],
                               'strategy' => self.class.to_s,
                               'user_info' => @user_info,
                               'extra' => @ldap_user_info
  })
end

#bind(username, password) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/omniauth/strategies/ldap.rb', line 33

def bind(username, password)
  filter = "(#{@uid_key}=#{username})"

  ldap = Net::LDAP.new(:host => @host, :port => @port, :auth => @auth)
  result = ldap.bind_as(:base => @base,
                        :filter => filter,
                        :password => password)
  if result
    result.first.inspect
  else
    false
  end
end

#bind_jruby(username, password) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/omniauth/strategies/ldap.rb', line 47

def bind_jruby(username, password)

  begin
    conn = ::LDAP::Conn.new(host=@host, port=@port)
    dn = "#{@uid_key}=#{username},#{@base}"
    
    result = false
    
    conn.bind(dn=dn, password=password, method=::LDAP::LDAP_AUTH_SIMPLE) do
      conn.search(dn, ::LDAP::LDAP_SCOPE_BASE, "(&(objectclass=person))",
                  ["name","email","displayName"]) do |entry|

        result = entry
      end          
    end
    
    result = result.inspect
    
    @user_info = entry_map result
    @user_info[@uid_key] = username        
    @ldap_user_info = result
    return result
    
  rescue
    false
  end
end

#connectObject



29
30
# File 'lib/omniauth/strategies/ldap.rb', line 29

def connect
end

#entry_attr(entry, key) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/omniauth/strategies/ldap.rb', line 111

def entry_attr(entry, key)
  key = key.to_s
  if entry[key]
    entry[key].first
  else
    nil
  end
end

#entry_map(entry) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/omniauth/strategies/ldap.rb', line 102

def entry_map(entry)
  {             
    'name' => entry_attr(entry, :name) || entry_attr(entry, :displayName),
    'displayName' => entry_attr(entry, :displayName),
    'uid' =>  entry_attr(entry, :uid),
    'email' => entry_attr(entry, :mail) || entry_attr(entry, :email)
  }
end

#request_phaseObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/omniauth/strategies/ldap.rb', line 75

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

  result = bind_jruby(request[:username], request[:password])
    
  if result
    @env['REQUEST_METHOD'] = 'GET'
    @env['PATH_INFO'] = "#{OmniAuth.config.path_prefix}/#{name}/callback"
    @env['omniauth.auth'] = auth_hash

    callback_phase
  else 
    fail!(:invalid_credentials)
  end
end