# Net::LDAP

**URL:** <https://www.ruby-forum.com/t/net-ldap/67664>\
**Category:** Rails\
**Created:** [September 5, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/net-ldap/67664 "2006-09-05T23:48:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 5, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/net-ldap/67664/1 "2006-09-05T23:48:41Z")

</div>

Im having difficulties setting up my controller to connect to our ldap  
server. I’ve searched around in google and found wikis of snippets of  
some code to set ruby ldap. Below is what I currently have but Im  
getting “uninitialized constant LDAP” when I try to access the login.  
Im not what what Im missing or how to debug this. Any suggestions or  
tips is appreciated.

class LoginController \< ApplicationController

#Bind with the main credential and query the full DN of the email  
address  
#given to us as a parameter, then unbind and rebind as the user.  
def self.authenticate(username,password)  
logger.debug("username = " + username)

```
ldap_con = initialize_ldap_con(username,password)
treebase = "DC=domain,DC=domainext"
mail_filter = Net::LDAP::Filter.eq( "mail", 'removed_email_address@domain.invalid' )
op_filter = Net::LDAP::Filter.eq( "objectClass",

```

“organizationalPerson” )  
dn = String.new  
ldap\_con.search( :base =\> treebase, :filter =\> op\_filter &  
mail\_filter, :attributes=\> ‘dn’) do |entry|  
dn = entry.dn  
end  
login\_succeeded = false  
unless dn.empty?  
ldap\_con = initialize\_ldap\_con(dn,password)  
login\_succeeded = true if ldap\_con.bind  
end  
logger.debug("login\_succeeded = " + login\_succeeded)  
login\_succeeded  
end

private  
def self.initialize\_ldap\_con(user\_name, password)  
Net::LDAP.new( {:host =\> ‘hostnamehere’, :port =\> 389, :auth =\> {  
:method =\> :simple, :username =\> user\_name, :password =\> password }} )  
end

def index  
case @request.method  
when :post  
if @session[‘user’] =  
LoginController.authenticate(@params[‘user\_login’],  
@params[‘user\_password’])

```
      flash['notice'] = "Login successful"
      redirect_back_or_default :action => "overview"
    else
      @login = @params['user_login']
      @message = "Login unsuccessful"
  end
end

```

end  
end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 6, 2006, 5:49am UTC](https://www.ruby-forum.com/t/net-ldap/67664/2 "2006-09-06T05:49:33Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Im having difficulties setting up my controller to connect to our ldap  
> server. I’ve searched around in google and found wikis of snippets of

Jadeler, did you ever figure it out?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 6, 2006, 12:30am UTC](https://www.ruby-forum.com/t/net-ldap/67664/3 "2006-09-06T00:30:03Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Im having difficulties setting up my controller to connect to our ldap  
> server. I’ve searched around in google and found wikis of snippets of  
> some code to set ruby ldap. Below is what I currently have but Im  
> getting “uninitialized constant LDAP” when I try to access the login.  
> Im not what what Im missing or how to debug this. Any suggestions or  
> tips is appreciated.

This may sound like a dumb question but did you require ‘net/ldap’? And  
did you actually install the library?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 6, 2006, 2:35pm UTC](https://www.ruby-forum.com/t/net-ldap/67664/4 "2006-09-06T14:35:28Z")

</div>

Jadeler Amin wrote:

> Hi Francis, yes, it was missing the require ‘net/ldap’, thanks for the  
> tip. I tried it in irb but forgot to put it in the controller. I have  
> to read up alittle bit about net::ldap as Im not getting the  
> appropriate credentials. Im getting results below in irb. Is there an  
> example I can use to test out an example to test out a schema?

Net::LDAP has an extensive Rdoc that should tell you what you need to  
know. In particular I would look at Net::LDAP#bind and  
Net::LDAP#bind\_as.

I assume from your sample code that you’re trying to contact your LDAP  
server in order to authenticate users (and not to search attributes or  
perform authorization). This is a very simple case and the docs should  
help you. I don’t know what you googled for but I’m aware that Simon  
Claret wrote up a wiki page somewhere, with code that looks very much  
like what you’ve posted. He used a standard pattern (bind as admin/query  
user DN/re-bind as user) which subsequently got added to Net::LDAP as  
the Net::LDAP#bind\_as method. If you are able to use #bind\_as, then  
you’ll get away with a lot less code than you’ve already written.

LDAP does tend to confuse people (you don’t mention whether you are new  
to LDAP or not), especially people who are used to relational data. LDAP  
is a pretty different animal.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 6, 2006, 7:24pm UTC](https://www.ruby-forum.com/t/net-ldap/67664/5 "2006-09-06T19:24:38Z")

</div>

Im new to LDAP and Ruby. Trying to tackle both is hard. I originally  
got the login to work in rails using database and the login generator  
but was going towards more of a single sign-on using ldap. I will  
continue to try to tackle this and will look at the rdocs. If you  
happen to know the url to the wiki, that you point me to the right  
direction. Thanks.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 6, 2006, 10:27am UTC](https://www.ruby-forum.com/t/net-ldap/67664/6 "2006-09-06T10:27:31Z")

</div>

Hi Francis, yes, it was missing the require ‘net/ldap’, thanks for the  
tip. I tried it in irb but forgot to put it in the controller. I have  
to read up alittle bit about net::ldap as Im not getting the  
appropriate credentials. Im getting results below in irb. Is there an  
example I can use to test out an example to test out a schema?

irb(main):023:0\> require ‘net/ldap’  
=\> true  
irb(main):024:0\> ldap = Net::LDAP.new :host =\> ‘solar’,  
irb(main):025:0\* :port =\> 389,  
irb(main):026:0\* :auth =\> {  
irb(main):027:1\* :method =\> :simple,  
irb(main):028:1\* :username =\>  
“cn=manager,dc=example,dc=com”,  
irb(main):029:1\* :password =\> “opensesame”  
irb(main):030:1\> }  
=\> #\<Net::LDAP:0x2c35148 @base=“dc=com”, @open\_connection=nil,  
@auth={:username=

> “cn=manager,dc=example,dc=com”, :password=\>“opensesame”, :method=\>:simple}, @en  
> cryption=nil, @verbose=false, @port=389, @host=“solar”\>  
> irb(main):031:0\>  
> irb(main):032:0\* filter = Net::LDAP::Filter.eq( “cn”, “George\*” )  
> =\> #\<Net::LDAP::Filter:0x2c265bc @left=“cn”, @op=:eq, @right=“George\*”\>  
> irb(main):033:0\> treebase = “dc=example,dc=com”  
> =\> “dc=example,dc=com”  
> irb(main):034:0\>  
> irb(main):035:0\* ldap.search( :base =\> treebase, :filter =\> filter )  
> do |entry|

irb(main):036:1\* puts “DN: #{entry.dn}”  
irb(main):037:1\> entry.each do |attribute, values|  
irb(main):038:2\* puts " #{attribute}:"  
irb(main):039:2\> values.each do |value|  
irb(main):040:3\* puts " —\>#{value}"  
irb(main):041:3\> end  
irb(main):042:2\> end  
irb(main):043:1\> end  
=\> false  
irb(main):044:0\>  
irb(main):045:0\* p ldap.get\_operation\_result  
#\<OpenStruct message=“Invalid Credentials”, code=49\>

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 9, 2008, 8:09pm UTC](https://www.ruby-forum.com/t/net-ldap/67664/7 "2008-01-09T20:09:20Z")

</div>

Hi,

apologies for hijacking this thread but it’s the simplest way for me  
to get to your email address, Google is having problems displaying  
CAPTCHAs at the moment!

I am trying to get ruby-net-ldap to work, using the Rails console.

Anyway, I can create LDAP objects and bind anonymously but as soon as  
I try to bind using an auth something like:

ldap\_user = (ldap.search :filter =\> “uid=acochran”).first  
auth = {:method =\> :simple, :username =\> ldap\_user, :password =\>  
“xxxxxx”}

So LDAP can find me, but I get when using the auth variable above:

ldap = Net::LDAP.new(:host =\> ‘[ldap.company.com](http://ldap.company.com)’,:base =\>  
‘dc=company,dc=com’, :auth =\> auth)  
puts ldap.bind  
NoMethodError: undefined method ‘to\_ber’  
from /usr/lib/ruby/gems/1.8/gems/ruby-net-ldap-0.0.4/lib/net/  
ldap/entry.rb:152:in `method_missing' from /usr/lib/ruby/gems/1.8/gems/ruby-net-ldap-0.0.4/lib/net/ ldap.rb:1100:in `bind’  
from /usr/lib/ruby/gems/1.8/gems/ruby-net-ldap-0.0.4/lib/net/  
ldap.rb:701:in `bind’  
from (irb):16

Have you seen this before? If so, is there a patch?

Thanks,

Allan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 6, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/net-ldap/67664/8 "2006-09-06T20:00:44Z")

</div>

From the error you posted change:

ldap.search( :base =\> treebase, :filter =\> filter )

to this:

ldap.search( :base =\> treebase, :filter =\> filter ,:auth =\> {:method =\>  
:anonymous})

or if you want

ldap.search(:base =\> treebase, :filter =\> filter, :auth =\> {:method =\>  
:simple, :username =\> username, :password =\> password})

–  
Andrew S.
