Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- 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
morhekil committed Jul 16, 2010
1 parent dc59505 commit 3e13c76
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 7 deletions.
31 changes: 26 additions & 5 deletions README.textile
Expand Up @@ -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*
Expand Down Expand Up @@ -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
Expand All @@ -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*

Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/typhoeus/easy.rb
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/typhoeus/hydra.rb
Expand Up @@ -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?
Expand Down
9 changes: 8 additions & 1 deletion 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

Expand Down Expand Up @@ -29,6 +30,9 @@ class Request
# ** +:ssl_cacert
# ** +:ssl_capath
# ** +:verbose
# ** +:username
# ** +:password
# ** +:auth_method
#
def initialize(url, options = {})
@method = options[:method] || :get
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/servers/app.rb
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions spec/typhoeus/request_spec.rb
Expand Up @@ -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"
Expand Down

0 comments on commit 3e13c76

Please sign in to comment.