首页 文章

omniauth(自定义策略)/门卫的法拉第超时错误

提问于
浏览
6

我目前正在跟随这个railscast并且因为我的具体情况在omniauth的回调中遇到了法拉第超时错误 .

目前我正在使用rails应用程序作为API和骨干作为javascript前端(在同一个应用程序中)

我决定使用OAuth锁定API并为Omniauth提供自定义策略以作为客户端访问API以及门卫来处理授权逻辑

module OmniAuth
      module Strategies
        class Twiddle < OmniAuth::Strategies::OAuth2
          option :name, :twiddle

          option :client_options, {
            site: "http://localhost:3001",
            authorize_path: "/oauth/authorize"
          }

          uid do
            raw_info["id"]
          end

          info do
            { 
              firstName: raw_info["firstName"],
              lastName: raw_info["lastName"], 
              email: raw_info["email"]
            }
          end

          def raw_info
            @raw_info ||= access_token.get('/api/v1/user').parsed
          end
        end
      end
    end

我包括这样的自定义策略:

require File.expand_path('lib/omniauth/strategies/twiddle', Rails.root)

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twiddle, id, secret # Omitting the actual ones for obvious reasons
end

我目前正在使用这些宝石

# OAuth 
gem 'oauth2'
gem 'omniauth'
gem 'omniauth-oauth2'
gem 'omniauth-facebook'
gem 'doorkeeper'

这是我验证的地方,并试图检索正确的访问令牌(以及我卡住的地方)

def loginParse
    if ( user = User.authenticate( params[:email], params[:password] ) ) 
      session[:user_id] = user.id
      redirect_to '/auth/twiddle/' 
    else 
      render :controller => "authentication", :action => "loginIndex", :notice => "Incorrect credentials" 
    end
  end

这是来自routes.rb的路由

# Oauth urls
  match '/auth/twiddle/callback', to: "authentication#connectAPI"
  match "/auth/facebook/callback", to: "authentication#loginSocialMedia"

应用程序永远无法呈现connectAPI操作,此时完全陷入困境(由服务器日志指定)

User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  Doorkeeper::Application Load (0.2ms)  SELECT `oauth_applications`.* FROM `oauth_applications` WHERE `oauth_applications`.`uid` = '' LIMIT 1
  CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  Doorkeeper::AccessToken Load (0.3ms)  SELECT `oauth_access_tokens`.* FROM `oauth_access_tokens` WHERE `oauth_access_tokens`.`revoked_at` IS NULL AND `oauth_access_tokens`.`application_id` = 1 AND `oauth_access_tokens`.`resource_owner_id` = 1 ORDER BY created_at desc LIMIT 1
   (0.1ms)  BEGIN
  Doorkeeper::AccessGrant Load (0.2ms)  SELECT `oauth_access_grants`.* FROM `oauth_access_grants` WHERE `oauth_access_grants`.`token` = '' LIMIT 1
  SQL (1.1ms)  INSERT INTO `oauth_access_grants` (`application_id`, `created_at`, `expires_in`, `redirect_uri`, `resource_owner_id`, `revoked_at`, `scopes`, `token`) VALUES (1, '2012-08-08 03:10:31', 600, 'http://localhost:3001/auth/twiddle/callback', 1, NULL, '', '')
   (1.4ms)  COMMIT
Redirected to http://localhost:3001/auth/twiddle/callback?code=a
Completed 302 Found in 12ms (ActiveRecord: 3.7ms)
(twiddle) Callback phase initiated.

日志中省略了许多uid /重要信息 .

最后给出了这个错误:

Faraday::Error::TimeoutError (Timeout::Error):

我希望我对这个问题的解释是彻底的 .

我不知道为什么应用程序似乎在omniauth的回调启动部分冻结 . 我已经尝试更新bundler,因为其他一些stackoverflow问题指出了我,但它不起作用 .

也许我对OAuth2的理解有点模糊 .

如果有人可以帮助我,我将非常感激

1 回答

  • 5

    我不确定这适用于你在这里是我的情景:

    问题

    • 包含我们内部OAuth服务器所需数据的应用

    • OAuth服务器上有很少甚至没有数据

    • 我们希望在不移动数据的情况下将App1的身份验证部分卸载到App2

    解决方案

    • App1使用App2作为身份验证服务器

    • App2将App1用于用户数据

    此解决方案存在问题

    死锁 - App1正在等待来自App2的OAuth响应,但要完成该响应,App2必须等待来自App1的响应 .

    最后的观察

    在Rails的开发模式下(使用WebBrick),您无法运行多线程,因此可能永远不会允许完成请求 .

    我的解决方案

    我的解决方案是安装puma并添加到 config/environments/development.rb

    if ENV["THREADS"]
      config.threadsafe!
    end
    

    然后当你启动服务器时,你会做 THREADS=1 rails s Puma 测试你的OAuth东西 .

    或者您的方案完全不同,实际上您的服务之间并没有进行通信 . 您的 extra_info (如Github的 /user ) endpoints 是否在OAuth服务器上运行?你的回调真的在做什么吗?

    我希望这有帮助!

相关问题