OpenID Sample Application Updated

27 Jul 2007

Jiiim Martin wrote me to ask about the changes that have happened to the openid plugin since I created my OpenID sample Rails application back in May. He pointed out some bug fixes that have occurred to the plugin, so I have updated my sample application to include those bug fixes, and you can download it here (the same location as before).

If you’d like a little more OpenID excitement in your life, check out Dr. Nic’s OpenID sample application that supports multiple OpenID URLs per user.



OpenID Sample Application

11 May 2007

Here’s a follow-up to my instructions on integrating OpenID with ActsAsAuthenticated: a full, sample Ruby on Rails application that provides an example of how to use OpenID. Much like the caboose sample app(s), this is intended to be a good starting point for building your own application with OpenID baked in from the beginning.

This OpenID sample application uses DHH’s open_id_authentication plugin, which has been tweaked to not require the current edge Rails. In other words, you can use this application on Rails 1.2.3 without any modifications. I modified the sample controller code from the plugin to automatically create user accounts with an OpenID login, and to only update the SRE fields for the user when first creating an account.

I’m also using Rick’s restful_authentication plugin with the activation email observer commented out in environment.rb, and with the User model tweaked to allow accounts to be created without a login and password if the account is created via an OpenID login. The controller code does ask the OpenID provider for a nickname (mapped to login) and email address, but they aren’t required and some OpenID providers may not provide the ability to get that extra data, so you can’t really depend on the OpenID provider to provide those. I’ll leave it as an exercise to the reader to figure out a way to collect an email address if responding to an activation email is desired.

Finally, Dr Nic’s Gems on Rails is employed to distribute the ruby-openid gem along with the sample application, so you should be able to just unpack and run.

With the help of so many excellent developers, very little of the code in this archive is actually mine. :) However, whatever there is of my code in there is distributed under the MIT license, as is the rest of the code so generously provided by the other authors (aside from the ruby-openid gem, which is distributed under the Apache license).

Download the example Ruby on Rails OpenID application.



Favorite Rails Plugins

5 Mar 2007

Earlier this morning I wrote about using OpenID with the Acts as Authenticated plugin for the Rails plugin directory. Were you wondering why you’d want to have a login at the plugin directory? :)

Over the weekend I added the ability to add plugins to your own list of favorite plugins. While viewing any plugin at the site, you can click the “Add to favorites” link to add it to your list. Now it’s easy to keep track of the plugins you use on a regular basis — which is especially handy when starting new projects.

Thanks to Michael Trier and Mike Schwab for the suggestion. Stay tuned… I’ll be adding another feature soon that will make your list of favorite plugins extra handy.



Rails, OpenID, and Acts as Authenticated

5 Mar 2007

This weekend I added OpenID to a Rails application for the first time, and this blog post describes the steps I took to integrate OpenID with Acts as Authenticated for account creation and access.

First I installed David’s OpenID Rails plugin (as discussed at David’s blog) into my application which was already using AAA to handle account creations and logins. I then created the following migration to add the OpenID identity URL to my user model:

class AddOpenId < ActiveRecord::Migration
  def self.up
    add_column :users, :identity_url, :string
  end

  def self.down
    remove_column :users, :identity_url
  end
end

And I changed the User model to allow accounts to be created either with login/email/password or with only an identity url (only changed lines are listed):

class User < ActiveRecord::Base
  validates_presence_of :login,
    :email, :if => :not_openid?
  validates_length_of :login,
    :within => 3..40, :if => :not_openid?
  validates_length_of :email,
    :within => 3..100, :if => :not_openid?
  validates_uniqueness_of :login, :email, :salt, :allow_nil => true

  def password_required?
    not_openid? && (crypted_password.blank? or not password.blank?)
  end
 
  def not_openid?
    identity_url.blank?
  end
end

This allows me to create User records without the usual required fields as long as the user created the account via an OpenID login.

And finally, the controller changes:

class AccountController < ApplicationController
  def login
    if using_open_id?
      open_id_authentication
    elsif params[:login]
      password_authentication(params[:login], params[:password])
    end
  end

  protected
 
    def password_authentication(login, password)
      if self.current_user = User.authenticate(params[:login], params[:password])
        successful_login
      else
        failed_login("Invalid login or password")
      end
    end
 
    def open_id_authentication
      authenticate_with_open_id do |result, identity_url|
        if result.successful?
          if self.current_user = User.find_or_create_by_identity_url(identity_url)
            successful_login
          else
            failed_login "Sorry, no user by that identity URL exists (#{identity_url})"
          end
        else
          failed_login result.message
        end
      end
    end

  private
 
    def successful_login
      redirect_back_or_default(index_url)
      flash[:notice] = "Logged in successfully"
    end

    def failed_login(message)
      redirect_to(:action => ‘login’)
      flash[:warning] = message
    end
end

That’s it! You can see it in action at the Rails plugin directory.

Update
I updated this code to match the plugin changes that were made between the time I installed the plugin and the time I posted this entry. :)

Update 2
I made another change to the code based on Geoff’s comment. Thanks, Geoff!