Class: Restforce::Middleware::Authentication
- Inherits:
-
Restforce::Middleware
- Object
- Faraday::Middleware
- Restforce::Middleware
- Restforce::Middleware::Authentication
- Defined in:
- lib/restforce/middleware/authentication.rb
Overview
Faraday middleware that allows for on the fly authentication of requests. When a request fails (a status of 401 is returned), the middleware will attempt to either reauthenticate (username and password) or refresh the oauth access token (if a refresh token is present).
Defined Under Namespace
Instance Method Summary collapse
-
#authenticate! ⇒ Object
Internal: Performs the authentication and returns the response body.
-
#call(env) ⇒ Object
Rescue from 401’s, authenticate then raise the error again so the client can reissue the request.
-
#connection ⇒ Object
Internal: Faraday connection to use when sending an authentication request.
-
#encode_www_form(params) ⇒ Object
Featured detect form encoding.
-
#error_message(response) ⇒ Object
Internal: The parsed error response.
-
#params ⇒ Object
Internal: The params to post to the OAuth service.
Methods inherited from Restforce::Middleware
Constructor Details
This class inherits a constructor from Restforce::Middleware
Instance Method Details
#authenticate! ⇒ Object
Internal: Performs the authentication and returns the response body.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/restforce/middleware/authentication.rb', line 20 def authenticate! response = connection.post '/services/oauth2/token' do |req| req.body = encode_www_form(params) end if response.status != 200 raise Restforce::AuthenticationError, (response) end @options[:instance_url] = response.body['instance_url'] @options[:oauth_token] = response.body['access_token'] if @options[:authentication_callback] @options[:authentication_callback].call(response.body) end response.body end |
#call(env) ⇒ Object
Rescue from 401’s, authenticate then raise the error again so the client can reissue the request.
12 13 14 15 16 17 |
# File 'lib/restforce/middleware/authentication.rb', line 12 def call(env) @app.call(env) rescue Restforce::UnauthorizedError authenticate! raise end |
#connection ⇒ Object
Internal: Faraday connection to use when sending an authentication request.
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/restforce/middleware/authentication.rb', line 45 def connection @connection ||= Faraday.new() do |builder| builder.use Faraday::Request::UrlEncoded builder.use Restforce::Middleware::Mashify, nil, @options builder.response :json builder.use Restforce::Middleware::Logger, Restforce.configuration.logger, @options if Restforce.log? builder.adapter @options[:adapter] end end |
#encode_www_form(params) ⇒ Object
Featured detect form encoding. URI in 1.8 does not include encode_www_form
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/restforce/middleware/authentication.rb', line 66 def encode_www_form(params) if URI.respond_to?(:encode_www_form) URI.encode_www_form(params) else params.map do |k, v| k = CGI.escape(k.to_s) v = CGI.escape(v.to_s) "#{k}=#{v}" end.join('&') end end |
#error_message(response) ⇒ Object
Internal: The parsed error response.
60 61 62 |
# File 'lib/restforce/middleware/authentication.rb', line 60 def (response) "#{response.body['error']}: #{response.body['error_description']}" end |
#params ⇒ Object
Internal: The params to post to the OAuth service.
40 41 42 |
# File 'lib/restforce/middleware/authentication.rb', line 40 def params raise NotImplementedError end |