Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implemented a promiscuous basic authenticator

  • Loading branch information...
commit d7f06e940ae135799546e150fed2c1f72590ee72 1 parent c2c5d6e
@pezra pezra authored
View
1  lib/resourceful.rb
@@ -15,6 +15,7 @@ module Resourceful
autoload :MultipartFormData, 'resourceful/multipart_form_data'
autoload :UrlencodedFormData, 'resourceful/urlencoded_form_data'
autoload :StubbedResourceProxy, 'resourceful/stubbed_resource_proxy'
+ autoload :PromiscuousBasicAuthenticator, 'resourceful/promiscuous_basic_authenticator'
extend Simple
end
View
3  lib/resourceful/authentication_manager.rb
@@ -27,7 +27,7 @@ def add_credentials(request)
end
- class BasicAuthenticator
+ class BasicAuthenticator
def initialize(realm, username, password)
@realm, @username, @password = realm, username, password
@@ -57,7 +57,6 @@ def add_credentials_to(request)
def credentials
HTTPAuth::Basic.pack_authorization(@username, @password)
end
-
end
class DigestAuthenticator
View
18 lib/resourceful/promiscuous_basic_authenticator.rb
@@ -0,0 +1,18 @@
+module Resourceful
+ # This class provides HTTP basic authentication without regard to
+ # the realm of receiving resource. This will send your username and
+ # password with any request made while it is in play.
+ class PromiscuousBasicAuthenticator < BasicAuthenticator
+ def initialize(username, password)
+ super(nil, username, password)
+ end
+
+ def valid_for?(challenge_response)
+ true
+ end
+
+ def can_handle?(request)
+ true
+ end
+ end
+end
View
30 spec/resourceful/promiscuous_basic_authenticator_spec.rb
@@ -0,0 +1,30 @@
+require File.expand_path("../spec_helper", File.dirname(__FILE__))
+
+describe Resourceful::PromiscuousBasicAuthenticator do
+ before do
+ @authenticator = Resourceful::PromiscuousBasicAuthenticator.new('jim', 'mypasswd')
+ end
+
+ it "should always claim to be valid for a challenge response" do
+ challenge_resp = mock("a challenge response")
+ @authenticator.valid_for?(challenge_resp).should eql(true)
+ end
+
+ it "should always claim to handle any request" do
+ a_req = mock("a request")
+ @authenticator.valid_for?(a_req).should eql(true)
+ end
+
+ it "be creatable with just a username and password" do
+ Resourceful::PromiscuousBasicAuthenticator.new('jim', 'mypasswd').should be_instance_of(Resourceful::PromiscuousBasicAuthenticator)
+ end
+
+ it "add credentials to request" do
+ header = mock('header')
+ header.should_receive(:[]=).with("Authorization", "Basic amltOm15cGFzc3dk")
+ a_req = mock("a request", :header => header)
+
+ @authenticator.add_credentials_to(a_req)
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.