Permalink
Browse files

Removing the access_token_user_finder from Warden::Manager, and addin…

…g it to the Warden::OAuth::Strategy class
  • Loading branch information...
1 parent bff45db commit 84d4b3644bc8c0d65722a5f6daee352c9a1abe56 @roman committed Oct 2, 2009
View
@@ -1,2 +1,6 @@
+* 0.0.2 (Oct 1, 2009) Keep the manager out of the business of strategies
+* Removing the access_token_user_finder from Warden::Manager and adding it to
+the Warden::StrategyBuilder module
+
* 0.0.1 (Sep 16, 2009) The beginning of easy OAuth for Warden
* Adding the initial implementation of the warden_oauth gem
@@ -7,10 +7,6 @@ module OAuth
#
module Manager
- def self.included(base) #:nodoc:
- base.extend(ClassMethods)
- end
-
#
# Helps to setup a new OAuth client authentication, to get started you need to define
# a service name, and then on the block assign the different values required in order
@@ -38,31 +34,6 @@ def oauth(service, &block)
config.provider_name = service
Warden::OAuth::Strategy.build(service, config)
end
-
- module ClassMethods
-
- #
- # Assigns a block that handles how to find a User given an access_token.
- # @param [Symbol] oauth_service The identifier specified on Warden::Manager.oauth
- #
- # @example
- # Warden::Manager.access_token_user_finder(:twitter) do |access_token|
- # # Find user with access_token
- # end
- #
- def access_token_user_finder(oauth_service, &block)
- raise Warden::OAuth::AccessTokenFinderMissing.new("You need to specify a block for Warden::Manager.acess_token_user_finder") unless block_given?
- raise Warden::OAuth::AccessTokenFinderMissing.new("You need to specify a block for Warden::Manager.access_token_user_finder, this must receive one parameter") if block.arity != 1
- @find_user_by_access_token ||= {}
- @find_user_by_access_token[oauth_service] = block
- end
-
- def find_user_by_access_token(oauth_service, access_token) #:nodoc:
- raise Warden::OAuth::AccessTokenFinderMissing.new("You need to specify a block for Warden::Manager.acess_token_user_finder") if @find_user_by_access_token.nil?
- @find_user_by_access_token[oauth_service].call(access_token)
- end
-
- end
end
@@ -47,7 +47,7 @@ def authenticate!
elsif !stored_token_match_recieved_token?
fail!("Received OAuth token didn't match stored OAuth token")
else
- user = Warden::Manager.find_user_by_access_token(config.provider_name , access_token)
+ user = find_user_by_access_token(access_token)
if user.nil?
fail!("User with access token not found")
throw_error_with_oauth_info
@@ -83,6 +83,19 @@ def access_token
protected
+ def find_user_by_access_token(access_token)
+ raise RuntimeError.new(<<-ERROR_MESSAGE) unless self.respond_to?(:_find_user_by_access_token)
+
+You need to define a finder by access_token for this strategy.
+Write on the warden initializer the following code:
+Warden::Strategies[:#{config.provider_name}_oauth].access_token_user_finder do |access_token|
+ # Logic to get your user from an access_token
+end
+
+ERROR_MESSAGE
+ self._find_user_by_access_token(access_token)
+ end
+
def throw_error_with_oauth_info
throw(:warden, :oauth => {
self.config.provider_name => {
@@ -8,6 +8,18 @@ module OAuth
module StrategyBuilder
extend self
+
+ #
+ # Defines the user finder from the access_token for the strategy, receives a block
+ # that will be invoked each time you want to find an user via an access_token in your
+ # system.
+ #
+ # @param blk Block that recieves the access_token as a parameter and will return a user or nil
+ #
+ def access_token_user_finder(&blk)
+ define_method(:_find_user_by_access_token, &blk)
+ end
+
#
# Manages the creation and registration of the OAuth strategy specified
# on the keyword
@@ -33,3 +33,4 @@ def self.call(env)
end
run ClientApp
end if $app.nil?
+
@@ -42,6 +42,7 @@ def fixture_response(name)
@request = Rack::MockRequest.new($app)
end
+
describe "without warden_oauth_service nor oauth_token parameter" do
before(:each) do
@@ -75,43 +76,65 @@ def app
$app
end
- before(:each) do
- Warden::Manager.access_token_user_finder(:example) do |access_token|
- Object.new if access_token.token == 'ABC' && access_token.secret == '123'
- end
- FakeWeb.register_uri(:post, 'http://localhost:3000/oauth/request_token',
- :body => fixture_response("unauthorized_request_token"))
- get "/", 'warden_oauth_provider' => 'example'
- end
-
- describe "and the user is not found" do
+ describe "and the access_token_finder hasn't been declared" do
before(:each) do
- FakeWeb.register_uri(:post, 'http://localhost:3000/oauth/access_token',
- :body => 'oauth_token=ABD&oauth_token_secret=122')
- get "/", 'oauth_token' => "SylltB94pocC6hex8kr9",
- 'oauth_verifier' => "omPxEkKnnx9ygnu7dd6f"
+ FakeWeb.register_uri(:post, 'http://localhost:3000/oauth/request_token',
+ :body => fixture_response("unauthorized_request_token"))
end
-
- it "should invoke the fail app" do
- last_response.body.should == "No user with the given access token"
+
+ it "should raise an exception saying that the access_token_finder is not declared" do
+ get "/", 'warden_oauth_provider' => 'example'
+ FakeWeb.register_uri(:post, 'http://localhost:3000/oauth/access_token',
+ :body => 'oauth_token=ABC&oauth_token_secret=123')
+ lambda do
+ get "/", 'oauth_token' => "SylltB94pocC6hex8kr9",
+ 'oauth_verifier' => "omPxEkKnnx9ygnu7dd6f"
+ end.should raise_error(RuntimeError, /strategy/)
end
end
-
- describe "and the user is found" do
+
+ describe "and the access_token_finder has been declared" do
before(:each) do
- FakeWeb.register_uri(:post, 'http://localhost:3000/oauth/access_token',
- :body => 'oauth_token=ABC&oauth_token_secret=123')
- get "/", 'oauth_token' => "SylltB94pocC6hex8kr9",
- 'oauth_verifier' => "omPxEkKnnx9ygnu7dd6f"
+ Warden::Strategies[:example_oauth].access_token_user_finder do |access_token|
+ Object.new if access_token.token == 'ABC' && access_token.secret == '123'
+ end
+ FakeWeb.register_uri(:post, 'http://localhost:3000/oauth/request_token',
+ :body => fixture_response("unauthorized_request_token"))
+ get "/", 'warden_oauth_provider' => 'example'
end
-
- it "should go to the desired app" do
- last_response.body.should == "Welcome"
+
+ describe "and the user is not found" do
+
+ before(:each) do
+ FakeWeb.register_uri(:post, 'http://localhost:3000/oauth/access_token',
+ :body => 'oauth_token=ABD&oauth_token_secret=122')
+ get "/", 'oauth_token' => "SylltB94pocC6hex8kr9",
+ 'oauth_verifier' => "omPxEkKnnx9ygnu7dd6f"
+ end
+
+ it "should invoke the fail app" do
+ last_response.body.should == "No user with the given access token"
+ end
+
end
+ describe "and the user is found" do
+
+ before(:each) do
+ FakeWeb.register_uri(:post, 'http://localhost:3000/oauth/access_token',
+ :body => 'oauth_token=ABC&oauth_token_secret=123')
+ get "/", 'oauth_token' => "SylltB94pocC6hex8kr9",
+ 'oauth_verifier' => "omPxEkKnnx9ygnu7dd6f"
+ end
+
+ it "should go to the desired app" do
+ last_response.body.should == "Welcome"
+ end
+
+ end
end
end

0 comments on commit 84d4b36

Please sign in to comment.