warden_oauth 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,15 @@
1
+ * 0.1.0 (Jan 9, 2010) Lets stay up to date with josevalim's work
2
+ * Renaming the warden_oauth/manager.rb to warden_oauth/config_extension.rb,
3
+ given that the Warden::Manager is not the given instance on the
4
+ Warden::Manager block anymore
5
+ * Creating a new way to assign access_token_user_finders to the strategies,
6
+ using the sole strategy had a drawback, the class was lazily created after the
7
+ first request to the Rack middleware.
8
+ * Added an example of a twitter client (given that's the more popular client
9
+ being used)
10
+ * Update of the README
11
+
12
+
1
13
  * 0.0.2 (Oct 1, 2009) Keep the manager out of the business of strategies
2
14
  * Removing the access_token_user_finder from Warden::Manager and adding it to
3
15
  the Warden::StrategyBuilder module
@@ -8,42 +8,42 @@ oauth strategies.
8
8
  To get started you just have to require the warden_oauth libraries, and setup the
9
9
  oauth services you would like to have on the <tt>Warden::Manager</tt> middleware declaration:
10
10
 
11
- Warden::Manager do |manager|
12
- manager.failure_app = FailureApp
13
- manager.oauth(:twitter) do |twitter|
11
+ Warden::Manager do |config|
12
+ config.failure_app = FailureApp
13
+ config.oauth(:twitter) do |twitter|
14
14
  twitter.consumer_secret = <YOUR CONSUMER SECRET>
15
15
  twitter.consumer_key = <YOUR CONSUMER KEY>
16
16
  twitter.options :site => 'http://twitter.com'
17
17
  end
18
- manager.default_strategies(:twitter_oauth, :password, :other)
18
+ config.default_strategies(:twitter_oauth, :password, :other)
19
19
  end
20
20
 
21
21
  == Giving an Access Token fetcher
22
22
 
23
23
  Users get identified on a system via an access_token and an access_secret, when a valid access_token is
24
- recevied, warden_oauth calls a fetcher declared on <tt>Warden::Manager.access_token_user_finder</tt>.
24
+ recevied, warden_oauth calls a fetcher declared on <tt>Warden::OAuth.access_token_user_finder(:<strategy_key>)</tt>.
25
25
 
26
- Warden::Manager.access_token_user_finder(:twitter) do |access_token|
26
+ Warden::OAuth.access_token_user_finder(:twitter) do |access_token|
27
27
  User.find_by_access_token_and_access_secret(access_token.token, access_token.secret)
28
28
  end
29
29
 
30
30
  If a user is returned, then this is the user that is going to be authenticated in the session, otherwise the
31
31
  <tt>FailureApp</tt> will be called, you may check the <tt>env['warden.options'][:oauth][:access_token]</tt> to check
32
- the original access_token and <em>create a new user</em> from there if desired.
32
+ the original access_token and <bold>create a new user</bold> from there if desired.
33
33
 
34
34
  == Strategy Class info
35
35
 
36
- When you declare an oauth strategy on the <tt>Warden::Manager</tt> initialization, (e.g. manager.oauth(:service_name))
36
+ When you declare an oauth strategy on the <tt>Warden::Config</tt> initialization, (e.g. config.oauth(:service_name))
37
37
  a <tt>Warden::OAuth::Strategy::ServiceName</tt> will be declared, at the same time this class will be registered as
38
38
  <tt>:service_name_oauth</tt> on the <tt>Warden::Strategies</tt>.
39
39
 
40
- So if we have a declaration like the one of the Getting Started section, we will have an Strategy class
41
- called <tt>Warden::OAuth::Strategy::Twitter</tt>, and this will be registered as <tt>:twitter_oauth</tt>.
40
+ So when we have a declaration like the one we have in the Getting Started section, we will have an Strategy class
41
+ called <tt>Warden::OAuth::Strategy::Twitter</tt>, and this will be registered as <tt>:twitter_oauth</tt> on the Warden::Strategies.
42
42
 
43
43
  == Running the Strategy
44
44
 
45
45
  In order to get the strategy running in the app, you have to specify a parameter called warden_oauth_provider
46
- with the name of the oauth service you want to start. So for example, if you would like to boot the twitter
46
+ with the name of the oauth service you want to use. So for example, if you would like to boot the twitter
47
47
  oauth example given on the "Getting Started" section you just have to specify the parameter on a protected
48
48
  url.
49
49
 
@@ -51,6 +51,21 @@ In Rails:
51
51
 
52
52
  link_to 'Twitter Authentication', url_for(login_path(:warden_oauth_provider => 'twitter'))
53
53
 
54
+ There can be 3 different outcomes from this strategy:
55
+
56
+ 1. The OAuth credentials are invalid and the FailureApp is called.
57
+ 2. The OAuth credentials are valid, but there is no user associated to them. In this case the FailureApp is called, but the env['warden.options'][:oauth][:access_token] will be = Note on Patches/Pull Requests available.
58
+ 3. The OAuth credentials are valid, and the user is authenticated successfuly.
59
+
60
+ Note:
61
+
62
+ In Rails, don't set the <tt>:warden_oauth_provider</tt> parameter as part of the login route, if you do this, rails will catch the parameter, but not the
63
+ warden rack middleware, ergo, it won't work as expected.
64
+
65
+ == Examples
66
+
67
+ If you want to know how to make a twitter authentication client, check examples/twitter/application.rb
68
+
54
69
  == Note on Patches/Pull Requests
55
70
 
56
71
  For any error send an email to: romanandreg [at] gmail [dot] com
data/Rakefile CHANGED
@@ -14,7 +14,7 @@ begin
14
14
  gem.homepage = "http://github.com/roman/warden_oauth"
15
15
  gem.authors = ["Roman Gonzalez"]
16
16
  gem.rubyforge_project = "warden_oauth"
17
- gem.add_dependency('warden')
17
+ gem.add_dependency('warden', ">= 0.8.1")
18
18
  gem.add_dependency('oauth')
19
19
  gem.add_development_dependency("rack-test")
20
20
  gem.add_development_dependency("fakeweb")
@@ -29,8 +29,10 @@ end
29
29
 
30
30
  require 'spec/rake/spectask'
31
31
  Spec::Rake::SpecTask.new(:spec) do |spec|
32
+ root = File.dirname(__FILE__)
32
33
  spec.libs << 'lib' << 'spec'
33
34
  spec.spec_files = FileList['spec/**/*_spec.rb']
35
+ spec.spec_opts = ['--options', "#{root}/spec/spec.opts"]
34
36
  end
35
37
 
36
38
  Spec::Rake::SpecTask.new(:rcov) do |spec|
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.1.0
@@ -0,0 +1,62 @@
1
+ require "rubygems"
2
+ $:.unshift << File.dirname(__FILE__) + "/../../lib"
3
+ require "warden"
4
+ require "warden_oauth"
5
+
6
+
7
+ # You need to specify the following URL in the browser to run the twitter authentication
8
+ # http://localhost:4567/?warden_oauth_provider=twitter
9
+
10
+
11
+ class ClientApp
12
+
13
+ def self.call(env)
14
+ env['warden'].authenticate!
15
+ [200, {"Content-Type" => 'text/plain'}, "Welcome"]
16
+ end
17
+
18
+ end
19
+
20
+ class ErrorApp
21
+
22
+ def self.call(env)
23
+ if env['warden.options'][:oauth].nil?
24
+ [401, {'Content-Type' => 'text/plain'}, "You are not authenticated"]
25
+ else
26
+ access_token = env['warden.options'][:oauth][:access_token]
27
+ [401, {'Content-Type' => 'text/plain'}, "No user with the given access token"]
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ class User
34
+ attr_accessor :token
35
+ attr_accessor :secret
36
+ def initialize(token, secret)
37
+ @token = token
38
+ @secret = secret
39
+ end
40
+ end
41
+
42
+ Warden::OAuth.access_token_user_finder(:twitter) do |access_token|
43
+ # NOTE: Normally here you use AR/DM to fetch up the user given an access_token and an access_secret
44
+ User.new(access_token.token, access_token.secret)
45
+ end
46
+
47
+ app = Rack::Builder.new do
48
+ use Rack::Session::Cookie
49
+ use Warden::Manager do |config|
50
+ config.oauth(:twitter) do |twitter|
51
+ # If you want this example to work, you need to specify both consumer_key and consumer_secret
52
+ twitter.consumer_key ""
53
+ twitter.consumer_secret ""
54
+ twitter.options :site => 'http://twitter.com'
55
+ end
56
+ config.default_strategies :twitter_oauth
57
+ config.failure_app = ErrorApp
58
+ end
59
+ run ClientApp
60
+ end
61
+
62
+ Rack::Handler::Mongrel.run app, :Port => '4567'
@@ -7,12 +7,13 @@ module Warden
7
7
 
8
8
  base_path = File.dirname(__FILE__) + "/warden_oauth"
9
9
 
10
+ require base_path + "/base"
10
11
  require base_path + "/errors"
11
12
  autoload :Utils, base_path + '/utils'
12
13
  autoload :StrategyBuilder, base_path + '/strategy_builder'
13
14
  autoload :Strategy, base_path + '/strategy'
14
15
  autoload :Config, base_path + "/config"
15
- require base_path + "/manager"
16
+ require base_path + "/config_extension"
16
17
 
17
18
 
18
19
  end
@@ -0,0 +1,13 @@
1
+ module Warden
2
+ module OAuth
3
+
4
+ def self.access_token_user_finder(key, &block)
5
+ Strategy.access_token_user_finders[key] = block
6
+ end
7
+
8
+ def self.clear_access_token_user_finders
9
+ Strategy.access_token_user_finders.clear
10
+ end
11
+
12
+ end
13
+ end
@@ -2,10 +2,10 @@ module Warden
2
2
  module OAuth
3
3
 
4
4
  #
5
- # Holds all the extensions made to Warden::Manager in order to create OAuth
5
+ # Holds all the extensions made to Warden::Config in order to create OAuth
6
6
  # consumers.
7
7
  #
8
- module Manager
8
+ module ConfigExtension
9
9
 
10
10
  #
11
11
  # Helps to setup a new OAuth client authentication, to get started you need to define
@@ -14,11 +14,12 @@ module Warden
14
14
  # @param [Symbol] service An identifier of the OAuth service
15
15
  #
16
16
  # @example
17
- #
18
- # Warden::Manager.oauth(:twitter) do
19
- # consumer_key "<YOUR CONSUMER KEY>"
20
- # consumer_secret "<YOUR CONSUMER SECRET>"
21
- # options :site => 'http://twitter.com'
17
+ # use Warden::Manager do |config|
18
+ # config.oauth(:twitter) do
19
+ # consumer_key "<YOUR CONSUMER KEY>"
20
+ # consumer_secret "<YOUR CONSUMER SECRET>"
21
+ # options :site => 'http://twitter.com'
22
+ # end
22
23
  # end
23
24
  #
24
25
  def oauth(service, &block)
@@ -40,4 +41,5 @@ module Warden
40
41
  end
41
42
  end
42
43
 
43
- Warden::Manager.send(:include, Warden::OAuth::Manager)
44
+ Warden::Config.send(:include, Warden::OAuth::ConfigExtension)
45
+
@@ -12,6 +12,11 @@ module Warden
12
12
  ### Strategy Logic ###
13
13
  ######################
14
14
 
15
+
16
+ def self.access_token_user_finders
17
+ (@_user_token_finders ||= {})
18
+ end
19
+
15
20
  #
16
21
  # An OAuth strategy will be valid to execute if:
17
22
  # * A 'warden_oauth_provider' parameter is given, with the name of the OAuth service
@@ -88,7 +93,7 @@ module Warden
88
93
 
89
94
  You need to define a finder by access_token for this strategy.
90
95
  Write on the warden initializer the following code:
91
- Warden::Strategies[:#{config.provider_name}_oauth].access_token_user_finder do |access_token|
96
+ Warden::OAuth.access_token_user_finder(:#{config.provider_name}) do |access_token|
92
97
  # Logic to get your user from an access_token
93
98
  end
94
99
 
@@ -31,6 +31,10 @@ module Warden
31
31
  strategy_class = self.create_oauth_strategy_class(keyword)
32
32
  self.register_oauth_strategy_class(keyword, strategy_class)
33
33
  self.set_oauth_service_info(strategy_class, config)
34
+ # adding the access_token_user_finder to the strategy
35
+ if self.access_token_user_finders.include?(keyword)
36
+ strategy_class.access_token_user_finder(&self.access_token_user_finders[keyword])
37
+ end
34
38
  end
35
39
 
36
40
  #
@@ -4,7 +4,7 @@ require 'rubygems'
4
4
  require 'warden_oauth'
5
5
  require File.dirname(__FILE__) + "/application_scenario"
6
6
 
7
- Warden::Manager.access_token_user_finder do |access_token|
7
+ Warden::OAuth.access_token_user_finder(:example) do |access_token|
8
8
  nil
9
9
  end
10
10
 
@@ -0,0 +1,5 @@
1
+ --colour
2
+ --format specdoc
3
+ --loadby mtime
4
+ --reverse
5
+
@@ -1,14 +1,18 @@
1
1
  require File.dirname(__FILE__) + "/../spec_helper"
2
2
 
3
- describe Warden::Manager do
3
+ describe Warden::Config do
4
4
 
5
5
  before(:each) do
6
6
  failure_app = lambda { |env| "Failure" }
7
- @manager = Warden::Manager.new(nil, :failure_app => failure_app)
7
+ config = nil
8
+ Warden::Manager.new(nil, :failure_app => failure_app) do |_config|
9
+ config = _config
10
+ end
11
+ @config = config
8
12
  end
9
13
 
10
14
  it "should respond to an `oauth` message" do
11
- @manager.should respond_to(:oauth)
15
+ @config.should respond_to(:oauth)
12
16
  end
13
17
 
14
18
  describe "#oauth" do
@@ -17,7 +21,7 @@ describe Warden::Manager do
17
21
 
18
22
  it "should require setting the consumer_key" do
19
23
  lambda do
20
- @manager.oauth(:service) do |service|
24
+ @config.oauth(:service) do |service|
21
25
  service.consumer_secret "ABC"
22
26
  end
23
27
  end.should raise_error(Warden::OAuth::ConfigError, "You need to specify the consumer key and the consumer secret")
@@ -25,14 +29,14 @@ describe Warden::Manager do
25
29
 
26
30
  it "should require setting the consumer_secret" do
27
31
  lambda do
28
- @manager.oauth(:service) do |service|
32
+ @config.oauth(:service) do |service|
29
33
  service.consumer_key "ABC"
30
34
  end
31
35
  end.should raise_error(Warden::OAuth::ConfigError, "You need to specify the consumer key and the consumer secret")
32
36
  end
33
37
 
34
38
  it "should create a new instance of strategy" do
35
- @manager.oauth(:service) do |service|
39
+ @config.oauth(:service) do |service|
36
40
  service.consumer_key "ABC"
37
41
  service.consumer_secret "123"
38
42
  end
@@ -60,8 +60,8 @@ describe Warden::OAuth::Strategy do
60
60
  before(:each) do
61
61
  FakeWeb.register_uri(:post, 'http://localhost:3000/oauth/request_token',
62
62
  :body => fixture_response("unauthorized_request_token"))
63
- @response = @request.get("/", :input => 'warden_oauth_provider=example')
64
- end
63
+ @response = @request.get("/", :params => { 'warden_oauth_provider' => 'example' })
64
+ end
65
65
 
66
66
  it "should redirect to the authorize url" do
67
67
  @response.headers['Location'].should =~ %r"http://localhost:3000/oauth/authorize"
@@ -76,7 +76,12 @@ describe Warden::OAuth::Strategy do
76
76
  $app
77
77
  end
78
78
 
79
- describe "and the access_token_finder hasn't been declared" do
79
+ before(:each) do
80
+ Warden::Strategies.clear!
81
+ Warden::OAuth::Strategy.send(:remove_const, "Example") if Warden::OAuth::Strategy.const_defined?("Example")
82
+ end
83
+
84
+ describe "and the access_token_user_finder hasn't been declared" do
80
85
 
81
86
  before(:each) do
82
87
  FakeWeb.register_uri(:post, 'http://localhost:3000/oauth/request_token',
@@ -95,10 +100,10 @@ describe Warden::OAuth::Strategy do
95
100
 
96
101
  end
97
102
 
98
- describe "and the access_token_finder has been declared" do
103
+ describe "and the access_token_user_finder has been declared" do
99
104
 
100
105
  before(:each) do
101
- Warden::Strategies[:example_oauth].access_token_user_finder do |access_token|
106
+ Warden::OAuth.access_token_user_finder(:example) do |access_token|
102
107
  Object.new if access_token.token == 'ABC' && access_token.secret == '123'
103
108
  end
104
109
  FakeWeb.register_uri(:post, 'http://localhost:3000/oauth/request_token',
@@ -106,6 +111,10 @@ describe Warden::OAuth::Strategy do
106
111
  get "/", 'warden_oauth_provider' => 'example'
107
112
  end
108
113
 
114
+ after(:each) do
115
+ Warden::OAuth.clear_access_token_user_finders
116
+ end
117
+
109
118
  describe "and the user is not found" do
110
119
 
111
120
  before(:each) do
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{warden_oauth}
8
- s.version = "0.0.0"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Roman Gonzalez"]
12
- s.date = %q{2009-11-02}
12
+ s.date = %q{2010-01-09}
13
13
  s.description = %q{
14
14
  warden_oauth will help you create oauth authentication strategies using the oauth
15
15
  helper method on the Warden::Manager config setup
@@ -27,10 +27,12 @@ Gem::Specification.new do |s|
27
27
  "README.rdoc",
28
28
  "Rakefile",
29
29
  "VERSION",
30
+ "examples/twitter/application.rb",
30
31
  "lib/warden_oauth.rb",
32
+ "lib/warden_oauth/base.rb",
31
33
  "lib/warden_oauth/config.rb",
34
+ "lib/warden_oauth/config_extension.rb",
32
35
  "lib/warden_oauth/errors.rb",
33
- "lib/warden_oauth/manager.rb",
34
36
  "lib/warden_oauth/strategy.rb",
35
37
  "lib/warden_oauth/strategy_builder.rb",
36
38
  "lib/warden_oauth/utils.rb",
@@ -38,8 +40,9 @@ Gem::Specification.new do |s|
38
40
  "spec/application_scenario.rb",
39
41
  "spec/fixtures/authorize_request_token.txt",
40
42
  "spec/fixtures/unauthorized_request_token.txt",
43
+ "spec/spec.opts",
41
44
  "spec/spec_helper.rb",
42
- "spec/warden_oauth/manager_spec.rb",
45
+ "spec/warden_oauth/config_extension_spec.rb",
43
46
  "spec/warden_oauth/strategy_spec.rb",
44
47
  "warden_oauth.gemspec"
45
48
  ]
@@ -53,8 +56,9 @@ Gem::Specification.new do |s|
53
56
  "spec/application_runner.rb",
54
57
  "spec/application_scenario.rb",
55
58
  "spec/spec_helper.rb",
56
- "spec/warden_oauth/manager_spec.rb",
57
- "spec/warden_oauth/strategy_spec.rb"
59
+ "spec/warden_oauth/config_extension_spec.rb",
60
+ "spec/warden_oauth/strategy_spec.rb",
61
+ "examples/twitter/application.rb"
58
62
  ]
59
63
 
60
64
  if s.respond_to? :specification_version then
@@ -62,14 +66,14 @@ Gem::Specification.new do |s|
62
66
  s.specification_version = 3
63
67
 
64
68
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
65
- s.add_runtime_dependency(%q<warden>, [">= 0"])
69
+ s.add_runtime_dependency(%q<warden>, [">= 0.8.1"])
66
70
  s.add_runtime_dependency(%q<oauth>, [">= 0"])
67
71
  s.add_development_dependency(%q<rack-test>, [">= 0"])
68
72
  s.add_development_dependency(%q<fakeweb>, [">= 0"])
69
73
  s.add_development_dependency(%q<rspec>, [">= 0"])
70
74
  s.add_development_dependency(%q<yard>, [">= 0"])
71
75
  else
72
- s.add_dependency(%q<warden>, [">= 0"])
76
+ s.add_dependency(%q<warden>, [">= 0.8.1"])
73
77
  s.add_dependency(%q<oauth>, [">= 0"])
74
78
  s.add_dependency(%q<rack-test>, [">= 0"])
75
79
  s.add_dependency(%q<fakeweb>, [">= 0"])
@@ -77,7 +81,7 @@ Gem::Specification.new do |s|
77
81
  s.add_dependency(%q<yard>, [">= 0"])
78
82
  end
79
83
  else
80
- s.add_dependency(%q<warden>, [">= 0"])
84
+ s.add_dependency(%q<warden>, [">= 0.8.1"])
81
85
  s.add_dependency(%q<oauth>, [">= 0"])
82
86
  s.add_dependency(%q<rack-test>, [">= 0"])
83
87
  s.add_dependency(%q<fakeweb>, [">= 0"])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: warden_oauth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Gonzalez
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-02 00:00:00 -08:00
12
+ date: 2010-01-09 00:00:00 -04:30
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: "0"
23
+ version: 0.8.1
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: oauth
@@ -89,10 +89,12 @@ files:
89
89
  - README.rdoc
90
90
  - Rakefile
91
91
  - VERSION
92
+ - examples/twitter/application.rb
92
93
  - lib/warden_oauth.rb
94
+ - lib/warden_oauth/base.rb
93
95
  - lib/warden_oauth/config.rb
96
+ - lib/warden_oauth/config_extension.rb
94
97
  - lib/warden_oauth/errors.rb
95
- - lib/warden_oauth/manager.rb
96
98
  - lib/warden_oauth/strategy.rb
97
99
  - lib/warden_oauth/strategy_builder.rb
98
100
  - lib/warden_oauth/utils.rb
@@ -100,8 +102,9 @@ files:
100
102
  - spec/application_scenario.rb
101
103
  - spec/fixtures/authorize_request_token.txt
102
104
  - spec/fixtures/unauthorized_request_token.txt
105
+ - spec/spec.opts
103
106
  - spec/spec_helper.rb
104
- - spec/warden_oauth/manager_spec.rb
107
+ - spec/warden_oauth/config_extension_spec.rb
105
108
  - spec/warden_oauth/strategy_spec.rb
106
109
  - warden_oauth.gemspec
107
110
  has_rdoc: true
@@ -136,5 +139,6 @@ test_files:
136
139
  - spec/application_runner.rb
137
140
  - spec/application_scenario.rb
138
141
  - spec/spec_helper.rb
139
- - spec/warden_oauth/manager_spec.rb
142
+ - spec/warden_oauth/config_extension_spec.rb
140
143
  - spec/warden_oauth/strategy_spec.rb
144
+ - examples/twitter/application.rb