Pat*_*ien 1 openid ruby-on-rails
我是在我的Rails应用程序中实现OpenID的第一步. open_id_authentication似乎是一个相当容易使用的插件,这就是我决定使用它的原因.
使用我的Google帐户登录似乎完美无缺,但是我没有得到我需要的sreg/AX字段.我的代码目前如下:
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
Run Code Online (Sandbox Code Playgroud)
我已经阅读了有关Google OpenID的各种讨论,并且只是说您需要使用AX架构而不是sreg字段email,但即使我这样做(正如您在上面的代码中看到的那样),registration.data仍将保留空({}).
如何通过open_id_authentication有效地要求大多数OpenID提供商提供的电子邮件?
authenticate_with_open_id返回Sreg对象,而不是AX响应.所以你需要使用Rack :: OpenID :: REPONSE来实现这个响应:
ax_response = OpenID::AX::FetchResponse.from_success_response(request.env[Rack::OpenID::RESPONSE])
Run Code Online (Sandbox Code Playgroud)
您可以获取数据后
ax_response['http://axschema.org/contact/email']
ax_response['http://axschema.org/namePerson/first']
ax_response['http://axschema.org/namePerson/last']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2341 次 |
| 最近记录: |