带有Google OpenID的Ruby open_id_authentication [英] Ruby open_id_authentication with Google OpenID
问题描述
我正在我的Rails应用程序中实现OpenID的第一步. open_id_authentication 似乎是一个非常易于使用的插件,这就是为什么我决定使用它.
I am in my first steps of implementing OpenID in my Rails app. open_id_authentication appeared to be a fairly easy-to-use plugin, which is why I decided to use it.
使用我的Google帐户登录似乎可以正常工作,但是我没有得到所需的sreg/AX字段. 我的代码当前如下:
Logging in with my Google account seems to work perfectly, however I do not get the sreg/AX fields that I require. My code is currently as follows:
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
我已经阅读了有关Google OpenID的各种讨论,所有讨论都只说您需要AX模式而不是sreg字段email
,但是即使这样做,(如您在上面的代码中所看到的) ,registration.data将保持为空({}
).
I have already read various discussions about Google OpenID and all only say that you need to require the AX schema instead of the sreg field email
, but even when I am doing so (as you can see in the code above), registration.data will remain empty ({}
).
如何有效地要求大多数具有Open_id_authentication的OpenID提供者提供的电子邮件?
推荐答案
authenticate_with_open_id返回Sreg对象,而不是AX响应.因此,您需要像这样使用Rack :: OpenID :: REPONSE来实例化此响应:
The authenticate_with_open_id return the Sreg object, not the AX response. So you need instanciate this respone with Rack::OpenID::REPONSE like that :
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']
这篇关于带有Google OpenID的Ruby open_id_authentication的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!