authlogic-connect-x 0.0.4.05x

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.markdown +248 -0
  3. data/Rakefile +75 -0
  4. data/init.rb +1 -0
  5. data/lib/authlogic-connect.rb +27 -0
  6. data/lib/authlogic_connect/access_token.rb +53 -0
  7. data/lib/authlogic_connect/authlogic_connect.rb +46 -0
  8. data/lib/authlogic_connect/callback_filter.rb +19 -0
  9. data/lib/authlogic_connect/common.rb +10 -0
  10. data/lib/authlogic_connect/common/session.rb +30 -0
  11. data/lib/authlogic_connect/common/state.rb +32 -0
  12. data/lib/authlogic_connect/common/user.rb +77 -0
  13. data/lib/authlogic_connect/common/variables.rb +137 -0
  14. data/lib/authlogic_connect/engine.rb +14 -0
  15. data/lib/authlogic_connect/ext.rb +56 -0
  16. data/lib/authlogic_connect/oauth.rb +14 -0
  17. data/lib/authlogic_connect/oauth/helper.rb +20 -0
  18. data/lib/authlogic_connect/oauth/process.rb +75 -0
  19. data/lib/authlogic_connect/oauth/session.rb +62 -0
  20. data/lib/authlogic_connect/oauth/state.rb +60 -0
  21. data/lib/authlogic_connect/oauth/tokens/aol_token.rb +2 -0
  22. data/lib/authlogic_connect/oauth/tokens/facebook_token.rb +11 -0
  23. data/lib/authlogic_connect/oauth/tokens/get_satisfaction_token.rb +9 -0
  24. data/lib/authlogic_connect/oauth/tokens/google_token.rb +41 -0
  25. data/lib/authlogic_connect/oauth/tokens/linked_in_token.rb +19 -0
  26. data/lib/authlogic_connect/oauth/tokens/meetup_token.rb +12 -0
  27. data/lib/authlogic_connect/oauth/tokens/myspace_token.rb +26 -0
  28. data/lib/authlogic_connect/oauth/tokens/netflix_token.rb +10 -0
  29. data/lib/authlogic_connect/oauth/tokens/oauth_token.rb +144 -0
  30. data/lib/authlogic_connect/oauth/tokens/oauth_token.rb~ +140 -0
  31. data/lib/authlogic_connect/oauth/tokens/ohloh_token.rb +9 -0
  32. data/lib/authlogic_connect/oauth/tokens/opensocial_token.rb +0 -0
  33. data/lib/authlogic_connect/oauth/tokens/twitter_token.rb +8 -0
  34. data/lib/authlogic_connect/oauth/tokens/vimeo_token.rb +18 -0
  35. data/lib/authlogic_connect/oauth/tokens/yahoo_token.rb +19 -0
  36. data/lib/authlogic_connect/oauth/user.rb +63 -0
  37. data/lib/authlogic_connect/oauth/variables.rb +64 -0
  38. data/lib/authlogic_connect/openid.rb +11 -0
  39. data/lib/authlogic_connect/openid/process.rb +74 -0
  40. data/lib/authlogic_connect/openid/session.rb +56 -0
  41. data/lib/authlogic_connect/openid/state.rb +48 -0
  42. data/lib/authlogic_connect/openid/tokens/aol_token.rb +0 -0
  43. data/lib/authlogic_connect/openid/tokens/blogger_token.rb +0 -0
  44. data/lib/authlogic_connect/openid/tokens/flickr_token.rb +0 -0
  45. data/lib/authlogic_connect/openid/tokens/my_openid_token.rb +3 -0
  46. data/lib/authlogic_connect/openid/tokens/openid_token.rb +9 -0
  47. data/lib/authlogic_connect/openid/user.rb +38 -0
  48. data/lib/authlogic_connect/openid/variables.rb +19 -0
  49. data/lib/open_id_authentication.rb +127 -0
  50. data/rails/init.rb +19 -0
  51. data/test/controllers/test_users_controller.rb +21 -0
  52. data/test/libs/database.rb +48 -0
  53. data/test/libs/user.rb +7 -0
  54. data/test/libs/user_session.rb +2 -0
  55. data/test/old.rb +53 -0
  56. data/test/test_authlogic_connect.rb +13 -0
  57. data/test/test_helper.rb +153 -0
  58. data/test/test_user.rb +194 -0
  59. metadata +242 -0
@@ -0,0 +1,19 @@
1
+ module AuthlogicConnect::Openid::Variables
2
+ include AuthlogicConnect::Openid::State
3
+
4
+ # openid_provider = "blogger", "myopenid", etc.
5
+ # openid_identifier = "viatropos.myopenid.com", etc.
6
+ # openid_key = "viatropos"
7
+ # def openid_attributes
8
+ # [:openid_provider, :openid_identifier, :openid_key]
9
+ # end
10
+
11
+ def openid_identifier
12
+ auth_params[:openid_identifier] if auth_params?
13
+ end
14
+
15
+ def openid_provider
16
+ from_session_or_params(:openid_provider) if auth_controller?
17
+ end
18
+
19
+ end
@@ -0,0 +1,127 @@
1
+ # copied from open_id_authentication plugin on github
2
+ require 'uri'
3
+ require 'openid'
4
+ require 'rack/openid'
5
+
6
+ module OpenIdAuthentication
7
+ def self.new(app)
8
+ store = OpenIdAuthentication.store
9
+ if store.nil?
10
+ Rails.logger.warn "OpenIdAuthentication.store is nil. Using in-memory store."
11
+ end
12
+
13
+ ::Rack::OpenID.new(app, OpenIdAuthentication.store)
14
+ end
15
+
16
+ def self.store
17
+ @@store
18
+ end
19
+
20
+ def self.store=(*store_option)
21
+ store, *parameters = *([ store_option ].flatten)
22
+
23
+ @@store = case store
24
+ when :memory
25
+ require 'openid/store/memory'
26
+ OpenID::Store::Memory.new
27
+ when :file
28
+ require 'openid/store/filesystem'
29
+ OpenID::Store::Filesystem.new(Rails.root.join('tmp/openids'))
30
+ when :memcache
31
+ require 'memcache'
32
+ require 'openid/store/memcache'
33
+ OpenID::Store::Memcache.new(MemCache.new(parameters))
34
+ else
35
+ store
36
+ end
37
+ end
38
+
39
+ self.store = nil
40
+
41
+ class Result
42
+ ERROR_MESSAGES = {
43
+ :missing => "Sorry, the OpenID server couldn't be found",
44
+ :invalid => "Sorry, but this does not appear to be a valid OpenID",
45
+ :canceled => "OpenID verification was canceled",
46
+ :failed => "OpenID verification failed",
47
+ :setup_needed => "OpenID verification needs setup"
48
+ }
49
+
50
+ def self.[](code)
51
+ new(code)
52
+ end
53
+
54
+ def initialize(code)
55
+ @code = code
56
+ end
57
+
58
+ def status
59
+ @code
60
+ end
61
+
62
+ ERROR_MESSAGES.keys.each { |state| define_method("#{state}?") { @code == state } }
63
+
64
+ def successful?
65
+ @code == :successful
66
+ end
67
+
68
+ def unsuccessful?
69
+ ERROR_MESSAGES.keys.include?(@code)
70
+ end
71
+
72
+ def message
73
+ ERROR_MESSAGES[@code]
74
+ end
75
+ end
76
+
77
+ protected
78
+ # The parameter name of "openid_identifier" is used rather than
79
+ # the Rails convention "open_id_identifier" because that's what
80
+ # the specification dictates in order to get browser auto-complete
81
+ # working across sites
82
+ def using_open_id?(identifier = nil) #:doc:
83
+ identifier ||= open_id_identifier
84
+ !identifier.blank? || request.env[Rack::OpenID::RESPONSE]
85
+ end
86
+
87
+ def authenticate_with_open_id(identifier = nil, options = {}, &block) #:doc:
88
+ identifier ||= open_id_identifier
89
+ if request.env[Rack::OpenID::RESPONSE]
90
+ complete_open_id_authentication(&block)
91
+ else
92
+ begin_open_id_authentication(identifier, options, &block)
93
+ end
94
+ end
95
+
96
+ private
97
+ def open_id_identifier
98
+ params[:openid_identifier] || params[:openid_url]
99
+ end
100
+
101
+ def begin_open_id_authentication(identifier, options = {})
102
+ options[:identifier] = identifier
103
+ value = Rack::OpenID.build_header(options)
104
+ response.headers[Rack::OpenID::AUTHENTICATE_HEADER] = value
105
+ head :unauthorized
106
+ end
107
+
108
+ def complete_open_id_authentication
109
+ response = request.env[Rack::OpenID::RESPONSE]
110
+ identifier = response.display_identifier
111
+ case response.status
112
+ when OpenID::Consumer::SUCCESS
113
+ yield Result[:successful], identifier,
114
+ OpenID::SReg::Response.from_success_response(response)
115
+ when :missing
116
+ yield Result[:missing], identifier, nil
117
+ when :invalid
118
+ yield Result[:invalid], identifier, nil
119
+ when OpenID::Consumer::CANCEL
120
+ yield Result[:canceled], identifier, nil
121
+ when OpenID::Consumer::FAILURE
122
+ yield Result[:failed], identifier, nil
123
+ when OpenID::Consumer::SETUP_NEEDED
124
+ yield Result[:setup_needed], response.setup_url, nil
125
+ end
126
+ end
127
+ end
@@ -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,7 @@
1
+ class User < ActiveRecord::Base
2
+ acts_as_authentic do |config|
3
+ config.validate_email_field = false
4
+ config.validate_login_field = false
5
+ config.validate_password_field = false
6
+ end
7
+ 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
+ options.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