Introduction
In addition to managing our public presence on the web, we spend a considerable amount of time developing internal applications and services. Ruby on Rails makes creating generic webapps a snap; however, for any practical application you need more than just simple CRUD.
How about Authentication?
Most businesses out there, in no small part due to the inflexibility of Windows authentication schemes, reluctantly use Active Directory to manage their user database. Ill conceived parallel authentication schemes that do not fully integrate with AD risk a host of problems: multiple points of failure, more complex security testing, user confusion, complex password management, etc… Instead, wouldn’t it be great if your Rails webapp played nice with AD?
Development
To solve these probles, we created ActiveAuthentication. ActiveAuthentication is a Rails plugin that allows users to authenticate against Active Directory
Requirements
- Use same username as password as AD account
- Transparently manage the creation of new users to your webapp
- Respect the suspension/deletion of users from the AD domain
What’s Out There
Search first, code second. There are a lot of existing authentication plugins for Ruby on Rails. We didn’t want to rewrite what was already working, so after a brief review we elected to build on acts_as_authenticated for our Active Directory authentication system.
Development
With the majority of the work done by others, we made as few modifications as possible. We modified an the Authenticator object by adding and initializing the attributes we need to connect to the Domain Controller. Next we created a new authenticate method.
The authenticate method does the following:
- Search the database for a user with the same username as the person trying to login.
- Connect to the domain controller with the given username and password.
- If step 2 failed, reject the login request, otherwise proceed.
- Load the user�s information from active directory.
- If you found a user in step one, proceed, otherwise create a new user.
- Populate your user model with the information from Active Directory.
- Save the new or updated user.
The actual code is as follows:
If you read the code carefully, you probably noticed the active_directory.yml file that is loaded in the initialization method. This file contains all the information necessary to connect to your domain controller. The file looks like:
Simply replace domaincontroller with the name of your domain controller, and yourcompany with your domain name, and you should be good to go. Depending on how your directory is setup, you may have to experiment with the dn, but this setup worked for us.
Installation
The instructions for using the plugin are the same as they were for the acts_as_authenticated plugin.
Update
You also need to install the ruby ldap libraries. On Ubuntu you can do this with the following command:
Then you install the plugin:
Next, generate your user model and update the database:
Now include the system in your application.rb file by adding the following code:
Add the before filter to any controllers you want to secure:
Lastly, make sure you edit the active_directory.yml file in the config directory to match your environment.
That’s it, now you can forget about administering users in your internal applications.

