Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix passing a URL with embedded basic auth #1324

Merged
merged 4 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Metrics/AbcSize:
# Offense count: 3
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 226
Max: 230

# Offense count: 12
# Configuration parameters: IgnoredMethods.
Expand Down
7 changes: 6 additions & 1 deletion lib/faraday/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,18 @@ def url_prefix=(url, encoder = nil)
uri.query = nil

with_uri_credentials(uri) do |user, password|
basic_auth user, password
set_basic_auth(user, password)
uri.user = uri.password = nil
end

@proxy = proxy_from_env(url) unless @manual_proxy
end

def set_basic_auth(user, password)
header = Faraday::Utils.basic_header_from(user, password)
headers[Faraday::Request::Authorization::KEY] = header
end

# Sets the path prefix and ensures that it always has a leading
# slash.
#
Expand Down
10 changes: 1 addition & 9 deletions lib/faraday/request/authorization.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'base64'

module Faraday
class Request
# Request middleware for the Authorization HTTP header
Expand Down Expand Up @@ -34,7 +32,7 @@ def on_request(env)
# @return [String] a header value
def header_from(type, *params)
if type.to_s.casecmp('basic').zero? && params.size == 2
basic_header_from(*params)
Utils.basic_header_from(*params)
elsif params.size != 1
raise ArgumentError, "Unexpected params received (got #{params.size} instead of 1)"
else
Expand All @@ -43,12 +41,6 @@ def header_from(type, *params)
"#{type} #{value}"
end
end

def basic_header_from(login, pass)
value = Base64.encode64("#{login}:#{pass}")
value.delete!("\n")
"Basic #{value}"
end
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions lib/faraday/utils.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'base64'
require 'uri'
require 'faraday/utils/headers'
require 'faraday/utils/params_hash'
Expand Down Expand Up @@ -52,6 +53,12 @@ def default_params_encoder
@default_params_encoder ||= NestedParamsEncoder
end

def basic_header_from(login, pass)
value = Base64.encode64("#{login}:#{pass}")
value.delete!("\n")
"Basic #{value}"
end

class << self
attr_writer :default_params_encoder
end
Expand Down
6 changes: 6 additions & 0 deletions spec/faraday/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@
it { expect(subject.params).to eq('a' => 3, 'b' => '2') }
end

context 'with basic_auth in url' do
let(:url) { 'http://Aladdin:open%20sesame@sushi.com/fish' }

it { expect(subject.headers['Authorization']).to eq('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==') }
end

context 'with custom headers' do
let(:options) { { headers: { user_agent: 'Faraday' } } }

Expand Down