【问题标题】:Ruby-OpenID: Requiring email-address from OpenID providerRuby-OpenID:需要来自 OpenID 提供商的电子邮件地址
【发布时间】:2010-10-21 17:37:47
【问题描述】:

我正在使用authlogic-example-app,当我注册用户时,我无法从 OpenID 提供商(在我的情况下为:Google 和 Yahoo)获取电子邮件地址。我得到一个空响应而不是电子邮件地址(检查下面代码中的 cmets)。

这就是我的用户模型的样子(其他一切看起来像上面提到的 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

知道如何解决这个问题吗?在使用authlogic 之前,这里有没有人这样做过?或者更好:你有一个可行的例子吗?

更新:我检查了Google Account Authentication API,并将authlogic(使用ruby-openid-gemopenid-authentication-plugin)提交的请求与Google Account Authentication API 文档中的example requests 进行了比较:


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

我的应用程序提交的请求

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

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

更新:如果您正在使用 authlogic 和 openid,请不要忘记查看latest railscast on this subject

【问题讨论】:

    标签: ruby-on-rails ruby openid authlogic ruby-openid


    【解决方案1】:

    针对您控制的 OpenID 服务器进行测试,因为它可以让您调试 OpenID 序列的每个部分。不能保证 Google 的 OpenID 提供者做的事情是正确的。尝试检查 Verisign 的服务器,因为我很确定至少应该使用 openid.sreg.required=email 字段做正确的事情。

    您的代码 sn-p 对我来说很合适。

    【讨论】:

      【解决方案2】:

      因为没有人能帮助我,所以我帮助了自己。 :-)

      我的问题的简短回答是:

      c.required_fields = [:email,"http://axschema.org/contact/email"]
      

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

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

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

      【讨论】:

      • 启用这些字段时出现错误...未定义的方法 `required_fields=' for #<0x2643d84>
      【解决方案3】:

      虽然这为我指明了正确的方向,但我需要的是:

      c.openid_required_fields = [:email,"http://axschema.org/contact/email"]
      

      这会拉入电子邮件并设置它。

      【讨论】:

        【解决方案4】:
        # fetch email by ax
        c.openid_required_fields = [
                                    "http://axschema.org/contact/email",
                                    "http://axschema.org/namePerson/first",
                                    "http://axschema.org/namePerson/last",
                                    "http://axschema.org/contact/country/home",
                                    "http://axschema.org/pref/language"
                                   ]
        

        这会获取指定@http://code.google.com/apis/accounts/docs/OpenID.html#Parameters的多个值

        虽然我仍然无法获取国家/地区名称...名称、电子邮件、语言都可以完美运行!

        【讨论】:

          【解决方案5】:

          问题是我能够从提供者那里获取参数,但无法从响应中提取它们...... 我使用过 OpenID::AX::FetchResponse.from_success_response(open_id_response) 作为保存响应的对象...我使用什么方法来提取电子邮件、昵称、国家等...

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-09
            • 1970-01-01
            • 1970-01-01
            • 2012-02-21
            • 1970-01-01
            相关资源
            最近更新 更多