lsdr-authlogic-connect 0.0.3.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.markdown +240 -0
  3. data/Rakefile +71 -0
  4. data/init.rb +1 -0
  5. data/lib/authlogic-connect.rb +27 -0
  6. data/lib/authlogic_connect/authlogic_connect.rb +46 -0
  7. data/lib/authlogic_connect/callback_filter.rb +19 -0
  8. data/lib/authlogic_connect/common.rb +10 -0
  9. data/lib/authlogic_connect/common/session.rb +27 -0
  10. data/lib/authlogic_connect/common/state.rb +16 -0
  11. data/lib/authlogic_connect/common/user.rb +115 -0
  12. data/lib/authlogic_connect/common/variables.rb +77 -0
  13. data/lib/authlogic_connect/engine.rb +14 -0
  14. data/lib/authlogic_connect/ext.rb +56 -0
  15. data/lib/authlogic_connect/oauth.rb +14 -0
  16. data/lib/authlogic_connect/oauth/helper.rb +20 -0
  17. data/lib/authlogic_connect/oauth/process.rb +68 -0
  18. data/lib/authlogic_connect/oauth/session.rb +58 -0
  19. data/lib/authlogic_connect/oauth/state.rb +54 -0
  20. data/lib/authlogic_connect/oauth/tokens/facebook_token.rb +11 -0
  21. data/lib/authlogic_connect/oauth/tokens/get_satisfaction_token.rb +9 -0
  22. data/lib/authlogic_connect/oauth/tokens/google_token.rb +41 -0
  23. data/lib/authlogic_connect/oauth/tokens/linked_in_token.rb +19 -0
  24. data/lib/authlogic_connect/oauth/tokens/myspace_token.rb +26 -0
  25. data/lib/authlogic_connect/oauth/tokens/oauth_token.rb +131 -0
  26. data/lib/authlogic_connect/oauth/tokens/opensocial_token.rb +0 -0
  27. data/lib/authlogic_connect/oauth/tokens/twitter_token.rb +8 -0
  28. data/lib/authlogic_connect/oauth/tokens/vimeo_token.rb +18 -0
  29. data/lib/authlogic_connect/oauth/tokens/yahoo_token.rb +19 -0
  30. data/lib/authlogic_connect/oauth/user.rb +68 -0
  31. data/lib/authlogic_connect/oauth/variables.rb +55 -0
  32. data/lib/authlogic_connect/openid.rb +11 -0
  33. data/lib/authlogic_connect/openid/process.rb +30 -0
  34. data/lib/authlogic_connect/openid/session.rb +78 -0
  35. data/lib/authlogic_connect/openid/state.rb +47 -0
  36. data/lib/authlogic_connect/openid/tokens/aol_token.rb +0 -0
  37. data/lib/authlogic_connect/openid/tokens/blogger_token.rb +0 -0
  38. data/lib/authlogic_connect/openid/tokens/flickr_token.rb +0 -0
  39. data/lib/authlogic_connect/openid/tokens/my_openid_token.rb +3 -0
  40. data/lib/authlogic_connect/openid/tokens/openid_token.rb +9 -0
  41. data/lib/authlogic_connect/openid/user.rb +62 -0
  42. data/lib/authlogic_connect/openid/variables.rb +19 -0
  43. data/lib/authlogic_connect/token.rb +53 -0
  44. data/lib/open_id_authentication.rb +128 -0
  45. data/rails/init.rb +19 -0
  46. data/test/controllers/test_users_controller.rb +21 -0
  47. data/test/libs/database.rb +48 -0
  48. data/test/libs/user.rb +3 -0
  49. data/test/libs/user_session.rb +2 -0
  50. data/test/old.rb +53 -0
  51. data/test/test_authlogic_connect.rb +13 -0
  52. data/test/test_helper.rb +153 -0
  53. data/test/test_user.rb +255 -0
  54. metadata +247 -0
@@ -0,0 +1,19 @@
1
+ require "authlogic-connect"
2
+
3
+ # copied from open_id_authentication plugin on github
4
+
5
+ # this is the Rails 2.x equivalent.
6
+ # Rails 3 equivalent is in authlogic_connect/engine.rb
7
+ if Rails.version < '3'
8
+ config.gem 'rack-openid', :lib => 'rack/openid', :version => '>=0.2.1'
9
+ end
10
+
11
+ require 'open_id_authentication'
12
+
13
+ config.middleware.use OpenIdAuthentication
14
+ config.middleware.use AuthlogicConnect::CallbackFilter
15
+
16
+ config.after_initialize do
17
+ OpenID::Util.logger = Rails.logger
18
+ ActionController::Base.send :include, OpenIdAuthentication
19
+ end
@@ -0,0 +1,21 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ class UsersControllerTest < ActionController::TestCase
4
+
5
+ tests UsersController
6
+
7
+ context "when signed out" do
8
+ # setup { sign_out }
9
+
10
+ context "on GET to #new" do
11
+
12
+ setup { get :new }
13
+
14
+ should "do something???" do
15
+ puts "REQUEST: #{@user.inspect}"
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,48 @@
1
+
2
+ begin
3
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
4
+ rescue ArgumentError
5
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
6
+ end
7
+
8
+ ActiveRecord::Base.configurations = true
9
+
10
+ # this schema was directly copied from
11
+ # http://github.com/viatropos/authlogic-connect-example/blob/master/db/schema.rb
12
+ ActiveRecord::Schema.define(:version => 1) do
13
+
14
+ create_table :sessions, :force => true do |t|
15
+ t.string :session_id, :null => false
16
+ t.text :data
17
+ t.datetime :created_at
18
+ t.datetime :updated_at
19
+ end
20
+
21
+ create_table :tokens, :force => true do |t|
22
+ t.integer :user_id
23
+ t.string :type, :limit => 30
24
+ t.string :key, :limit => 1024
25
+ t.string :secret
26
+ t.boolean :active
27
+ t.datetime :created_at
28
+ t.datetime :updated_at
29
+ end
30
+
31
+ create_table :users, :force => true do |t|
32
+ t.datetime :created_at
33
+ t.datetime :updated_at
34
+ t.string :login
35
+ t.string :crypted_password
36
+ t.string :password_salt
37
+ t.string :persistence_token, :null => false
38
+ t.integer :login_count, :default => 0, :null => false
39
+ t.datetime :last_request_at
40
+ t.datetime :last_login_at
41
+ t.datetime :current_login_at
42
+ t.string :last_login_ip
43
+ t.string :current_login_ip
44
+ t.string :openid_identifier
45
+ t.integer :active_token_id
46
+ end
47
+
48
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ acts_as_authentic
3
+ end
@@ -0,0 +1,2 @@
1
+ class UserSession < Authlogic::Session::Base
2
+ end
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'tempfile'
3
+ require 'test/unit'
4
+
5
+ require 'shoulda'
6
+ gem 'activerecord', '~>3.0.0'
7
+ gem 'activesupport', '~>3.0.0'
8
+ gem 'actionpack', '~>3.0.0'
9
+ require 'active_record'
10
+ require 'active_record/version'
11
+ require 'active_support'
12
+ require 'action_pack'
13
+ gem "ruby-openid"
14
+ gem 'rack-openid', '>=0.2.1'
15
+ gem "authlogic", :git => "git://github.com/odorcicd/authlogic.git", :branch => "rails3"
16
+ require 'authlogic'
17
+ gem "oauth"
18
+ gem "oauth2"
19
+
20
+ puts "Testing against version #{ActiveRecord::VERSION::STRING}"
21
+
22
+ begin
23
+ require 'ruby-debug'
24
+ rescue LoadError => e
25
+ puts "debugger disabled"
26
+ end
27
+
28
+ ROOT = File.join(File.dirname(__FILE__), '..')
29
+
30
+ def silence_warnings
31
+ old_verbose, $VERBOSE = $VERBOSE, nil
32
+ yield
33
+ ensure
34
+ $VERBOSE = old_verbose
35
+ end
36
+
37
+ class Test::Unit::TestCase
38
+ def setup
39
+ silence_warnings do
40
+ Object.const_set(:Rails, stub('Rails', :root => ROOT, :env => 'test'))
41
+ end
42
+ end
43
+ end
44
+
45
+ $LOAD_PATH << File.join(ROOT, 'lib')
46
+ $LOAD_PATH << File.join(ROOT, 'lib', 'authlogic-connect')
47
+
48
+ require File.join(ROOT, 'lib', 'authlogic-connect.rb')
49
+
50
+ FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
51
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
52
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
53
+ ActiveRecord::Base.establish_connection(config['test'])
@@ -0,0 +1,13 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class AuthlogicConnectTest < Test::Unit::TestCase
4
+ context "AuthlogicConnect.config" do
5
+ setup do
6
+ AuthlogicConnect.config = {}
7
+ end
8
+
9
+ should "have an empty configuration hash" do
10
+ assert_equal true, AuthlogicConnect.config.empty?
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,153 @@
1
+ require "test/unit"
2
+ require "rubygems"
3
+ require "ruby-debug"
4
+ gem "activerecord", "= 2.3.5"
5
+ require "active_record"
6
+ require "active_record/fixtures"
7
+ gem "activesupport", "= 2.3.5"
8
+ require 'active_support'
9
+ gem 'actionpack', "= 2.3.5"
10
+ require 'action_controller'
11
+ require 'shoulda'
12
+
13
+ require File.dirname(__FILE__) + '/libs/database'
14
+ require File.dirname(__FILE__) + '/../lib/authlogic-connect' unless defined?(AuthlogicConnect)
15
+ require File.dirname(__FILE__) + '/libs/user'
16
+ require File.dirname(__FILE__) + '/libs/user_session'
17
+ require 'authlogic/test_case'
18
+
19
+ # A temporary fix to bring active record errors up to speed with rails edge.
20
+ # I need to remove this once the new gem is released. This is only here so my tests pass.
21
+ unless defined?(::ActiveModel)
22
+ class ActiveRecord::Errors
23
+ def [](key)
24
+ value = on(key)
25
+ value.is_a?(Array) ? value : [value].compact
26
+ end
27
+ end
28
+ end
29
+
30
+
31
+ AuthlogicConnect.config = {
32
+ :default => "twitter",
33
+ :connect => {
34
+ :twitter => {
35
+ :key => "my_key",
36
+ :secret => "my_secret",
37
+ :label => "Twitter"
38
+ },
39
+ :facebook => {
40
+ :key => "my_key",
41
+ :secret => "my_secret",
42
+ :label => "Facebook"
43
+ },
44
+ :google => {
45
+ :key => "my_key",
46
+ :secret => "my_secret",
47
+ :label => "Google"
48
+ },
49
+ :yahoo => {
50
+ :key => "my_key",
51
+ :secret => "my_secret",
52
+ :label => "Yahoo"
53
+ },
54
+ :vimeo => {
55
+
56
+ }
57
+ }
58
+ }
59
+
60
+ # want to add a "method" property!
61
+ Authlogic::TestCase::MockRequest.class_eval do
62
+ def method
63
+ "POST"
64
+ end
65
+ end
66
+
67
+ module ControllerHelpers
68
+ def controller_name
69
+ "users"
70
+ end
71
+
72
+ def action_name
73
+ "create"
74
+ end
75
+
76
+ def url_for(options = {})
77
+ p = []
78
+ option.each do |k,v|
79
+ p << "#{k}=#{v}"
80
+ end
81
+ p = "?#{p.join("&")}"
82
+ url = "http://localhost:3000/users#{p}"
83
+ end
84
+
85
+ def session=(value)
86
+ @session = value
87
+ end
88
+ end
89
+ Authlogic::ControllerAdapters::AbstractAdapter.send(:include, ControllerHelpers)
90
+
91
+ Authlogic::CryptoProviders::AES256.key = "myafdsfddddddddddddddddddddddddddddddddddddddddddddddd"
92
+
93
+ class ActiveSupport::TestCase
94
+ include ActiveRecord::TestFixtures
95
+ self.fixture_path = File.dirname(__FILE__) + "/fixtures"
96
+ self.use_transactional_fixtures = false
97
+ self.use_instantiated_fixtures = false
98
+ self.pre_loaded_fixtures = false
99
+ fixtures :all
100
+ setup :activate_authlogic
101
+
102
+ private
103
+ def password_for(user)
104
+ case user
105
+ when users(:ben)
106
+ "benrocks"
107
+ when users(:zack)
108
+ "zackrocks"
109
+ end
110
+ end
111
+
112
+ def http_basic_auth_for(user = nil, &block)
113
+ unless user.blank?
114
+ controller.http_user = user.login
115
+ controller.http_password = password_for(user)
116
+ end
117
+ yield
118
+ controller.http_user = controller.http_password = nil
119
+ end
120
+
121
+ def set_cookie_for(user, id = nil)
122
+ controller.cookies["user_credentials"] = {:value => user.persistence_token, :expires => nil}
123
+ end
124
+
125
+ def unset_cookie
126
+ controller.cookies["user_credentials"] = nil
127
+ end
128
+
129
+ def set_params_for(user, id = nil)
130
+ controller.params["user_credentials"] = user.single_access_token
131
+ end
132
+
133
+ def unset_params
134
+ controller.params["user_credentials"] = nil
135
+ end
136
+
137
+ def set_request_content_type(type)
138
+ controller.request_content_type = type
139
+ end
140
+
141
+ def unset_request_content_type
142
+ controller.request_content_type = nil
143
+ end
144
+
145
+ def set_session_for(user, id = nil)
146
+ controller.session["user_credentials"] = user.persistence_token
147
+ controller.session["user_credentials_id"] = user.id
148
+ end
149
+
150
+ def unset_session
151
+ controller.session["user_credentials"] = controller.session["user_credentials_id"] = nil
152
+ end
153
+ end
@@ -0,0 +1,255 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ module AuthlogicConnect
4
+ class UserTest < Test::Unit::TestCase
5
+ context "User creation" do
6
+ setup do
7
+ @user = User.new(:login => "viatropos")
8
+ end
9
+
10
+ should "make sure we are loading the models" do
11
+ assert_equal "viatropos", @user.login
12
+ end
13
+
14
+ context "responds to added oauth methods (our oauth api on the user)" do
15
+
16
+ should "have 'tokens' method" do
17
+ assert @user.respond_to?(:tokens)
18
+ assert_equal [], @user.tokens
19
+ end
20
+
21
+ should "have 'active_token' method" do
22
+ assert @user.respond_to?(:active_token)
23
+ assert_equal nil, @user.active_token
24
+ end
25
+
26
+ end
27
+
28
+ context "with controller and session..." do
29
+
30
+ setup do
31
+ controller.params.merge!(:authentication_type => "user")
32
+ Authlogic::Session::Base.controller = controller
33
+ end
34
+
35
+ should "have a valid controller" do
36
+ assert @user.auth_controller
37
+ end
38
+
39
+ should "have auth_params" do
40
+ assert @user.auth_params?
41
+ end
42
+
43
+ should "have an empty 'auth_session'" do
44
+ assert @user.auth_session.empty?
45
+ assert_equal false, @user.auth_session?
46
+ end
47
+
48
+ context "save the user without any parameters" do
49
+
50
+ setup do
51
+ @save_success = @user.save
52
+ end
53
+
54
+ should "not be a valid save" do
55
+ assert_equal false, @save_success
56
+ end
57
+
58
+ should "not be using oauth" do
59
+ assert_equal false, @user.using_oauth?
60
+ end
61
+
62
+ should "not be using openid" do
63
+ assert_equal false, @user.using_openid?
64
+ end
65
+
66
+ end
67
+
68
+ context "with oauth parameters" do
69
+
70
+ setup do
71
+ @user.auth_controller.params.merge!(:oauth_provider => "twitter")
72
+ # mock token
73
+ @token = OAuth::RequestToken.new("twitter", "key", "secret")
74
+ @token.params = {
75
+ :oauth_callback_confirmed => "true",
76
+ :oauth_token_secret=>"secret",
77
+ :oauth_token=>"key"
78
+ }
79
+ @token.consumer = OAuth::Consumer.new("key", "secret",
80
+ :site=>"http://twitter.com",
81
+ :proxy=>nil,
82
+ :oauth_version=>"1.0",
83
+ :request_token_path=>"/oauth/request_token",
84
+ :authorize_path=>"/oauth/authorize",
85
+ :scheme=>:header,
86
+ :signature_method=>"HMAC-SHA1",
87
+ :authorize_url=>"http://twitter.com/oauth/authenticate",
88
+ :access_token_path=>"/oauth/access_token"
89
+ )
90
+ @session_vars = [
91
+ :authentication_type,
92
+ :auth_request_class,
93
+ :oauth_provider,
94
+ :auth_callback_method
95
+ ]
96
+ end
97
+
98
+ should "have an 'oauth_provider'" do
99
+ assert @user.oauth_provider?
100
+ end
101
+
102
+ should "be an 'oauth_request'" do
103
+ assert @user.oauth_request?
104
+ end
105
+
106
+ should "not be an 'oauth_response'" do
107
+ assert_equal false, @user.oauth_response?
108
+ end
109
+
110
+ should "be using oauth" do
111
+ assert @user.using_oauth?
112
+ end
113
+
114
+ should "not be using openid" do
115
+ assert_equal false, @user.using_openid?
116
+ end
117
+
118
+ should "have the correct class (authentication_type == user)" do
119
+ assert @user.correct_request_class?
120
+ end
121
+
122
+ should "realize we are authenticating_with_oauth?" do
123
+ assert @user.authenticating_with_oauth?
124
+ end
125
+
126
+ context "and 'save_with_oauth', manually checking each step" do
127
+
128
+ setup do
129
+ # mock save
130
+ # this, and the whole redirect process happens
131
+ # but we'll just assume we saved the session data and got the redirect back
132
+ @user.save_oauth_session
133
+ @user.save(:skip_redirect => true, :keep_session => true) do
134
+ "I'm the block you want"
135
+ end
136
+ # copy to test controller
137
+ @user.auth_session.each do |key, value|
138
+ @user.auth_controller.session[key] = value
139
+ end
140
+ end
141
+
142
+ should "should have a full session" do
143
+ @session_vars.each {|key| assert @user.auth_session.has_key?(key)}
144
+ end
145
+
146
+ should "'cleanup_auth_session'" do
147
+ @user.cleanup_auth_session
148
+ @session_vars.each {|key| assert_equal false, @user.auth_session.has_key?(key)}
149
+ end
150
+
151
+ teardown do
152
+ @user.destroy
153
+ end
154
+
155
+ end
156
+
157
+ context "and 'save_with_oauth' completely" do
158
+ setup do
159
+ # mock save
160
+ # this, and the whole redirect process happens
161
+ # but we'll just assume we saved the session data and got the redirect back
162
+ @user.save_oauth_session
163
+ @user.save(:skip_redirect => true, :keep_session => false) do
164
+ "I'm the block you want"
165
+ end
166
+ # copy to test controller
167
+ @user.auth_controller.session = @user.auth_session
168
+ end
169
+
170
+ should "have a clear session" do
171
+ @session_vars.each do |key|
172
+ assert_equal false, @user.auth_session.has_key?(key)
173
+ end
174
+ end
175
+
176
+ should "be a valid save" do
177
+ assert @user.valid?
178
+ end
179
+
180
+ # so login isn't saved
181
+ teardown do
182
+ User.all.collect(&:destroy)
183
+ end
184
+ end
185
+
186
+
187
+ end
188
+
189
+ context "with openid parameters" do
190
+ setup do
191
+ @user.auth_controller.params.merge!(:openid_identifier => "viatropos.myopenid.com")
192
+ @session_vars = [
193
+ :authentication_type,
194
+ :auth_request_class,
195
+ :openid_identifier,
196
+ :auth_callback_method
197
+ ]
198
+ end
199
+
200
+ should "have an 'openid_identifier'" do
201
+ assert_equal true, @user.openid_identifier?
202
+ end
203
+
204
+ should "be an 'openid_request'" do
205
+ assert @user.openid_request?
206
+ end
207
+
208
+ should "not be an 'openid_response'" do
209
+ assert_equal false, @user.openid_response?
210
+ end
211
+
212
+ should "be using openid" do
213
+ assert @user.using_openid?
214
+ end
215
+
216
+ should "not be using oauth" do
217
+ assert_equal false, @user.using_oauth?
218
+ end
219
+
220
+ should "have the correct class (authentication_type == user)" do
221
+ assert @user.correct_request_class?
222
+ end
223
+
224
+ should "realize we are authenticating_with_openid?" do
225
+ assert @user.authenticating_with_openid?
226
+ end
227
+
228
+ context "and 'save_with_openid', manually checking each step" do
229
+
230
+ setup do
231
+ # mock save
232
+ # this, and the whole redirect process happens
233
+ # but we'll just assume we saved the session data and got the redirect back
234
+ @user.save_openid_session
235
+ @user.save(:skip_redirect => true, :keep_session => true) do
236
+ "I'm the block you want"
237
+ end
238
+ # copy to test controller
239
+ @user.auth_session.each do |key, value|
240
+ @user.auth_controller.session[key] = value
241
+ end
242
+ end
243
+
244
+ teardown do
245
+ @user.destroy
246
+ end
247
+
248
+ end
249
+ end
250
+
251
+ end
252
+ end
253
+
254
+ end
255
+ end