Connect Ruby to OpenID
The example for making web applications compatible with OpenID involves a traditional web site built upon Ruby on Rails 2.0. In following this example, you first will create a basic application and then improve it by adding credentials checking and user login. The basic application will be a basic web-based to-do list. It is purposefully simple, so that you can focus on the OpenID integration.
Make sure you have a locally running MySQL instance. Connect to it and create a development database by issuing the following command:
create database openid_development ;
The database will contain both the data used by your application and the additional tables required to interact with OpenID providers. Open a terminal and create an empty Rails application with this command:
# rails -d mysql openid
Next, verify that everything is working correctly by running the script/server command and accessing the application on http://localhost:3000/.
Create the basic scaffolding that will manage your to-do items by asking Rails to generate it. Each to-do item will have an owner (identified by a name and an email), a start and end date, and a description:
# script/generate scaffold Todo \
person:string email:string start:date end:date description:text
# rake db:migrate
Even though it is extremely simple, your sample application now handles to-do lists perfectly. It is now time to integrate it with OpenID, which will allow you to:
- Provide a login screen for the application
- Enrich the to-do creation page by reusing some of the credentials received by the OpenID provider
First, install the ruby-openid library. This library contains all the client code necessary for the interaction between your application and remote OpenID providers. You can use rubygems to install it:
# sudo gem install ruby-openid
If you inspect the installed gems, you will see something like this, depending on the version of the library installed (different versions make no difference for the purpose of this article):
# gem list --local
ruby-openid (2.0.4, 1.1.4)
... other installed gems ...
The next step is to install the openid_authentication plugin, integrates Rails with the newly installed ruby-openid library. Since this plugin requires some database tables to work properly, you'll also have to run rake to perform the required migrations:
# script/plugin install open_id_authentication
# rake open_id_authentication:db:create
# rake db:migrate