lib/flexirest/request.rb in flexirest-1.10.3 vs lib/flexirest/request.rb in flexirest-1.10.4

- old
+ new

@@ -30,10 +30,14 @@ def object_is_class? !@object.respond_to?(:dirty?) end + def model_class + object_is_class? ? @object : @object.class + end + def class_name if object_is_class? @object.name else @object.class.name @@ -122,10 +126,22 @@ ret = ret.call(@object) if ret.respond_to?(:call) end ret end + def inject_basic_auth_in_url(url) + url.gsub!(%r{//(.)}, "//#{username}:#{password}@\\1") if !url[%r{//[^/]*:[^/]*@}] + end + + def using_basic_auth? + !!username + end + + def basic_auth_digest + Base64.strict_encode64("#{username}:#{password}") + end + def request_body_type if @method[:options][:request_body_type] @method[:options][:request_body_type] elsif @object.nil? nil @@ -525,11 +541,13 @@ @base_url = "#{uri.scheme}://#{uri.host}#{":#{uri.port}" if uri.port != 80 && uri.port != 443}" @url = "#{base_url}#{@url}".gsub(@base_url, "") else _, @base_url, @url = parts end - base_url.gsub!(%r{//(.)}, "//#{username}:#{password}@\\1") if username && !base_url[%r{//[^/]*:[^/]*@}] + if using_basic_auth? && model_class.basic_auth_method == :url + inject_basic_auth_in_url(base_url) + end connection = Flexirest::ConnectionManager.get_connection(base_url) end else parts = @url.match(%r{^(https?://[a-z\d\.:-]+?)(/.*)?$}).to_a if (parts.empty?) # Not a full URL, so use hostname/protocol from existing base_url @@ -538,11 +556,13 @@ @url = "#{base_url}#{@url}".gsub(@base_url, "") base_url = @base_url else base_url = parts[0] end - base_url.gsub!(%r{//(.)}, "//#{username}:#{password}@\\1") if username && !base_url[%r{//[^/]*:[^/]*@}] + if using_basic_auth? && model_class.basic_auth_method == :url + inject_basic_auth_in_url(base_url) + end connection = Flexirest::ConnectionManager.get_connection(base_url) end if @method[:options][:direct] Flexirest::Logger.info " \033[1;4;32m#{Flexirest.name}\033[0m #{@instrumentation_name} - Requesting #{@url}" else @@ -564,9 +584,11 @@ request_options[:api_auth] = { :api_auth_access_id => api_auth_access_id, :api_auth_secret_key => api_auth_secret_key, :api_auth_options => api_auth_options } + elsif using_basic_auth? && model_class.basic_auth_method == :header + http_headers["Authorization"] = "Basic #{basic_auth_digest}" end if @method[:options][:timeout] request_options[:timeout] = @method[:options][:timeout] end