Ruby on rails Ruby open_id_与Google OpenID的身份验证
我正处于在Rails应用程序中实现OpenID的第一步。 似乎是一个相当容易使用的插件,这就是为什么我决定使用它 用我的Google帐户登录似乎工作得很好,但是我没有得到我需要的sreg/AX字段。 我的代码目前如下:Ruby on rails Ruby open_id_与Google OpenID的身份验证,ruby-on-rails,openid,Ruby On Rails,Openid,我正处于在Rails应用程序中实现OpenID的第一步。 似乎是一个相当容易使用的插件,这就是为什么我决定使用它 用我的Google帐户登录似乎工作得很好,但是我没有得到我需要的sreg/AX字段。 我的代码目前如下: class SessionsController < ApplicationController def new; end def create open_id_authentication end protected def ope
class SessionsController < ApplicationController
def new; end
def create
open_id_authentication
end
protected
def open_id_authentication
authenticate_with_open_id(params[:openid_identifier], :required => ["http://axschema.org/contact/email"]) do |result, identity_url, registration|
if result.successful?
p registration.data
@current_user = User.find_by_identity_url(identity_url)
if @current_user
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
session[:user_id] = @current_user.id
redirect_to(root_url)
end
def failed_login(message)
flash[:error] = message
redirect_to(new_session_url)
end
end
class sessioncontrollerhttp://axschema.org/contact/email“])完成|结果、标识| url、注册|
如果结果成功?
p.注册数据
@当前用户=用户。通过标识url(标识url)查找
如果@当前用户
成功登录
其他的
_登录失败“对不起,不存在该标识URL的用户(#{identity_URL})”
结束
其他的
登录结果失败。消息
结束
结束
结束
私有的
def成功登录
会话[:user\u id]=@current\u user.id
重定向到(根目录url)
结束
def登录失败(消息)
闪存[:错误]=消息
重定向到(新会话url)
结束
结束
我已经阅读了关于Google OpenID的各种讨论,所有人都只说您需要AX模式而不是sreg字段email
,但即使我这样做(正如您在上面的代码中看到的),registration.data仍将保持空({}
)
如何有效地要求大多数OpenID提供商通过OpenID身份验证发送电子邮件?通过OpenID身份验证将返回Sreg对象,而不是AX响应。因此,您需要将此响应实例化为Rack::OpenID::Response,如下所示:
ax_response = OpenID::AX::FetchResponse.from_success_response(request.env[Rack::OpenID::RESPONSE])
在您可以获取数据之后
ax_response['http://axschema.org/contact/email']
ax_response['http://axschema.org/namePerson/first']
ax_response['http://axschema.org/namePerson/last']
authenticate_with_open_id返回Sreg对象,而不是AX响应。因此,您需要将此响应实例化为Rack::OpenID::Response,如下所示:
ax_response = OpenID::AX::FetchResponse.from_success_response(request.env[Rack::OpenID::RESPONSE])
在您可以获取数据之后
ax_response['http://axschema.org/contact/email']
ax_response['http://axschema.org/namePerson/first']
ax_response['http://axschema.org/namePerson/last']
这篇文章包含了一个很好的策略,可以将AX用于谷歌,将Sreg用于其他公司,使这一点更加无缝
这篇文章包含了一个很好的策略,可以将AX用于谷歌,将Sreg用于其他公司,使这一点更加无缝
我还为Ruby on Rails 3、OpenID和谷歌拼凑了一个完整的解决方案:我还为Ruby on Rails 3、OpenID和谷歌拼凑了一个完整的解决方案:非常感谢!随附的自述文件提到,您可以像添加Sreg字段一样添加AX,但是它没有提到您必须这样做!:)节省了我的时间我搜索了很长时间才发现,两周前谢谢了一吨!随附的自述文件提到,您可以像添加Sreg字段一样添加AX,但是它没有提到您必须这样做!:)节省了我的时间我搜索了很长时间才发现,两周前