【问题标题】:Rails3 omniauth google authentication on Returns User IdentityRails3 omniauth google 身份验证返回用户身份
【发布时间】:2011-12-01 04:13:09
【问题描述】:

我的 Omniauth 集成适用于本地开发,但在 google 暂存时失败。

require 'omniauth/openid'
require 'openid/store/memcache'

Rails.application.config.middleware.use OmniAuth::Builder do
  OmniAuth.config.full_host = "http://xx.xx.xxx/"

  # dedicated openid
   provider :open_id, OpenID::Store::Memcache.new(Dalli::Client.new), :name => 'google', :identifier => 'https://www.google.com/accounts/o8/id'

end

我收到一条错误消息:

于 2011 年 12 月 1 日 02:22:20 +0000 开始 GET "/auth/failure?message=invalid_credentials" for 58.71.19.178 由 ErrorsController#routing 作为 HTML 处理 参数:{"message"=>"invalid_credentials", "a"=>"auth/failure"} 渲染 public/404.html (0.1ms) Completed 404 Not Found in 1ms (Views: 0.6ms | ActiveRecord: 0.0ms)

另外,我的 OmniAuth.config.full_host 中的 ip 也不相同,这可能是导致问题的原因吗?

【问题讨论】:

  • 我认为这可能与 https-http monkey patch 有关,其中请求是从 https 发出的,并且 OpenID 的 return_to 设置为 http。但是尝试他们的猴子补丁也没有解决问题。
  • 终于用openid-for-rails-behind-apache的这个猴子补丁解决了问题

标签: ruby omniauth


【解决方案1】:

罪魁祸首是apache在不同的ips上发送和返回

这个猴子补丁解决了这个问题。

module OmniAuth
  module Strategies
    # OmniAuth strategy for connecting via OpenID. This allows for connection
    # to a wide variety of sites, some of which are listed [on the OpenID website](http://openid.net/get-an-openid/).
    class OpenID
      protected
      def callback_url
        uri = URI.parse(request.url)
        uri.path += '/callback'

        # by KirylP: to overcome hosting subdomain forwarding to rails port        
        uri.port = '' if request.env.has_key? 'HTTP_X_FORWARDED_SERVER'

        uri.to_s
      end
    end
  end
end

module Rack
  class OpenID
    SERVER_PORT_TO_AVOID = 12002

    private
    def realm_url(req)
      url = req.scheme + "://"
      url << req.host

      scheme, port = req.scheme, req.port
      if scheme == "https" && port != 443 ||
          scheme == "http" && port != 80
        url << ":#{port}" if port != SERVER_PORT_TO_AVOID # KirylP
      end

      url
    end
  end
end

module OpenID
  class Consumer
    def complete(query, current_url)
      message = Message.from_post_args(query)

      current_url.sub!(":#{Rack::OpenID::SERVER_PORT_TO_AVOID}", '') # KirylP

      mode = message.get_arg(OPENID_NS, 'mode', 'invalid')
      begin
        meth = method('complete_' + mode)
      rescue NameError
        meth = method(:complete_invalid)
      end
      response = meth.call(message, current_url)
      cleanup_last_requested_endpoint
      if [SUCCESS, CANCEL].member?(response.status)
        cleanup_session
      end
      return response
    end    
  end
end

【讨论】:

    【解决方案2】:

    我遇到了类似的问题。似乎您的 google 身份验证失败(可能是由于不同的原因 - 无效的凭据,或用户拒绝访问),因此您收到回调 /auth/failure - 然后您得到 404。

    您是否在您的 routes.rb 中实现了 /auth/failure 的路由?在我当前的项目中:

    routes.rb

    match '/auth/failure', :to => 'sessions#failure'
    

    sessions_controller

    def failure
       redirect_to session[:return_uri] || root_path, alert: "Sorry, we were not able to    authenticate you using your chosen sign on method"
    end
    

    【讨论】:

    • 感谢您指出这一点。现在我没有得到 404 页面,但这并不能真正解决身份验证问题,因为谷歌用户是有效的并且它不会阻止我的应用程序,但我仍然得到无效的凭据。我认为这可能是因为 https - http return 因此不同的 ip 回调
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 2013-02-09
    • 2021-03-18
    • 2014-04-29
    • 2016-09-26
    • 2019-12-25
    相关资源
    最近更新 更多