Ruby-OpenID:要求OpenID提供商提供电子邮件地址

Jav*_*ier 10 ruby openid ruby-on-rails ruby-openid authlogic

我正在使用authlogic-example-app,当我注册用户时,我无法从OpenID提供商(在我的情况下是谷歌和雅虎)获得电子邮件地址.我得到一个空响应而不是电子邮件地址(请查看下面代码中的注释).

这就是我的用户模型的样子(其他一切看起来像上面提到的authlogic-example-app的"with_openid" - 分支).除了缺少"电子邮件"之外,openid-authentication-process按预期工作:

class User < ActiveRecord::Base
  acts_as_authentic do |c| 
    # not needed because I use OpenID
    c.validate_login_field = false
    # avoid failed validation before OpenID request
    c.validate_email_field = false
    # this one sets 'openid.sreg.required=email'
    c.required_fields = [:email]
  end

  private

  # overwriting the existing method in '/lib/authlogic_openid/acts_as_authentic.rb'
  def map_openid_registration(registration)
    # this is my problem: 'registration' is an empty hash
    self.email ||= registration[:email] if respond_to?(:email) && !registration[:email].blank?
  end

end
Run Code Online (Sandbox Code Playgroud)

不知道怎么解决这个问题?有没有人在使用authlogic之前完成此操作?甚至更好:你有一个有效的例子吗?

更新:我检查了Google帐户身份验证API,并将authlogic(使用ruby-openid-gemopenid-authentication-plugin)提交的请求与Google帐户身份验证API文档中的示例请求进行了比较:


Google通过身份验证和获取电子邮件地址的示例请求:

https://www.google.com/accounts/o8/ud
?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0
&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select
&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select
&openid.return_to=http%3A%2F%2Fwww.example.com%2Fcheckauth
&openid.realm=http%3A%2F%2Fwww.example.com%2F
&openid.assoc_handle=ABSmpf6DNMw
&openid.mode=checkid_setup
&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0
&openid.ext1.mode=fetch_request
&openid.ext1.type.email=http%3A%2F%2Faxschema.org%2Fcontact%2Femail
&openid.ext1.required=email
Run Code Online (Sandbox Code Playgroud)

我的申请提交的请求:

https://www.google.com/accounts/o8/ud
?openid.assoc_handle=AOQobUcdICerEyK6SXJfukaz8ygXiBqF_gKXv68OBtPXmeafBSdZ6576
&openid.ax.mode=fetch_request
&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select
&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select
&openid.mode=checkid_setup
&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0
&openid.ns.ax=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0
&openid.ns.sreg=http%3A%2F%2Fopenid.net%2Fextensions%2Fsreg%2F1.1
&openid.realm=http%3A%2F%2Flocalhost%3A3000%2F
&openid.return_to=http%3A%2F%2Flocalhost%3A3000%2Faccount%3Ffor_model%3D1%26_method%3Dpost%26open_id_complete%3D1
&openid.sreg.required=email
Run Code Online (Sandbox Code Playgroud)

在调试整个设置时,我发现openid-authentication-plugin在从openid提供程序收到的响应中从未收到过电子邮件,这至少解释了为什么registration我的用户模型中的哈希为空...

更新:如果您正在使用authlogic和openid,请不要忘记查看有关此主题最新railscast!

Jav*_*ier 10

没有人可以帮助我,我帮助自己.:-)

我的问题的简短回答是:

c.required_fields = [:email,"http://axschema.org/contact/email"]
Run Code Online (Sandbox Code Playgroud)

使用此行,应用程序使用sreg和ax(Google支持的请求类型)请求电子邮件地址.

您可以在此处找到更详细的答案以及使用Javascript OpenID-Selector的authlogic-openid的工作实现:

http://github.com/vazqujav/authlogic_openid_selector_example/