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

Respect auth set in upstream host uri #317

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/gemstash/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ def self.for(upstream)
config.adapter :net_http
config.options.timeout = gemstash_env.config[:fetch_timeout]
end

client.basic_auth(upstream.user, upstream.password) if upstream.auth?

user_agent = "#{upstream.user_agent} " unless upstream.user_agent.to_s.empty?
user_agent = user_agent.to_s + DEFAULT_USER_AGENT

new(client, user_agent: user_agent)
end

attr_reader :client

def initialize(client = nil, user_agent: nil)
@client = client
@user_agent = user_agent || DEFAULT_USER_AGENT
Expand Down
2 changes: 1 addition & 1 deletion lib/gemstash/upstream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def url(path = nil, params = nil)
end

def auth?
!user.to_s.empty? && !password.to_s.empty?
!user.to_s.empty? || !password.to_s.empty?
end

# Utilized as the parent directory for cached gems
Expand Down
20 changes: 20 additions & 0 deletions spec/gemstash/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@
@other_server.stop
end

describe '#for' do
context 'when user:pass auth is in the upstream url' do
let(:upstream) { Gemstash::Upstream.new("https://username:password@localhost/") }
subject { Gemstash::HTTPClient.for(upstream).client.headers }
it { is_expected.to include("Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=") }
end

context 'when api_key auth is in the upstream url' do
let(:upstream) { Gemstash::Upstream.new("https://api_key@localhost/") }
subject { Gemstash::HTTPClient.for(upstream).client.headers }
it { is_expected.to include("Authorization" => "Basic YXBpX2tleTo=") }
end

context 'when no auth is included in the upstream url' do
let(:upstream) { Gemstash::Upstream.new("https://localhost/") }
subject { Gemstash::HTTPClient.for(upstream).client.headers }
it { is_expected.to_not include("Authorization") }
end
end

describe ".get" do
let(:http_client) do
Gemstash::HTTPClient.for(Gemstash::Upstream.new(@server.url))
Expand Down
10 changes: 9 additions & 1 deletion spec/gemstash/upstream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@
expect(upstream_uri.password).to be_nil
end

it "supports url auth in the uri" do
it "supports user:pass url auth in the uri" do
upstream_uri = Gemstash::Upstream.new("https://myuser:mypassword@rubygems.org/")
expect(upstream_uri.user).to eq("myuser")
expect(upstream_uri.password).to eq("mypassword")
expect(upstream_uri.auth?).to be_truthy
end

it "supports api_key url auth in the uri" do
upstream_uri = Gemstash::Upstream.new("https://api_key@rubygems.org/")
expect(upstream_uri.user).to eq("api_key")
expect(upstream_uri.password).to be_nil
expect(upstream_uri.auth?).to be_truthy
end

it "distinguishes between ports, auths, and paths" do
Expand Down