lib/soaspec/o_auth2.rb in soaspec-0.2.15 vs lib/soaspec/o_auth2.rb in soaspec-0.2.16

- old
+ new

@@ -19,24 +19,31 @@ attr_accessor :refresh_token # @attr [Hash] access_tokens List of access tokens. They are mapped according to the OAuth parameters used attr_accessor :access_tokens # List of URLs to that define the instance of an application attr_accessor :instance_urls + # Specify whether to see params sent to and retrieved from oauth. This will put password in log file, only recommended for debugging + attr_writer :debug_oauth + + # @return [Boolean] Whether to see params sent to & received from oauth URL + def debug_oauth? + @debug_oauth || false + end end # @attr [Hash] OAuth parameters attr_accessor :params # @attr [Integer] Count of tries to obtain access token attr_accessor :retry_count # @param [Hash] params_sent Parameters to make OAuth request - # @param_value [token_url] URL to retrieve OAuth token from. @Note this can be set globally instead of here - # @param_value [client_id] Client ID - # @param_value [client_secret] Client Secret - # @param_value [username] Username used in password grant - # @param_value [password] Password used in password grant - # @param_value [security_token] Security Token used in password grant + # @option params_sent [token_url] URL to retrieve OAuth token from. @Note this can be set globally instead of here + # @option params_sent [client_id] Client ID + # @option params_sent [client_secret] Client Secret + # @option params_sent [username] Username used in password grant + # @option params_sent [password] Password used in password grant + # @option params_sent [security_token] Security Token used in password grant # @param [String] api_username Username to use which can be set by Soaspec::ExchangeHandler def initialize(params_sent, api_username = nil) self.retry_count = 0 # No initial tries at getting access token params = params_sent.transform_keys_to_symbols params[:token_url] ||= Soaspec::OAuth2.token_url @@ -48,10 +55,16 @@ params[:security_token] = ERB.new(params[:security_token]).result(binding) if params[:security_token] params[:token_url] = ERB.new(params[:token_url]).result(binding) if params[:token_url] params[:password] = ERB.new(params[:password]).result(binding) if params[:password] end + # Retrieve whether to debug oauth parameters based on global settings + # @return [Boolean] Whether to see params sent to & received from oauth URL + def debug_oauth? + self.class.debug_oauth? + end + # Retrieve instance_url according to access token response. # Some applications have a different instance # It's assumed this will be constant for a set of oauth parameters # @return [String] Instance url def instance_url @@ -69,25 +82,25 @@ end end # @return [Hash] Hash containing access token parameters def response - Soaspec::SpecLogger.info "using oauth_params: #{params}" if Soaspec.debug_oauth? + Soaspec::SpecLogger.info "using oauth_params: #{params}" if debug_oauth? response = RestClient.post(params[:token_url], payload, cache_control: 'no_cache', verify_ssl: false) rescue RestClient::Exception => error Soaspec::SpecLogger.info(["oauth_error: #{error.message}", "oauth_response: #{error.response}"]) self.retry_count += 1 sleep 0.1 # Wait if a bit before retying obtaining access token retry if retry_count < 3 raise error else - Soaspec::SpecLogger.info(["response_headers: #{response.headers}", "response_body: #{response.body}"]) if Soaspec.debug_oauth? + Soaspec::SpecLogger.info(["response_headers: #{response.headers}", "response_body: #{response.body}"]) if debug_oauth? JSON.parse(response) end # @return [String] String to represent OAuth for logging logs def request_message - if Soaspec.debug_oauth? + if debug_oauth? "request_params: #{payload}" else params[:username] ? "User '#{params[:username]}'" : 'client_credentials' end end