OpenId Authentication For Rails3
1. Add Gem
GemFile
gem 'ruby-openid' gem 'rack-openid', '>=0.2.1', :require => 'rack/openid'
2. install open_id_authentication as a plugin
cd vendor/plugins git clone git://github.com/rails/open_id_authentication.git
3. Modify vendor/plugins/open_id_authentication/init.rb
remove code
if Rails.version < '3' config.gem 'rack-openid', :lib => 'rack/openid', :version => '>=0.2.1' end
Because your rails is 3.0.0.
4. Add OpenId Authentication
SessionController
def create logout_keeping_session! if using_open_id? open_id_authentication else user = User.authenticate(params[:login], params[:password]) ..... end def open_id_authentication(identity_url = nil) # Pass optional :required and :optional keys to specify what sreg fields you want. # Be sure to yield registration, a third argument in the #authenticate_with_open_id block. authenticate_with_open_id(identity_url, :required => [ :nickname, :email ], :optional => :fullname) do |result, identifier, registration| case result.status when :missing note_failed_signin "Sorry, the OpenID server couldn't be found" when :invalid note_failed_signin "Sorry, but this does not appear to be a valid OpenID" when :canceled note_failed_signin "OpenID verification was canceled" when :failed note_failed_signin "Sorry, the OpenID verification failed" when :successful if @current_user = User.where("open_id LIKE ?", "%#{identifier}%").first self.current_user = @current_user flash[:notice] = "Logged in successfully" else note_failed_signin "Sorry, no user by that identity URL exists" end end redirect_back_or_default('/') end def note_failed_signin(msg = nil) flash[:error] = msg || "Couldn't log you in as '#{params[:login]}'" logger.warn(msg || "Failed login for '#{params[:login]}' from #{request.remote_ip} at #{Time.now.utc}") end
Page: create.rhtml.erb
<%= form_tag session_path do -%><% end -%>
User model should add a open_id column.