Skip to content

Instantly share code, notes, and snippets.

@shelling
Created December 27, 2011 03:56
Show Gist options
  • Select an option

  • Save shelling/1522659 to your computer and use it in GitHub Desktop.

Select an option

Save shelling/1522659 to your computer and use it in GitHub Desktop.
class LDAPAdapter
def initialize(options={})
@config = options.select do |key, value|
[:uid].include? key
end
@server = options.select do |key, value|
[:host, :port, :base].include? key
end
@conn = ldap(options)
end
def search(user)
if @conn.bind
filter = Net::LDAP::Filter.eq(@config[:uid], user)
@conn.search(:base => @server[:base], :filter => filter).first
else
return nil
end
rescue Net::LDAP::LdapError => e
return false
end
def bindable?(user, pass)
return false if user.empty? or pass.empty?
user = search(user)
ldap(username: user.dn, password: pass).bind ? user : false
rescue
false
end
private
def ldap(options={})
Net::LDAP.new auth(options).merge(@server)
end
def auth(options={})
{ auth:
{ method: :simple }.merge(options.select { |key, value|
[:username, :password, :method].include? key
})
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment