This post originated from an RSS feed registered with Ruby Buzz
by Matt Parrish.
Original Post: Switched to pure ruby ldap library
Feed Title: pearware blog - agile web development
Feed URL: http://blog.pearware.org/feed/atom.xml
Feed Description: Agile web development using Ruby on Rails (and sometimes Java)
I wrote an article awhile back about using the Ruby/LDAP library to handle LDAP authentication in Ruby on Rails. I just finished swapping out the LDAP client library in that application from Ruby/LDAP to ruby-net-ldap. The problems with Ruby/LDAP are that it isn’t a GEM, so installation is a bit more difficult, and it relies on a common LDAP library, like OpenLDAP, to already be installed on the system. The ruby-net-ldap library is written in pure Ruby, so no other library needs to be installed on the system.
Here is the new code that performs the authentication:
require "net/ldap"
class User < ActiveRecord::Base
def self.authenticate(login, password, host, port)
if login.to_s.length > 0 and password.to_s.length > 0
ldap = Net::LDAP.new
ldap.host = host
ldap.port = port
ldap.auth = "cn=#{login},cn=users,o=xyz...", password
if ldap.bin
return find(:first, :conditions => ['username=?', login])
else
return false
end
end
end
end