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

我正在关注这个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 

我目前正在捆绑中使用这些gem

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

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

  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 /重要信息。

最后给出了这个错误:

法拉第::错误:: TimeoutError(超时::错误):

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

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

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

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

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

问题

  • 包含我们内部OAuth服务器所需数据的应用
  • OAuth服务器上有很少甚至没有数据
  • 我们希望在不移动数据的情况下将App1的身份validation部分卸载到App2

  • App1使用App2作为身份validation服务器
  • App2将App1用于用户数据

此解决方案存在问题

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

最后的观察

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

我的解决方案

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

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

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

要么

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

我希望这有帮助!