OpenId Authentication For Rails3

[align=center][size=xx-large]OpenId Authentication For Rails3[/size][/align]

[b]1. Add Gem[/b]

GemFile

gem 'ruby-openid'
gem 'rack-openid', '>=0.2.1', :require => 'rack/openid'



[b]2. install open_id_authentication as a plugin[/b]

cd vendor/plugins

git clone git://github.com/rails/open_id_authentication.git



[b]3. Modify vendor/plugins/open_id_authentication/init.rb[/b]

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.


[b]4. Add OpenId Authentication[/b]

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 -%>
<fieldset>
<legend>Log In</legend>

<dl>
<dt><%= label_tag 'login' %></dt>
<dd><%= text_field_tag 'login', @login %></dd>
</dl>
<dl>
<dt><%= label_tag 'password' %> </dt>
<dd><%= password_field_tag 'password', nil %></dd>
</dl>
<!-- Uncomment this if you want this functionality-->
<dl>
<dt><%= label_tag 'remember_me', 'Remember me' %></dt>
<dd><%= check_box_tag 'remember_me', '1', @remember_me %></dd>
</dl>
<dl>
Or
</dl>
<dl>
<dt><%= label_tag 'openid_identifier', 'OpenID' %></dt>
<dd><%= text_field_tag 'openid_identifier' %></dd>
</dl>
<dl>
<%= submit_tag 'Log in', :class=>"button" %>
</dl>
<p style="color: red"><%= flash[:error] unless flash[:error].nil? %></p>
</fieldset>
<% end -%>



[b]User model should add a open_id column.[/b]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值