Omniauth + Google + Faraday + Behind the proxy = how setup proxy?
Asked Answered
F

3

2

My production server is Ubuntu 12.
I'm using Devise + OmniAuth to handle the Google authentication.
But when Google returns a control to my application I get an error "Network is unreachable".
I assume it's because of server is behind the proxy.

Here's error description:

-------------------------------
Request:
-------------------------------

  * URL       : http://efiling.uipv.org/users/auth/google_oauth2/callback?state=f4eaca9fbed2a77c0baac50cdbdccab7eee671179bb8fc43&code=4/YRcyYZzkrfNh8mEBbenxaxyx5EjN.4snirEA-93MXOl05ti8ZT3a9mXdIcgI
  * IP address: 213.160.144.239
  * Parameters: {"state"=>"f4eaca9fbed2a77c0baac50cdbdccab7eee671179bb8fc43", "code"=>"4/YRcyYZzkrfNh8mEBbenxaxyx5EjN.4snirEA-93MXOl05ti8ZT3a9mXdIcgI"}
  * Rails root: /home/test/efiling2
  * Timestamp : 2012-08-14 11:22:27 +0300

-------------------------------
Session:
-------------------------------

  * session id: "5b45534889547514b01f59e39f21f74a"
  * data: {"session_id"=>"5b45534889547514b01f59e39f21f74a",
   "_csrf_token"=>"GJAFgiVO3zd/INIW6DqDh/VYGlvoZ8WuEKAX1HseJwA="}

-------------------------------
Environment:
-------------------------------

  * CONTENT_LENGTH                                 : 0
  * DOCUMENT_ROOT                                  : /home/test/efiling2/public
  * HTTP_ACCEPT                                    : text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  * HTTP_ACCEPT_ENCODING                           : gzip, deflate
  * HTTP_ACCEPT_LANGUAGE                           : ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3
  * HTTP_CACHE_CONTROL                             : max-age=0
  * HTTP_CONNECTION                                : keep-alive
  * HTTP_COOKIE                                    : _eFiling2_session=BAh7CkkiD3Nlc3Npb25faWQGOgZFRiIlNWI0NTUzNDg4OTU0NzUxNGIwMWY1OWUzOWYyMWY3NGFJIhBfY3NyZl90b2tlbgY7AEZJIjFHSkFGZ2lWTzN6ZC9JTklXNkRxRGgvVllHbHZvWjhXdUVLQVgxSHNlSndBPQY7AEZJIhRvbW5pYXV0aC5wYXJhbXMGOwBGewBJIhRvbW5pYXV0aC5vcmlnaW4GOwBGIidodHRwOi8vZWZpbGluZy51aXB2Lm9yZy8%2FbG9jYWxlPXVrSSITb21uaWF1dGguc3RhdGUGOwBGIjVmNGVhY2E5ZmJlZDJhNzdjMGJhYWM1MGNkYmRjY2FiN2VlZTY3MTE3OWJiOGZjNDM%3D--08b5e8bcf347ac5c845922cbf85f31a24907c100
  * HTTP_HOST                                      : efiling.uipv.org
  * HTTP_USER_AGENT                                : Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1
  * HTTP_VIA                                       : 1.1 proxy.ukrpatent.org (squid/3.1.19)
  * HTTP_X_FORWARDED_FOR                           : 10.11.10.56
  * PASSENGER_APP_SPAWNER_IDLE_TIME                : -1
  * PASSENGER_APP_TYPE                             : rack
  * PASSENGER_CONNECT_PASSWORD                     : [FILTERED]
  * PASSENGER_DEBUGGER                             : false
  * PASSENGER_ENVIRONMENT                          : development
  * PASSENGER_FRAMEWORK_SPAWNER_IDLE_TIME          : -1
  * PASSENGER_FRIENDLY_ERROR_PAGES                 : true
  * PASSENGER_GROUP                                :
  * PASSENGER_MAX_REQUESTS                         : 0
  * PASSENGER_MIN_INSTANCES                        : 1
  * PASSENGER_SHOW_VERSION_IN_HEADER               : true
  * PASSENGER_SPAWN_METHOD                         : smart-lv2
  * PASSENGER_USER                                 :
  * PASSENGER_USE_GLOBAL_QUEUE                     : true
  * PATH_INFO                                      : /users/auth/google_oauth2/callback
  * QUERY_STRING                                   : state=f4eaca9fbed2a77c0baac50cdbdccab7eee671179bb8fc43&code=4/YRcyYZzkrfNh8mEBbenxaxyx5EjN.4snirEA-93MXOl05ti8ZT3a9mXdIcgI
  * REMOTE_ADDR                                    : 213.160.144.239
  * REMOTE_PORT                                    : 62175
  * REQUEST_METHOD                                 : GET
  * REQUEST_URI                                    : /users/auth/google_oauth2/callback?state=f4eaca9fbed2a77c0baac50cdbdccab7eee671179bb8fc43&code=4/YRcyYZzkrfNh8mEBbenxaxyx5EjN.4snirEA-93MXOl05ti8ZT3a9mXdIcgI
  * SCGI                                           : 1
  * SCRIPT_NAME                                    :
  * SERVER_ADDR                                    : 10.10.18.3
  * SERVER_NAME                                    : efiling.uipv.org
  * SERVER_PORT                                    : 80
  * SERVER_PROTOCOL                                : HTTP/1.1
  * SERVER_SOFTWARE                                : nginx/1.2.2
  * UNION_STATION_SUPPORT                          : false
  * _                                              : _
  * action_dispatch.cookies                        : {"_eFiling2_session"=>"BAh7CkkiD3Nlc3Npb25faWQGOgZFRiIlNWI0NTUzNDg4OTU0NzUxNGIwMWY1OWUzOWYyMWY3NGFJIhBfY3NyZl90b2tlbgY7AEZJIjFHSkFGZ2lWTzN6ZC9JTklXNkRxRGgvVllHbHZvWjhXdUVLQVgxSHNlSndBPQY7AEZJIhRvbW5pYXV0aC5wYXJhbXMGOwBGewBJIhRvbW5pYXV0aC5vcmlnaW4GOwBGIidodHRwOi8vZWZpbGluZy51aXB2Lm9yZy8/bG9jYWxlPXVrSSITb21uaWF1dGguc3RhdGUGOwBGIjVmNGVhY2E5ZmJlZDJhNzdjMGJhYWM1MGNkYmRjY2FiN2VlZTY3MTE3OWJiOGZjNDM=--08b5e8bcf347ac5c845922cbf85f31a24907c100"}
  * action_dispatch.parameter_filter               : [:password, :efile, /RAW_POST_DATA/]
  * action_dispatch.remote_ip                      : 213.160.144.239
  * action_dispatch.request.content_type           :
  * action_dispatch.request.parameters             : {"state"=>"f4eaca9fbed2a77c0baac50cdbdccab7eee671179bb8fc43", "code"=>"4/YRcyYZzkrfNh8mEBbenxaxyx5EjN.4snirEA-93MXOl05ti8ZT3a9mXdIcgI"}
  * action_dispatch.request.path_parameters        : {}
  * action_dispatch.request.query_parameters       : {"state"=>"f4eaca9fbed2a77c0baac50cdbdccab7eee671179bb8fc43", "code"=>"4/YRcyYZzkrfNh8mEBbenxaxyx5EjN.4snirEA-93MXOl05ti8ZT3a9mXdIcgI"}
  * action_dispatch.request.request_parameters     : {}
  * action_dispatch.request.unsigned_session_cookie: {"session_id"=>"5b45534889547514b01f59e39f21f74a", "_csrf_token"=>"GJAFgiVO3zd/INIW6DqDh/VYGlvoZ8WuEKAX1HseJwA=", "omniauth.params"=>{}, "omniauth.origin"=>"http://efiling.uipv.org/?locale=uk", "omniauth.state"=>"f4eaca9fbed2a77c0baac50cdbdccab7eee671179bb8fc43"}
  * action_dispatch.secret_token                   : f7fa3943c0fc9f3b40a9bc5b0260fa43979de183fc338c08ec96277d6aeab1ce7973c188f93e978d4a513a08d871888520f41671d06a15315ffc6c3d43a1abfa
  * action_dispatch.show_exceptions                : true
  * exception_notifier.options                     : {:sender_address=>"\"Efiling\" <[email protected]>", :exception_recipients=>["[email protected]"], :email_prefix=>"[Efiling Error Report] ", :sections=>["request", "session", "environment", "backtrace"], :background_sections=>["backtrace", "data"], :verbose_subject=>true, :normalize_subject=>false, :ignore_exceptions=>["ActiveRecord::RecordNotFound", "AbstractController::ActionNotFound", "ActionController::RoutingError"], :ignore_crawlers=>[], :ignore_if=>#<Proc:0xa55f0b4@/home/test/.rvm/gems/ruby-1.9.3-p194/gems/exception_notification-2.6.1/lib/exception_notifier.rb:30 (lambda)>}
  * omniauth.origin                                : http://efiling.uipv.org/?locale=uk
  * omniauth.params                                : {}
  * omniauth.strategy                              : #<OmniAuth::Strategies::GoogleOauth2:0xafdbca4>
  * rack.errors                                    : #<IO:0x9a598f4>
  * rack.input                                     : #<PhusionPassenger::Utils::RewindableInput:0xafc2718>
  * rack.multiprocess                              : true
  * rack.multithread                               : false
  * rack.request.cookie_hash                       : {"_eFiling2_session"=>"BAh7CkkiD3Nlc3Npb25faWQGOgZFRiIlNWI0NTUzNDg4OTU0NzUxNGIwMWY1OWUzOWYyMWY3NGFJIhBfY3NyZl90b2tlbgY7AEZJIjFHSkFGZ2lWTzN6ZC9JTklXNkRxRGgvVllHbHZvWjhXdUVLQVgxSHNlSndBPQY7AEZJIhRvbW5pYXV0aC5wYXJhbXMGOwBGewBJIhRvbW5pYXV0aC5vcmlnaW4GOwBGIidodHRwOi8vZWZpbGluZy51aXB2Lm9yZy8/bG9jYWxlPXVrSSITb21uaWF1dGguc3RhdGUGOwBGIjVmNGVhY2E5ZmJlZDJhNzdjMGJhYWM1MGNkYmRjY2FiN2VlZTY3MTE3OWJiOGZjNDM=--08b5e8bcf347ac5c845922cbf85f31a24907c100"}
  * rack.request.cookie_string                     : _eFiling2_session=BAh7CkkiD3Nlc3Npb25faWQGOgZFRiIlNWI0NTUzNDg4OTU0NzUxNGIwMWY1OWUzOWYyMWY3NGFJIhBfY3NyZl90b2tlbgY7AEZJIjFHSkFGZ2lWTzN6ZC9JTklXNkRxRGgvVllHbHZvWjhXdUVLQVgxSHNlSndBPQY7AEZJIhRvbW5pYXV0aC5wYXJhbXMGOwBGewBJIhRvbW5pYXV0aC5vcmlnaW4GOwBGIidodHRwOi8vZWZpbGluZy51aXB2Lm9yZy8%2FbG9jYWxlPXVrSSITb21uaWF1dGguc3RhdGUGOwBGIjVmNGVhY2E5ZmJlZDJhNzdjMGJhYWM1MGNkYmRjY2FiN2VlZTY3MTE3OWJiOGZjNDM%3D--08b5e8bcf347ac5c845922cbf85f31a24907c100
  * rack.request.query_hash                        : {"state"=>"f4eaca9fbed2a77c0baac50cdbdccab7eee671179bb8fc43", "code"=>"4/YRcyYZzkrfNh8mEBbenxaxyx5EjN.4snirEA-93MXOl05ti8ZT3a9mXdIcgI"}
  * rack.request.query_string                      : state=f4eaca9fbed2a77c0baac50cdbdccab7eee671179bb8fc43&code=4/YRcyYZzkrfNh8mEBbenxaxyx5EjN.4snirEA-93MXOl05ti8ZT3a9mXdIcgI
  * rack.run_once                                  : false
  * rack.session                                   : {"session_id"=>"5b45534889547514b01f59e39f21f74a", "_csrf_token"=>"GJAFgiVO3zd/INIW6DqDh/VYGlvoZ8WuEKAX1HseJwA="}
  * rack.session.options                           : {:path=>"/", :domain=>nil, :expire_after=>nil, :secure=>false, :httponly=>true, :id=>"5b45534889547514b01f59e39f21f74a"}
  * rack.url_scheme                                : http
  * rack.version                                   : [1, 0]
  * warden                                         : Warden::Proxy:92171780 @config={:default_scope=>:user, :scope_defaults=>{}, :default_strategies=>{:user=>[:rememberable, :database_authenticatable]}, :intercept_401=>false, :failure_app=>#<Devise::Delegator:0xbf7cb04>}

  * Process: 31617

  * Server : STORK

-------------------------------
Backtrace:
-------------------------------

  /home/test/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:762:in `initialize'
  /home/test/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:762:in `open'
  /home/test/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:762:in `block in connect'
  /home/test/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/timeout.rb:54:in `timeout'
  /home/test/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
  /home/test/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:762:in `connect'
  /home/test/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
  /home/test/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:744:in `start'
  /home/test/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1284:in `request'
  rest-client (1.6.7) lib/restclient/net_http_ext.rb:51:in `request'
  faraday (0.8.2) lib/faraday/adapter/net_http.rb:73:in `perform_request'
  faraday (0.8.2) lib/faraday/adapter/net_http.rb:36:in `call'
  faraday (0.8.2) lib/faraday/request/url_encoded.rb:14:in `call'
  faraday (0.8.2) lib/faraday/connection.rb:226:in `run_request'
  oauth2 (0.8.0) lib/oauth2/client.rb:88:in `request'
  oauth2 (0.8.0) lib/oauth2/client.rb:131:in `get_token'
  oauth2 (0.8.0) lib/oauth2/strategy/auth_code.rb:29:in `get_token'
  omniauth-oauth2 (1.1.0) lib/omniauth/strategies/oauth2.rb:100:in `build_access_token'
  omniauth-oauth2 (1.1.0) lib/omniauth/strategies/oauth2.rb:75:in `callback_phase'
  omniauth (1.1.0) lib/omniauth/strategy.rb:219:in `callback_call'
  omniauth (1.1.0) lib/omniauth/strategy.rb:175:in `call!'
  omniauth (1.1.0) lib/omniauth/strategy.rb:157:in `call'
  sass (3.1.20) lib/sass/plugin/rack.rb:54:in `call'
  exception_notification (2.6.1) lib/exception_notifier.rb:34:in `call'
  warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.1) lib/warden/manager.rb:34:in `catch'
  warden (1.2.1) lib/warden/manager.rb:34:in `call'
  actionpack (3.0.9) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
  actionpack (3.0.9) lib/action_dispatch/middleware/head.rb:14:in `call'
  rack (1.2.5) lib/rack/methodoverride.rb:24:in `call'
  actionpack (3.0.9) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
  actionpack (3.0.9) lib/action_dispatch/middleware/flash.rb:182:in `call'
  actionpack (3.0.9) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
  actionpack (3.0.9) lib/action_dispatch/middleware/cookies.rb:302:in `call'
  activerecord (3.0.9) lib/active_record/query_cache.rb:32:in `block in call'
  activerecord (3.0.9) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
  activerecord (3.0.9) lib/active_record/query_cache.rb:12:in `cache'
  activerecord (3.0.9) lib/active_record/query_cache.rb:31:in `call'
  activerecord (3.0.9) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in `call'
  actionpack (3.0.9) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
  activesupport (3.0.9) lib/active_support/callbacks.rb:416:in `_run_call_callbacks'
  actionpack (3.0.9) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
  rack (1.2.5) lib/rack/sendfile.rb:106:in `call'
  actionpack (3.0.9) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
  actionpack (3.0.9) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
  railties (3.0.9) lib/rails/rack/logger.rb:13:in `call'
  rack (1.2.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.0.9) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.2.5) lib/rack/lock.rb:13:in `block in call'
  <internal:prelude>:10:in `synchronize'
  rack (1.2.5) lib/rack/lock.rb:13:in `call'
  actionpack (3.0.9) lib/action_dispatch/middleware/static.rb:30:in `call'
  railties (3.0.9) lib/rails/application.rb:168:in `call'
  railties (3.0.9) lib/rails/application.rb:77:in `method_missing'
  passenger (3.0.14) lib/phusion_passenger/rack/request_handler.rb:96:in `process_request'
  passenger (3.0.14) lib/phusion_passenger/abstract_request_handler.rb:516:in `accept_and_process_next_request'
  passenger (3.0.14) lib/phusion_passenger/abstract_request_handler.rb:274:in `main_loop'
  passenger (3.0.14) lib/phusion_passenger/rack/application_spawner.rb:206:in `start_request_handler'
  passenger (3.0.14) lib/phusion_passenger/rack/application_spawner.rb:171:in `block in handle_spawn_application'
  passenger (3.0.14) lib/phusion_passenger/utils.rb:470:in `safe_fork'
  passenger (3.0.14) lib/phusion_passenger/rack/application_spawner.rb:166:in `handle_spawn_application'
  passenger (3.0.14) lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
  passenger (3.0.14) lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
  passenger (3.0.14) lib/phusion_passenger/abstract_server.rb:180:in `start'
  passenger (3.0.14) lib/phusion_passenger/rack/application_spawner.rb:129:in `start'
  passenger (3.0.14) lib/phusion_passenger/spawn_manager.rb:253:in `block (2 levels) in spawn_rack_application'
  passenger (3.0.14) lib/phusion_passenger/abstract_server_collection.rb:132:in `lookup_or_add'
  passenger (3.0.14) lib/phusion_passenger/spawn_manager.rb:246:in `block in spawn_rack_application'
  passenger (3.0.14) lib/phusion_passenger/abstract_server_collection.rb:82:in `block in synchronize'
  <internal:prelude>:10:in `synchronize'
  passenger (3.0.14) lib/phusion_passenger/abstract_server_collection.rb:79:in `synchronize'
  passenger (3.0.14) lib/phusion_passenger/spawn_manager.rb:244:in `spawn_rack_application'
  passenger (3.0.14) lib/phusion_passenger/spawn_manager.rb:137:in `spawn_application'
  passenger (3.0.14) lib/phusion_passenger/spawn_manager.rb:275:in `handle_spawn_application'
  passenger (3.0.14) lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
  passenger (3.0.14) lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
  passenger (3.0.14) helper-scripts/passenger-spawn-server:99:in `<main>'

As you can see faraday can't reach network connection.

How Can I setup faraday or maybe rest_client to use Proxy settings?

Thanks in advance.

Fornax answered 14/8, 2012 at 8:38 Comment(0)
F
2

I have solved problem by using monkey patching Oauth2.

require 'faraday'
module OAuth2
  # The OAuth2::Client class
  class Client 
    # The Faraday connection object
    def connection
        options[:connection_opts].merge!({:proxy => 'http://10.10.16.8:3128'})  
      @connection ||= begin
        conn = Faraday.new(site, options[:connection_opts])
        conn.build do |b|
          options[:connection_build].call(b)
        end if options[:connection_build]
        conn
      end
    end 
  end
end
Fornax answered 14/8, 2012 at 13:49 Comment(1)
Rather than monnkey patching just Oauth, you can also monkey patch Faraday with a snippet like here ecarmi.org/writing/faraday-proxyIndigoid
D
2

Here is a better solution :

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, 'login', 'password' {
    scope: ['userinfo.email'],
    access_type: 'online',
    client_options: {connection_opts: {proxy: 'http://myproxy:3128'}}
  }
end
Dekker answered 12/12, 2014 at 16:29 Comment(0)
S
0

Put in an initializer

 RestClient.proxy = "http://proxy.example.com/"
Sally answered 14/8, 2012 at 12:30 Comment(3)
Do You mean put it to config/application.rb?Fornax
Yes you could put in config/application.rb too, or you can create a file config/initializers/rest_client.rb if the logic is more complex like loading an yaml file which will keep the proxy settings for each environment.Sally
Thanks for help, Vlad. But unfortunately it didn't help me.Fornax

© 2022 - 2024 — McMap. All rights reserved.