【问题标题】:Omniauth making multiple auth calls per request?Omniauth 每个请求进行多次身份验证调用?
【发布时间】:2012-08-21 13:00:55
【问题描述】:

我为 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 中花费了 548 毫秒,而在 Net::HTTP[stage.wepayapi.com]: POST 中花费了 261 毫秒。我不知道这是否表明了什么。

【问题讨论】:

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


    【解决方案1】:

    很酷,正在检查。最后,作为测试,我只是通过几个 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

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

    【讨论】:

    • 嗯...好吧,我意识到我的 Facebook 测试我给出了误报,因为它没有为每个身份验证调用提供新的访问令牌。你知道有什么好的方法来尝试和调试吗?我已经注释掉了控制器方法中的所有内容,所以我对如何调试它有点困惑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 2014-02-05
    • 2013-04-17
    • 2012-06-13
    • 1970-01-01
    相关资源
    最近更新 更多