Omniauth每次请求多次授权?

时间:2012-08-27 01:05:15

标签: ruby-on-rails ruby-on-rails-3 oauth oauth-2.0 omniauth

我让Omniauth设置了WePay策略(https://github.com/intridea/omniauth)。获得授权后,它会进行四次v2/oauth2/token调用(穿插/v2/user次调用),但会返回env["omniauth.auth"]变量中的第一个访问令牌。这会导致回调时加载时间过长,以及稍后尝试执行API调用时出现“access_token revoked”错误。

我完全不知道为什么会这样。我已经尝试禁用回调后的每个方法,所以我很确定这是在Omniauth本身内发生的,而不是我的应用程序(在Rails中,顺便说一下)。

这是我的omniauth.rb初始值设定项文件:

require "omniauth/strategies/wepay"
 OmniAuth.config.logger = Rails.logger
 Rails.application.config.middleware.use OmniAuth::Builder do
   provider :wepay, ENV['WEPAY_STAGE_APP_ID'], ENV['WEPAY_STAGE_SECRET']
   provider :twitter, ENV['TWITTER_CONSUMER_KEY'], ENV['TWITTER_CONSUMER_SECRET']
   provider :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET']
 end

相关路线:

match 'auth/wepay/callback', to: 'sessions#wepay'
match 'auth/failure', to: redirect('/organization')

会话控制器(虽然我有理由相信循环在调用之前发生):

class SessionsController < ApplicationController
  before_filter :get_all_organizations
  before_filter :authorize_current_organization

  def wepay
    current_user.from_omniauth(env["omniauth.auth"])
    if @organization.wepay_account_id? == false
      @organization.create_wepay_account(current_user)
    end

    redirect_to transactions_path, notice: 'Login successful.' 
  end
end

我的日志的相关部分:

Started GET "/auth/wepay/" for 127.0.0.1 at 2012-08-26 17:40:18 -0700
(wepay) Request phase initiated.


Started GET "/auth/wepay/callback?code=XXXXX&state=XXXXX" for 127.0.0.1 at 2012-08-26 17:40:25 -0700
(wepay) Callback phase initiated.
Connected to NewRelic Service at collector-6.newrelic.com
Processing by SessionsController#wepay as HTML
  Parameters: {"code"=>"XXXXX", "state"=>"XXXXX"}

我很难调试这个,但New Relic确实显示sessions#wepay花费了548ms,而Net::HTTP[stage.wepayapi.com]: POST花费了261ms。我不知道这是否具有指示性。

1 个答案:

答案 0 :(得分:0)

很酷,只是检查一下。在我的最后,作为测试,我只是通过几个Rails Omniauth预制应用程序运行gem,这两个应用程序都可以轻松地进行身份验证并返回到应用程序。我用这些来创建应用程序: http://net.tutsplus.com/tutorials/ruby/how-to-use-omniauth-to-authenticate-your-users/http://railsapps.github.com/tutorial-rails-mongoid-omniauth.html

我会检查会话控制器代码,因为它可能不仅仅是路由问题。