jspooner-authlogic-connect 0.0.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README.markdown +234 -0
- data/Rakefile +85 -0
- data/init.rb +1 -0
- data/lib/authlogic-connect.rb +39 -0
- data/lib/authlogic_connect/access_token.rb +61 -0
- data/lib/authlogic_connect/authlogic_connect.rb +46 -0
- data/lib/authlogic_connect/callback_filter.rb +19 -0
- data/lib/authlogic_connect/common/session.rb +30 -0
- data/lib/authlogic_connect/common/state.rb +45 -0
- data/lib/authlogic_connect/common/user.rb +77 -0
- data/lib/authlogic_connect/common/variables.rb +124 -0
- data/lib/authlogic_connect/common.rb +10 -0
- data/lib/authlogic_connect/engine.rb +14 -0
- data/lib/authlogic_connect/ext.rb +56 -0
- data/lib/authlogic_connect/oauth/helper.rb +20 -0
- data/lib/authlogic_connect/oauth/process.rb +77 -0
- data/lib/authlogic_connect/oauth/session.rb +90 -0
- data/lib/authlogic_connect/oauth/state.rb +60 -0
- data/lib/authlogic_connect/oauth/tokens/aol_token.rb +2 -0
- data/lib/authlogic_connect/oauth/tokens/facebook_token.rb +11 -0
- data/lib/authlogic_connect/oauth/tokens/foursquare_token.rb +15 -0
- data/lib/authlogic_connect/oauth/tokens/get_satisfaction_token.rb +9 -0
- data/lib/authlogic_connect/oauth/tokens/github_token.rb +14 -0
- data/lib/authlogic_connect/oauth/tokens/google_token.rb +41 -0
- data/lib/authlogic_connect/oauth/tokens/linked_in_token.rb +19 -0
- data/lib/authlogic_connect/oauth/tokens/meetup_token.rb +12 -0
- data/lib/authlogic_connect/oauth/tokens/myspace_token.rb +26 -0
- data/lib/authlogic_connect/oauth/tokens/netflix_token.rb +10 -0
- data/lib/authlogic_connect/oauth/tokens/oauth_token.rb +164 -0
- data/lib/authlogic_connect/oauth/tokens/ohloh_token.rb +9 -0
- data/lib/authlogic_connect/oauth/tokens/opensocial_token.rb +0 -0
- data/lib/authlogic_connect/oauth/tokens/twitter_token.rb +8 -0
- data/lib/authlogic_connect/oauth/tokens/vimeo_token.rb +18 -0
- data/lib/authlogic_connect/oauth/tokens/yahoo_token.rb +19 -0
- data/lib/authlogic_connect/oauth/user.rb +64 -0
- data/lib/authlogic_connect/oauth/variables.rb +64 -0
- data/lib/authlogic_connect/oauth.rb +14 -0
- data/lib/authlogic_connect/openid/process.rb +74 -0
- data/lib/authlogic_connect/openid/session.rb +56 -0
- data/lib/authlogic_connect/openid/state.rb +48 -0
- data/lib/authlogic_connect/openid/tokens/aol_token.rb +0 -0
- data/lib/authlogic_connect/openid/tokens/blogger_token.rb +0 -0
- data/lib/authlogic_connect/openid/tokens/flickr_token.rb +0 -0
- data/lib/authlogic_connect/openid/tokens/my_openid_token.rb +3 -0
- data/lib/authlogic_connect/openid/tokens/openid_token.rb +9 -0
- data/lib/authlogic_connect/openid/user.rb +38 -0
- data/lib/authlogic_connect/openid/variables.rb +19 -0
- data/lib/authlogic_connect/openid.rb +11 -0
- data/lib/authlogic_connect/rack_state.rb +19 -0
- data/lib/open_id_authentication.rb +127 -0
- data/rails/init.rb +19 -0
- data/test/controllers/test_users_controller.rb +21 -0
- data/test/libs/database.rb +47 -0
- data/test/libs/user.rb +7 -0
- data/test/libs/user_session.rb +2 -0
- data/test/test_helper.rb +178 -0
- data/test/test_oauth.rb +178 -0
- data/test/test_openid.rb +71 -0
- data/test/test_user.rb +85 -0
- metadata +243 -0
@@ -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
|
data/rails/init.rb
ADDED
@@ -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,47 @@
|
|
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 :access_tokens do |t|
|
22
|
+
t.integer :user_id
|
23
|
+
t.string :type, :limit => 30
|
24
|
+
t.string :key # how we identify the user, in case they logout and log back in
|
25
|
+
t.string :token, :limit => 1024 # This has to be huge because of Yahoo's excessively large tokens
|
26
|
+
t.string :secret
|
27
|
+
t.boolean :active # whether or not it's associated with the account
|
28
|
+
t.timestamps
|
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 :email
|
36
|
+
t.string :crypted_password
|
37
|
+
t.string :password_salt
|
38
|
+
t.string :persistence_token, :null => false
|
39
|
+
t.integer :login_count, :default => 0, :null => false
|
40
|
+
t.datetime :last_request_at
|
41
|
+
t.datetime :last_login_at
|
42
|
+
t.datetime :current_login_at
|
43
|
+
t.string :last_login_ip
|
44
|
+
t.string :current_login_ip
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
data/test/libs/user.rb
ADDED
data/test/test_helper.rb
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "ruby-debug"
|
3
|
+
gem 'test-unit'
|
4
|
+
require "test/unit"
|
5
|
+
require 'active_support'
|
6
|
+
require 'active_support/test_case'
|
7
|
+
require "active_record"
|
8
|
+
require "active_record/fixtures"
|
9
|
+
require 'action_controller'
|
10
|
+
require 'shoulda'
|
11
|
+
require 'mocha'
|
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
|
+
AuthlogicConnect.config = {
|
31
|
+
:default => "twitter",
|
32
|
+
:connect => {
|
33
|
+
:twitter => {
|
34
|
+
:key => "my_key",
|
35
|
+
:secret => "my_secret",
|
36
|
+
:headers => {
|
37
|
+
"User-Agent" => "Safari",
|
38
|
+
"MyApp-Version" => "1.2"
|
39
|
+
},
|
40
|
+
:api_version => 1
|
41
|
+
},
|
42
|
+
:facebook => {
|
43
|
+
:key => "my_key",
|
44
|
+
:secret => "my_secret"
|
45
|
+
},
|
46
|
+
:foursquare => {
|
47
|
+
:key => "my_key",
|
48
|
+
:secret => "my_secret"
|
49
|
+
},
|
50
|
+
:google => {
|
51
|
+
:key => "my_key",
|
52
|
+
:secret => "my_secret"
|
53
|
+
},
|
54
|
+
:yahoo => {
|
55
|
+
:key => "my_key",
|
56
|
+
:secret => "my_secret"
|
57
|
+
},
|
58
|
+
:vimeo => {
|
59
|
+
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
# want to add a "method" property!
|
65
|
+
Authlogic::TestCase::MockRequest.class_eval do
|
66
|
+
def method
|
67
|
+
"POST"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
module ControllerHelpers
|
72
|
+
def controller_name
|
73
|
+
"users"
|
74
|
+
end
|
75
|
+
|
76
|
+
def action_name
|
77
|
+
"create"
|
78
|
+
end
|
79
|
+
|
80
|
+
def url_for(options = {})
|
81
|
+
p = []
|
82
|
+
options.each do |k,v|
|
83
|
+
p << "#{k}=#{v}"
|
84
|
+
end
|
85
|
+
p = "?#{p.join("&")}"
|
86
|
+
url = "http://localhost:3000/users#{p}"
|
87
|
+
end
|
88
|
+
|
89
|
+
def session=(value)
|
90
|
+
@session = value
|
91
|
+
end
|
92
|
+
end
|
93
|
+
Authlogic::ControllerAdapters::AbstractAdapter.send(:include, ControllerHelpers)
|
94
|
+
|
95
|
+
Authlogic::CryptoProviders::AES256.key = "myafdsfddddddddddddddddddddddddddddddddddddddddddddddd"
|
96
|
+
|
97
|
+
class ActiveSupport::TestCase
|
98
|
+
include ActiveRecord::TestFixtures
|
99
|
+
self.fixture_path = File.dirname(__FILE__) + "/fixtures"
|
100
|
+
self.use_transactional_fixtures = false
|
101
|
+
self.use_instantiated_fixtures = false
|
102
|
+
self.pre_loaded_fixtures = false
|
103
|
+
fixtures :all
|
104
|
+
setup :activate_authlogic
|
105
|
+
|
106
|
+
def create_token
|
107
|
+
token = OAuth::RequestToken.new("twitter", "key", "secret")
|
108
|
+
token.params = {
|
109
|
+
:oauth_callback_confirmed => "true",
|
110
|
+
:oauth_token_secret => "secret",
|
111
|
+
:oauth_token => "key"
|
112
|
+
}
|
113
|
+
token.consumer = OAuth::Consumer.new("key", "secret",
|
114
|
+
:site => "http://twitter.com",
|
115
|
+
:proxy => nil,
|
116
|
+
:oauth_version => "1.0",
|
117
|
+
:request_token_path => "/oauth/request_token",
|
118
|
+
:authorize_path => "/oauth/authorize",
|
119
|
+
:scheme => :header,
|
120
|
+
:signature_method => "HMAC-SHA1",
|
121
|
+
:authorize_url => "http://twitter.com/oauth/authenticate",
|
122
|
+
:access_token_path => "/oauth/access_token"
|
123
|
+
)
|
124
|
+
token
|
125
|
+
end
|
126
|
+
|
127
|
+
private
|
128
|
+
def password_for(user)
|
129
|
+
case user
|
130
|
+
when users(:ben)
|
131
|
+
"benrocks"
|
132
|
+
when users(:zack)
|
133
|
+
"zackrocks"
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def http_basic_auth_for(user = nil, &block)
|
138
|
+
unless user.blank?
|
139
|
+
controller.http_user = user.login
|
140
|
+
controller.http_password = password_for(user)
|
141
|
+
end
|
142
|
+
yield
|
143
|
+
controller.http_user = controller.http_password = nil
|
144
|
+
end
|
145
|
+
|
146
|
+
def set_cookie_for(user, id = nil)
|
147
|
+
controller.cookies["user_credentials"] = {:value => user.persistence_token, :expires => nil}
|
148
|
+
end
|
149
|
+
|
150
|
+
def unset_cookie
|
151
|
+
controller.cookies["user_credentials"] = nil
|
152
|
+
end
|
153
|
+
|
154
|
+
def set_params_for(user, id = nil)
|
155
|
+
controller.params["user_credentials"] = user.single_access_token
|
156
|
+
end
|
157
|
+
|
158
|
+
def unset_params
|
159
|
+
controller.params["user_credentials"] = nil
|
160
|
+
end
|
161
|
+
|
162
|
+
def set_request_content_type(type)
|
163
|
+
controller.request_content_type = type
|
164
|
+
end
|
165
|
+
|
166
|
+
def unset_request_content_type
|
167
|
+
controller.request_content_type = nil
|
168
|
+
end
|
169
|
+
|
170
|
+
def set_session_for(user, id = nil)
|
171
|
+
controller.session["user_credentials"] = user.persistence_token
|
172
|
+
controller.session["user_credentials_id"] = user.id
|
173
|
+
end
|
174
|
+
|
175
|
+
def unset_session
|
176
|
+
controller.session["user_credentials"] = controller.session["user_credentials_id"] = nil
|
177
|
+
end
|
178
|
+
end
|
data/test/test_oauth.rb
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
module AuthlogicConnect
|
4
|
+
class OauthTest < ActiveSupport::TestCase
|
5
|
+
context "Oauth (with TwitterToken)" do
|
6
|
+
setup do
|
7
|
+
@user = User.new(:login => "viatropos")
|
8
|
+
controller.params.merge!(:authentication_type => "user")
|
9
|
+
Authlogic::Session::Base.controller = controller
|
10
|
+
|
11
|
+
# this is the only thing the controller passes through for oauth
|
12
|
+
@user.auth_controller.params.merge!(:oauth_provider => "twitter")
|
13
|
+
|
14
|
+
# mock token
|
15
|
+
@token = create_token
|
16
|
+
|
17
|
+
@session_vars = [
|
18
|
+
:authentication_type,
|
19
|
+
:auth_request_class,
|
20
|
+
:oauth_provider,
|
21
|
+
:auth_callback_method
|
22
|
+
]
|
23
|
+
end
|
24
|
+
|
25
|
+
context "REQUEST (with TwitterToken)" do
|
26
|
+
|
27
|
+
should "have an 'oauth_provider'" do
|
28
|
+
assert_equal "twitter", @user.auth_params[:oauth_provider]
|
29
|
+
assert_equal true, @user.oauth_provider?
|
30
|
+
# session hasn't started yet
|
31
|
+
assert_equal false, @user.auth_session?
|
32
|
+
end
|
33
|
+
|
34
|
+
should "be an 'oauth_request'" do
|
35
|
+
assert_equal true, @user.oauth_request?
|
36
|
+
# oauth_request? == (auth_params? && oauth_provider?)
|
37
|
+
assert_equal true, @user.auth_params?
|
38
|
+
assert_equal true, @user.oauth_provider?
|
39
|
+
end
|
40
|
+
|
41
|
+
should "not be an 'oauth_response'" do
|
42
|
+
assert_equal false, @user.oauth_response?
|
43
|
+
# oauth_response? == (!oauth_response.nil? && auth_session? && auth_session[:auth_request_class] == self.class.name && auth_session[:auth_method] == "oauth")
|
44
|
+
assert_equal false, !@user.oauth_response.nil?
|
45
|
+
assert_equal false, @user.auth_session?
|
46
|
+
assert_equal false, @user.stored_oauth_token_and_secret?
|
47
|
+
end
|
48
|
+
|
49
|
+
should "be using oauth" do
|
50
|
+
# all of the above too!
|
51
|
+
assert @user.using_oauth?
|
52
|
+
end
|
53
|
+
|
54
|
+
should "start authentication" do
|
55
|
+
assert_equal true, @user.start_authentication?
|
56
|
+
# start_authentication? == (start_oauth? || start_openid?)
|
57
|
+
assert_equal true, @user.start_oauth?
|
58
|
+
# start_oauth == (authenticating_with_oauth? && !oauth_complete?)
|
59
|
+
assert_equal true, @user.authenticating_with_oauth?
|
60
|
+
# authenticating_with_oauth? == (correct_request_class? && using_oauth?)
|
61
|
+
assert_equal true, @user.correct_request_class?
|
62
|
+
assert_equal true, @user.using_oauth?
|
63
|
+
assert_equal true, !@user.oauth_complete?
|
64
|
+
end
|
65
|
+
|
66
|
+
should "not be using openid" do
|
67
|
+
assert_equal false, @user.start_openid?
|
68
|
+
assert_equal false, @user.using_openid?
|
69
|
+
assert_equal false, @user.openid_request?
|
70
|
+
# openid_request? == (!openid_identifier.blank? && auth_session[:auth_attributes].nil?)
|
71
|
+
assert_equal false, @user.openid_response?
|
72
|
+
# openid_response? == (auth_controller? && !auth_session[:auth_attributes].nil? && auth_session[:auth_method] == "openid")
|
73
|
+
end
|
74
|
+
|
75
|
+
should "have the correct class (authentication_type == user)" do
|
76
|
+
assert_equal "user", @user.auth_params[:authentication_type]
|
77
|
+
assert @user.correct_request_class?
|
78
|
+
end
|
79
|
+
|
80
|
+
should "realize we are authenticating_with_oauth?" do
|
81
|
+
assert_equal true, @user.authenticating_with_oauth?
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context "SAVE" do
|
86
|
+
setup do
|
87
|
+
@user.save
|
88
|
+
request_token = {:token => "a_token", :secret => "a_secret"}
|
89
|
+
# mock out like we've saved the data just before the first redirect
|
90
|
+
@user.save_oauth_session
|
91
|
+
@user.auth_session[:oauth_request_token] = request_token[:token]
|
92
|
+
@user.auth_session[:oauth_request_token_secret] = request_token[:secret]
|
93
|
+
end
|
94
|
+
|
95
|
+
should "save without a block" do
|
96
|
+
assert_equal true, @user.authenticating_with_oauth?
|
97
|
+
assert_equal true, @user.valid?
|
98
|
+
end
|
99
|
+
|
100
|
+
should "still be an oauth request" do
|
101
|
+
assert_equal true, @user.oauth_request?
|
102
|
+
end
|
103
|
+
|
104
|
+
context "RESPONSE (with TwitterToken)" do
|
105
|
+
setup do
|
106
|
+
@key_and_secret = {:key => "a_key", :secret => "a_secret", :token => "a_token"}
|
107
|
+
@user.auth_controller.params.merge!(:oauth_token => @key_and_secret[:token])
|
108
|
+
TwitterToken.stubs(:get_token_and_secret).returns(@key_and_secret)
|
109
|
+
end
|
110
|
+
|
111
|
+
should "have TwitterToken" do
|
112
|
+
assert_equal TwitterToken, @user.token_class
|
113
|
+
assert 1.0, @user.token_class.oauth_version
|
114
|
+
end
|
115
|
+
|
116
|
+
should "have oauth token" do
|
117
|
+
assert @user.auth_params
|
118
|
+
assert_equal true, @user.auth_params?
|
119
|
+
assert_equal "a_token", @user.oauth_token
|
120
|
+
end
|
121
|
+
|
122
|
+
should "not be an 'oauth_request'" do
|
123
|
+
assert_equal true, @user.auth_params?
|
124
|
+
assert_equal true, @user.oauth_provider?
|
125
|
+
assert_equal false, @user.oauth_response.blank?
|
126
|
+
#assert_equal false, @user.oauth_request?
|
127
|
+
# need a better way of checking this!
|
128
|
+
end
|
129
|
+
|
130
|
+
should "be an 'oauth_response'" do
|
131
|
+
assert_equal true, !@user.oauth_response.nil?
|
132
|
+
assert_equal true, @user.auth_session?
|
133
|
+
assert_equal true, (@user.auth_session[:auth_request_class] == @user.class.name)
|
134
|
+
assert_equal true, (@user.auth_session[:auth_method] == "oauth")
|
135
|
+
assert_equal true, @user.oauth_response?
|
136
|
+
end
|
137
|
+
|
138
|
+
should "be using oauth" do
|
139
|
+
assert_equal true, @user.using_oauth?
|
140
|
+
end
|
141
|
+
|
142
|
+
should "not be using openid" do
|
143
|
+
assert_equal false, @user.using_openid?
|
144
|
+
end
|
145
|
+
|
146
|
+
should "not be an 'openid_request'" do
|
147
|
+
assert_equal false, @user.using_openid?
|
148
|
+
end
|
149
|
+
|
150
|
+
should "not be an 'openid_response" do
|
151
|
+
assert_equal false, @user.using_openid?
|
152
|
+
end
|
153
|
+
|
154
|
+
teardown do
|
155
|
+
#TwitterToken.unstub(:get_token_and_secret)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
end
|
161
|
+
|
162
|
+
context "tokens" do
|
163
|
+
setup do
|
164
|
+
@token = TwitterToken.new
|
165
|
+
end
|
166
|
+
|
167
|
+
should "be version 1 since it's twitter" do
|
168
|
+
assert_equal 1.0, @token.oauth_version
|
169
|
+
end
|
170
|
+
|
171
|
+
should "return a new consumer with each call" do
|
172
|
+
first_consumer = @token.consumer
|
173
|
+
second_consumer = @token.consumer
|
174
|
+
assert_not_equal first_consumer, second_consumer
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
data/test/test_openid.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
module AuthlogicConnect
|
4
|
+
class OpenIdTest < Test::Unit::TestCase
|
5
|
+
context "OpenId" do
|
6
|
+
setup do
|
7
|
+
@user = User.new(:login => "viatropos")
|
8
|
+
controller.params.merge!(:authentication_type => "user")
|
9
|
+
Authlogic::Session::Base.controller = controller
|
10
|
+
@user.auth_controller.params.merge!(:openid_identifier => "viatropos.myopenid.com")
|
11
|
+
@session_vars = [
|
12
|
+
:authentication_type,
|
13
|
+
:auth_request_class,
|
14
|
+
:openid_identifier,
|
15
|
+
:auth_callback_method
|
16
|
+
]
|
17
|
+
end
|
18
|
+
|
19
|
+
should "have an 'openid_identifier'" do
|
20
|
+
assert_equal true, @user.openid_identifier?
|
21
|
+
end
|
22
|
+
|
23
|
+
should "be an 'openid_request'" do
|
24
|
+
assert @user.openid_request?
|
25
|
+
end
|
26
|
+
|
27
|
+
should "not be an 'openid_response'" do
|
28
|
+
assert_equal false, @user.openid_response?
|
29
|
+
end
|
30
|
+
|
31
|
+
should "be using openid" do
|
32
|
+
assert @user.using_openid?
|
33
|
+
end
|
34
|
+
|
35
|
+
should "not be using oauth" do
|
36
|
+
assert_equal false, @user.using_oauth?
|
37
|
+
end
|
38
|
+
|
39
|
+
should "have the correct class (authentication_type == user)" do
|
40
|
+
assert @user.correct_request_class?
|
41
|
+
end
|
42
|
+
|
43
|
+
should "realize we are authenticating_with_openid?" do
|
44
|
+
assert @user.authenticating_with_openid?
|
45
|
+
end
|
46
|
+
|
47
|
+
context "and 'save_with_openid', manually checking each step" do
|
48
|
+
|
49
|
+
setup do
|
50
|
+
# mock save
|
51
|
+
# this, and the whole redirect process happens
|
52
|
+
# but we'll just assume we saved the session data and got the redirect back
|
53
|
+
@user.save_openid_session
|
54
|
+
@user.save(:skip_redirect => true, :keep_session => true) do
|
55
|
+
"I'm the block you want"
|
56
|
+
end
|
57
|
+
# copy to test controller
|
58
|
+
@user.auth_session.each do |key, value|
|
59
|
+
@user.auth_controller.session[key] = value
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
teardown do
|
64
|
+
@user.destroy
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|