ruby - Rails3 omniauth google 身份验证返回用户身份

标签 ruby omniauth

我的 Omniauth 集成适用于本地开发,但无法用于谷歌暂存。

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

我收到一条错误消息:

开始 GET "/auth/failure?message=invalid_credentials"for 58.71.19.178 at 2011-12-01 02:22:20 +0000 由 ErrorsController 处理#routing 为 HTML 参数:{"message"=>"invalid_credentials", "a"=>"auth/failure"} 呈现 public/404.html(0.1 毫秒) 在 1 毫秒内完成 404 Not Found( View :0.6 毫秒 | ActiveRecord:0.0 毫秒)

此外,我的 OmniAuth.config.full_host 中的 ip 也不相同,这可能会导致问题吗?

最佳答案

罪魁祸首是 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

关于ruby - Rails3 omniauth google 身份验证返回用户身份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8336455/

相关文章:

ruby-on-rails - 使用 Rspec 打桩 sleep 方法。

ruby-on-rails - 如何使用 Devise Omniauth + (Facebook , Twitter, Google ) 完成多重认证?

ruby - 如何编写偶数除以最大相等奇数的代码

ruby - 为什么我在 Mountain Lion 上更新 Rubygems 时得到 "conflicting chdir during another chdir block"?

ruby-on-rails - Omniauth - 起源为零

ruby-on-rails - oauth-twitter 返回未定义的方法 'gsub' 为 nil :NilClass

ruby-on-rails - 在 SSL 模式下在 nginx 后面运行时,Omniauth 和 open_id 与 Google 损坏

ruby-on-rails - 如何覆盖 gem 依赖?

ruby-on-rails - 带有提示的 Formtastic radio 合集

ruby-on-rails - 防止ffmpeg接管stdout