At the Forge - Authenticating to a Rails Application
Last month, we began to look at OpenID, the open standard for distributed identification on the Internet. OpenID allows you to have a single user profile, authenticated against a provider you trust, and to use that profile with many different Web sites and Internet applications. OpenID has been growing in popularity during the past few years, after it was first developed and used by blogging company LiveJournal. Since then, it has become a more popular and open standard, and is now supported by many Web sites, as well as all popular programming languages.
I was hoping to use this month's column to show how easy it is to make a Web application compliant with OpenID—or in OpenID terminology, to make it into an OpenID consumer. It turns out that adding OpenID capabilities isn't actually that complicated or difficult, particularly with a popular framework like Ruby on Rails, for which there are many established plugins.
However, I also found that the OpenID plugin for Rails works especially well with a plugin called acts_as_authenticated. This plugin provides a simple, secure and highly customizable authentication system for Rails applications. So this month, we are taking a slight detour, looking at how we can use acts_as_authenticated in Rails applications. Along the way, we can see how to download and use Rails plugins, an important part of Web development with Rails. Next month, we'll build on what we have created, adding OpenID to our application for a truly flexible set of login options for our users.
Although Rails provides a great deal of functionality for developers, it offers few application-level features. Rather, most of its functionality is in the form of objects and methods that programmers can use to create new applications. But, there are no built-in applications, or application fragments, or even a centralized database schema that developers can expect to find in every Rails installation.
The Rails core developers have said that this is done on purpose, because every application has different needs, and it would be impossible to please everyone. And indeed, I understand their point. Each of my applications always has needed to keep a slightly different type of information about users, let alone other types of data. Any choice the developers might make will be wrong for some people.
I happen to think there is a middle ground here. Perhaps the Rails core doesn't need to include a complete solution for users, groups and permissions. But, given the overwhelming number of applications that do define and use such objects, it would make sense to include an easily extensible skeleton within the framework itself.
Such extensions are unlikely to appear in the near future, given the strong feelings the Rails core team has expressed about them in the past. However, all is not lost. Rails includes a “plugin” system that makes it possible to download collections of code—including models, views, controllers and more—and to install them into an application. If you can find and install an appropriate plugin, you get something of a compromise solution. Once installed, the code acts as if it were an integral part of your application. And, of course, you can add only those plugins that are important to your particular application.
Because so many applications require users to register and authenticate, it should come as no surprise that there are a number of available plugins. One of the most popular is acts_as_authenticated, a plugin written by Rails core team member Rick Olson. The name does not refer to an actual declaration, but is rather a playful way of saying that it was designed to work with Rails. And, although the README file (displayed when you install the plugin) indicates that it has been deprecated (in favor of restful_authentication), acts_as_authenticated is popular and stable enough, and plays well enough with OpenID, that it is worth a look.
Rails plugins are installed with the built-in plugin tool, located in script/plugin. You can list the plugins that are available:
But, this will list only those plugins located at one of the sources known to the system. To see a list of these sources, simply type:
To add a new source to the list, simply say:
script/plugin source http://svn.techno-weenie.net/projects/plugins/
Sure enough, after doing this, running script/plugin sources shows the new URL. And, of course, now typing script/plugin list shows many new plugins, from both the old source and the new one.
August 27, 2015
12:00 PM CDT
Free to Linux Journal readers.Register Now!
- Three More Lessons
- Django Models and Migrations
- August 2015 Issue of Linux Journal: Programming
- Hacking a Safe with Bash
- Secure Server Deployments in Hostile Territory, Part II
- The Controversy Behind Canonical's Intellectual Property Policy
- Shashlik - a Tasty New Android Simulator
- Huge Package Overhaul for Debian and Ubuntu
- Embed Linux in Monitoring and Control Systems
- KDE Reveals Plasma Mobile