Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Parse creds from .netrc files

  • Loading branch information...
commit a6cd608d3fa69730a93fef9746fa8c4e1b505fda 1 parent c80af47
@pengwynn pengwynn authored
View
1  lib/octokit.rb
@@ -1,3 +1,4 @@
+require 'netrc'
require 'octokit/configuration'
require 'octokit/error'
require 'octokit/client'
View
15 lib/octokit/authentication.rb
@@ -26,5 +26,20 @@ def unauthed_rate_limit_params
:client_secret => client_secret
}
end
+
+ def login_and_password_from_netrc(rc = false)
+ return unless rc
+
+ info = case rc
+ when TrueClass
+ Netrc.read
+ when String
+ Netrc.read rc
+ end
+ netrc_host = URI.parse(api_endpoint).host
+ creds = info[netrc_host]
+ self.login = creds.shift
+ self.password = creds.shift
+ end
end
end
View
2  lib/octokit/client.rb
@@ -38,6 +38,8 @@ def initialize(options={})
Configuration::VALID_OPTIONS_KEYS.each do |key|
send("#{key}=", options[key])
end
+
+ login_and_password_from_netrc(options[:netrc])
end
include Octokit::Authentication
View
2  lib/octokit/configuration.rb
@@ -17,6 +17,7 @@ module Configuration
:client_secret,
:user_agent,
:request_host,
+ :netrc,
:auto_traversal,
:per_page].freeze
@@ -65,6 +66,7 @@ def reset
self.client_id = nil
self.client_secret = nil
self.request_host = nil
+ self.netrc = false
self.user_agent = DEFAULT_USER_AGENT
self.auto_traversal = DEFAULT_AUTO_TRAVERSAL
end
View
1  octokit.gemspec
@@ -7,6 +7,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'faraday_middleware', '~> 0.9'
spec.add_dependency 'hashie', '~> 1.2'
spec.add_dependency 'multi_json', '~> 1.3'
+ spec.add_dependency 'netrc', '~> 0.7.7'
spec.add_development_dependency 'json', '~> 1.7'
spec.add_development_dependency 'kramdown'
spec.add_development_dependency 'rake'
View
2  spec/fixtures/.netrc
@@ -0,0 +1,2 @@
+machine api.github.com login sferik password il0veruby
+machine api.github.dev login defunkt password il0veruby
View
23 spec/octokit/client_spec.rb
@@ -2,6 +2,10 @@
describe Octokit::Client do
+ before do
+ Octokit.reset
+ end
+
it "sets a default user agent" do
stub_request(:get, "https://api.github.com/rate_limit").
with(:headers => {:user_agent => Octokit.user_agent }).
@@ -30,6 +34,25 @@
}.not_to raise_exception
end
+ it "can read .netrc files" do
+ stub_get("https://sferik:il0veruby@api.github.com/user").
+ to_return(json_response("user.json"))
+ Octokit.reset
+ client = Octokit::Client.new(:netrc => File.join(fixture_path, '.netrc'))
+ user = client.user
+ expect(user.login).to eq("sferik")
+ end
+
+ it "can read non-standard API endpoint creds from .netrc" do
+ stub_get("http://defunkt:il0veruby@api.github.dev/user").
+ to_return(json_response("user.json"))
+ Octokit.reset
+ Octokit.api_endpoint = "http://api.github.dev"
+ client = Octokit::Client.new(:netrc => File.join(fixture_path, '.netrc'))
+ user = client.user
+ expect(user.followers).to eq(205)
+ end
+
it "configures faraday from faraday_config_block" do
mw_evaluated = false
Octokit.configure do |c|
Please sign in to comment.
Something went wrong with that request. Please try again.