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

时间:2011-12-01 04:13:09

标签: 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

我收到此错误消息:

在2011-12-01 02:22:20 +0000开始获取58.71.19.178的GET“/ auth / failure?message = invalid_credentials”   由ErrorsController处理#routing as HTML   参数:{“message”=>“invalid_credentials”,“a”=>“auth / failure”} 公开/ 404.html(0.1ms) 完成404未找到1ms(浏览次数:0.6ms | ActiveRecord:0.0ms)

我的OmniAuth.config.full_host中的ip in for也许不一样可能导致问题?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我有类似的问题。好像你的谷歌认证失败(可能是由于不同的原因 - 无效的凭据,或用户拒绝访问),因此你收到/ 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