Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- exposed authentication control methods through the quick request in…

…terface;

- added support for automatic detection of authentication method;
- fixed specs to test properly different authentication methods;
- amended README to properly describe all authentication options.
  • Loading branch information...
commit 3e13c7621f42d549748774f0c403818ecc04c6ea 1 parent dc59505
@morhekil morhekil authored
View
31 README.textile
@@ -183,9 +183,8 @@ hydra.stub(:get, "http://localhost:3000/users")
*Basic Authentication*
<pre>
-require 'base64'
response = Typhoeus::Request.get("http://twitter.com/statuses/followers.json",
- :headers => {"Authorization" => "Basic #{Base64.b64encode("#{username}:#{password}")}"})
+ :username => username, :password => password)
</pre>
*SSL*
@@ -227,9 +226,9 @@ e = Typhoeus::Request.get("https://example.com/action",
end
</pre>
-h2. NTLM authentication
+h2. Advanced authentication
-Thanks for the authentication piece and this description go to Oleg Ivanov (morhekil). The major reason to start this fork was the need to perform NTLM authentication in Ruby. Now you can do it via Typhoeus::Easy interface using the following API.
+Thanks for the authentication piece and this description go to Oleg Ivanov (morhekil). The major reason to start this fork was the need to perform NTLM authentication in Ruby, and other libcurl's authentications method were made possible as a result. Now you can do it via Typhoeus::Easy interface using the following API.
<pre>
e = Typhoeus::Easy.new
@@ -251,6 +250,22 @@ The following authentication types are available:
* CURLAUTH_GSSNEGOTIATE
* CURLAUTH_NTLM
* CURLAUTH_DIGEST_IE
+* CURLAUTH_AUTO
+
+The last one (CURLAUTH_AUTO) is really a combination of all previous methods and is provided by Typhoeus for convenience. When you set authentication to auto, Typhoeus will retrieve the given URL first and examine it's headers to confirm what auth types are supported by the server. The it will select the strongest of available auth methods and will send the second request using the selected authentication method.
+
+*Authentication via the quick request interface*
+
+There's also an easy way to perform any kind of authentication via the quick request interface:
+
+<pre>
+e = Typhoeus::Request.get("http://example.com",
+ :username => 'username',
+ :password => 'password',
+ :method => :ntlm)
+</pre>
+
+All methods listed above is available in a shorter form - :basic, :digest, :gssnegotiate, :ntlm, :digest_ie, :auto.
*Query of available auth types*
@@ -266,7 +281,13 @@ e = Typhoeus::Easy.new
e.verbose = 1
</pre>
-Please note that libcurl prints it's output to the console, so you'll need to run your scripts from the console to see the debug info.
+or using the quick request:
+
+<pre>
+e = Typhoeus::Request.get("http://example.com", :verbose => true)
+</pre>
+
+Just remember that libcurl prints it's debug output to the console (to STDERR), so you'll need to run your scripts from the console to see it.
h2. Benchmarks
View
3  lib/typhoeus/easy.rb
@@ -50,7 +50,8 @@ class Easy
:CURLAUTH_DIGEST => 2,
:CURLAUTH_GSSNEGOTIATE => 4,
:CURLAUTH_NTLM => 8,
- :CURLAUTH_DIGEST_IE => 16
+ :CURLAUTH_DIGEST_IE => 16,
+ :CURLAUTH_AUTO => 16 | 8 | 4 | 2 | 1
}
def initialize
View
6 lib/typhoeus/hydra.rb
@@ -132,6 +132,12 @@ def get_easy_object(request)
@running_requests += 1
easy = @easy_pool.pop || Easy.new
+ easy.verbose = request.verbose
+ if request.username || request.password
+ auth = { :username => request.username, :password => request.password }
+ auth[:method] = Typhoeus::Easy::AUTH_TYPES["CURLAUTH_#{request.auth_method.to_s.upcase}".to_sym] if request.auth_method
+ easy.auth = auth
+ end
easy.url = request.url
easy.method = request.method
easy.params = request.params if request.method == :post && !request.params.nil?
View
9 lib/typhoeus/request.rb
@@ -1,6 +1,7 @@
module Typhoeus
class Request
- attr_accessor :method, :params, :body, :headers, :connect_timeout, :timeout, :user_agent, :response, :cache_timeout, :follow_location, :max_redirects, :proxy, :disable_ssl_peer_verification, :ssl_cert, :ssl_cert_type, :ssl_key, :ssl_key_type, :ssl_key_password, :ssl_cacert, :ssl_capath, :verbose
+ attr_accessor :method, :params, :body, :headers, :connect_timeout, :timeout, :user_agent, :response, :cache_timeout, :follow_location, :max_redirects, :proxy, :disable_ssl_peer_verification, :ssl_cert, :ssl_cert_type, :ssl_key, :ssl_key_type, :ssl_key_password, :ssl_cacert, :ssl_capath, :verbose, :username, :password,
+:auth_method
attr_reader :url
@@ -29,6 +30,9 @@ class Request
# ** +:ssl_cacert
# ** +:ssl_capath
# ** +:verbose
+ # ** +:username
+ # ** +:password
+ # ** +:auth_method
#
def initialize(url, options = {})
@method = options[:method] || :get
@@ -51,6 +55,9 @@ def initialize(url, options = {})
@ssl_cacert = options[:ssl_cacert]
@ssl_capath = options[:ssl_capath]
@verbose = options[:verbose]
+ @username = options[:username]
+ @password = options[:password]
+ @auth_method = options[:auth_method]
if @method == :post
@url = url
View
1  spec/servers/app.rb
@@ -44,6 +44,7 @@
# we're just checking for the existence if NTLM auth header here. It's validation
# is too troublesome and really doesn't bother is much, it's up to libcurl to make
# it valid
+ response['WWW-Authenticate'] = 'NTLM'
is_ntlm_auth = /^NTLM/ =~ request.env['HTTP_AUTHORIZATION']
true if is_ntlm_auth
throw(:halt, [401, "Not authorized\n"]) if !is_ntlm_auth
View
26 spec/typhoeus/request_spec.rb
@@ -160,6 +160,32 @@
request.call_handlers
good.should be_true
end
+
+ describe "authentication" do
+
+ it "should allow to set username and password" do
+ auth = { :username => 'foo', :password => 'bar' }
+ e = Typhoeus::Request.get(
+ "http://localhost:3001/auth_basic/#{auth[:username]}/#{auth[:password]}",
+ auth
+ )
+ e.code.should == 200
+ end
+
+ it "should allow to set authentication method" do
+ auth = {
+ :username => 'username',
+ :password => 'password',
+ :auth_method => :ntlm
+ }
+ e = Typhoeus::Request.get(
+ "http://localhost:3001/auth_ntlm",
+ auth
+ )
+ e.code.should == 200
+ end
+
+ end
describe "retry" do
it "should take a retry option"
Please sign in to comment.
Something went wrong with that request. Please try again.