Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LDAP through Ruby or Rails

I've been attempting to hook a Rails application up to ActiveDirectory. I'll be synchronizing data about users between AD and a database, currently MySQL (but may turn into SQL Server or PostgreSQL).

I've checked out activedirectory-ruby, and it looks really buggy (for a 1.0 release!?). It wraps Net::LDAP, so I tried using that instead, but it's really close to the actual syntax of LDAP, and I enjoyed the abstraction of ActiveDirectory-Ruby because of its ActiveRecord-like syntax.

Is there an elegant ORM-type tool for a directory server? Better yet, if there were some kind of scaffolding tool for LDAP (CRUD for users, groups, organizational units, and so on). Then I could quickly integrate that with my existing authentication code though Authlogic, and keep all of the data synchronized.

like image 921
Clinton Avatar asked Dec 02 '08 16:12

Clinton


People also ask

What port does LDAP use?

Possible issues. LDAPS communication occurs over port TCP 636. LDAPS communication to a global catalog server occurs over TCP 3269.

Does LDAP work with Active Directory?

AD does support LDAP, which means it can still be part of your overall access management scheme. Active Directory is just one example of a directory service that supports LDAP. There are other flavors, too: Red Hat Directory Service, OpenLDAP, Apache Directory Server, and more.

How is LDAP used for authentication?

LDAP authentication involves verifying provided usernames and passwords by connecting with a directory service that uses the LDAP protocol. Some directory-servers that use LDAP in this manner are OpenLDAP, MS Active Directory, and OpenDJ.

Is LDAP only used for authentication?

While both are network protocols used for authentication (verification of a user's ID), LDAP differs in that it can also authorize (determine access permissions) clients and store user and group information.


2 Answers

Here is sample code I use with the net-ldap gem to verify user logins from the ActiveDirectory server at my work:

require 'net/ldap' # gem install net-ldap  def name_for_login( email, password )   email = email[/\A\w+/].downcase  # Throw out the domain, if it was there   email << "@mycompany.com"        # I only check people in my company   ldap = Net::LDAP.new(     host: 'ldap.mycompany.com',    # Thankfully this is a standard name     auth: { method: :simple, email: email, password:password }   )   if ldap.bind     # Yay, the login credentials were valid!     # Get the user's full name and return it     ldap.search(       base:         "OU=Users,OU=Accounts,DC=mycompany,DC=com",       filter:       Net::LDAP::Filter.eq( "mail", email ),       attributes:   %w[ displayName ],       return_result:true     ).first.displayName.first   end end 

The first.displayName.first code at the end looks a little goofy, and so might benefit from some explanation:

  • Net::LDAP#search always returns an array of results, even if you end up matching only one entry. The first call to first finds the first (and presumably only) entry that matched the email address.

  • The Net::LDAP::Entry returned by the search conveniently lets you access attributes via method name, so some_entry.displayName is the same as some_entry['displayName'].

  • Every attribute in a Net::LDAP::Entry is always an array of values, even when only one value is present. Although it might be silly to have a user with multiple "displayName" values, LDAP's generic nature means that it's possible. The final first invocation turns the array-of-one-string into just the string for the user's full name.

like image 190
Phrogz Avatar answered Sep 20 '22 09:09

Phrogz


Have you tried looking at these:

http://saush.wordpress.com/2006/07/18/rubyrails-user-authentication-with-microsoft-active-directory/

http://xaop.com/blog/2008/06/17/simple-windows-active-directory-ldap-authentication-with-rails/

like image 43
mrTomahawk Avatar answered Sep 23 '22 09:09

mrTomahawk