OpenID Authentication
关键字: Rails OpenID
使用ruby-openid这个gem和open_id_authentication插件做OpenID认证
- sudo gem install ruby-openid
- ruby script\plugin install open_id_authentication
- rake open_id_authentication:db:create
- # routes.rb
- map.open_id_complete 'session', :controller => "session", :action => "create", :requirements => { :method => :get }
- # session_controller.rb
- class SessionController < ApplicationController
- def create
- if using_open_id?
- open_id_authentication(params[:openid_url])
- else
- password_authentication(params[:login], params[:password])
- end
- end
- protected
- def open_id_authentication(openid_url)
- authenticate_with_open_id(openid_url, :required => [:nickname, :email]) do |result, identity_url, registration|
- if result.successful?
- @user = User.find_or_initialize_by_identity_url(identity_url)
- if @user.new_record?
- @user.login = registration['nickname']
- @user.email = registration['email']
- @user.save(false)
- end
- self.current_user = @user
- successful_login
- else
- failed_login result.message
- end
- end
- end
- end
莫愁前路无知己,天下无人不识君。