OpenidWrapper

Thin OpenID Consumer Wrapper for Ruby on Rails using ruby-openid gem.

SHORT STORY

It helps to keep your session controller nice and clean and provides some handy methods such as begin_openid, complete_openid and openid_params.

LONG STORY

The goal is to have openid consumer wrapper, what follows more closely the spirit of OpenID spec and the same time provides some handy methods.

No more you need squeezed openid through one method as in the original plugin, enjoy same clarity as you will find in OpenID spec, so it means when you start request then you just use method begin_openid (aliased to create_openid) and when you complete your request, then use method complete_openid.

note: this wrapper is made openid only in mind, so that’s why it might not be the quickest fit for you if you also need old way of username-password authentication, however they play nice together if you want so.

Installation

First you need Rails 2.1.0 or newer.

Start with:

gem sources -a http://gems.github.com
sudo gem install priit-openid_wrapper

Add to config/environment.rb:

config.gem 'priit-openid_wrapper', :lib => 'openid_wrapper', :source => 'http://gems.github.com'

Add to config/routes.rb:

map.resources :users

# OpenID spec does not honor RESTful design,
# so we need add complete action as well.
map.resources :sessions, :collection => {:complete => :get}

# some handy shortcuts, you can use as link_to 'Signup', signup_path
map. '/signup', :controller => 'users', :action => 'new'
map. '/login', :controller => 'sessions', :action => 'new'
map.logout '/logout', :controller => 'sessions', :action => 'destroy'

Add to app/controller/sessions_controller.rb

def create
  begin_openid # you can change defaults like :return_url => complete_sessions_url etc
               # take a look lib/openid_wrapper/openid_wrapper.rb def begin_openid
end

def complete
  case complete_openid.status
    when :success
      user = User.find_by_openid_identifier(params[:openid][:openid_identifier])
      if user.nil?
        user = User.create(params[:openid])
      end
      session[:current_user_id] = user.id
      flash[:success] = "Logged in succesfully!"
      return redirect_to root_path
    when :failure
      flash[:error] = 'Something went wrong!'
    when :cancel
      flash[:error] = 'Openid login cancelled!' 
    when :setup_needed
      flash[:notice] = 'Openid setup needed!'
    end
  return redirect_to root_path
end

Add to app/views/users/new.html.haml:

- form_tag sessions_path do
  = text_field_tag :openid_identifier
  = submit_tag 'Login'

Additional Tips and Notes

  • Make OpenID identifier field more nuby friendly and

add OpenID official selector from www.idselector.com/

  • OpenID Wrapper will generate log entry: ** openid_wrapper: initialized properly.

How to contribute?

You are welcome to clone it at your favourite git repository:

http://gitorious.org/projects/openid-wrapper
http://github.com/priit/openid-wrapper/
http://rubyforge.org/projects/openid-wrapper/

Check out TODO file to find out some pending tasks or do whatever you like to improve. Just send merge request, when you like to push your improvements.

General Info about OpenID

OpenID Authentication 2.0 - final specifications openid.net/specs/openid-authentication-2_0.html

History and Credits

* OpenID Authentication plugin started by David Heinemeier Hansson.[1]
* Alex Gorbatchev made complete clean up without changing original API.[2]
* Priit Tamboom made complete API change as well and gemified it.[3]
  1. svn.rubyonrails.org/rails/plugins/open_id_authentication/

  2. code.google.com/p/open-id-authentication/

  3. gitorious.org/projects/openid-wrapper/

Copyright © 2008 Priit Tamboom, released under the MIT license