【问题标题】:Ruby open_id_authentication with Google OpenIDRuby open_id_authentication 与 Google OpenID
【发布时间】:2010-03-22 12:10:19
【问题描述】:

我正在我的 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

我已经阅读了有关 Google OpenID 的各种讨论,并且都只说您需要 AX 架构而不是 sreg 字段 email,但即使我这样做(如您在上面的代码中所见) ,registration.data 将保持为空 ({})。

如何通过 open_id_authentication 有效地要求来自大多数 OpenID 提供商的电子邮件?

【问题讨论】:

    标签: ruby-on-rails openid


    【解决方案1】:

    authenticate_with_open_id 返回 Sreg 对象,而不是 AX 响应。所以你需要像这样用 Rack::OpenID::REPONSE 实例化这个响应:

    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']
    

    【讨论】:

    • 非常感谢!随附的 README 提到您可以像 Sreg 字段一样添加 AX,但它没有提到您必须这样做! :) 拯救了我的一天
    • 我搜索了很久才发现,两周前
    【解决方案2】:

    我还为 Ruby on Rails 3、OpenID 和 Google 拼凑了一个完整的解决方案:http://blog.sethladd.com/2010/09/ruby-rails-openid-and-google.html

    【讨论】:

      【解决方案3】:

      这篇文章包含了一个很好的策略,可以将 AX 用于 google,将 Sreg 用于其他人,以更无缝地实现这一点 http://www.franzens.org/2009/01/using-google-federated-login-in-your.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-09
        • 1970-01-01
        • 1970-01-01
        • 2011-07-25
        • 2011-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多