sorcery 0.4.1 → 0.5.2
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/Gemfile +5 -2
- data/Gemfile.lock +12 -0
- data/README.rdoc +12 -7
- data/VERSION +1 -1
- data/lib/sorcery/controller/adapters/sinatra.rb +35 -28
- data/lib/sorcery/controller/submodules/brute_force_protection.rb +1 -1
- data/lib/sorcery/controller/submodules/http_basic_auth.rb +1 -1
- data/lib/sorcery/crypto_providers/bcrypt.rb +2 -4
- data/lib/sorcery/crypto_providers/common.rb +2 -2
- data/lib/sorcery/engine.rb +0 -11
- data/lib/sorcery/initializers/initializer.rb +5 -1
- data/lib/sorcery/model/adapters/active_record.rb +28 -0
- data/lib/sorcery/model/adapters/mongoid.rb +64 -0
- data/lib/sorcery/model/submodules/activity_logging.rb +12 -3
- data/lib/sorcery/model/submodules/brute_force_protection.rb +6 -1
- data/lib/sorcery/model/submodules/external.rb +1 -0
- data/lib/sorcery/model/submodules/remember_me.rb +15 -1
- data/lib/sorcery/model/submodules/reset_password.rb +14 -5
- data/lib/sorcery/model/submodules/user_activation.rb +11 -1
- data/lib/sorcery/model/temporary_token.rb +1 -1
- data/lib/sorcery/model.rb +37 -10
- data/lib/sorcery/sinatra.rb +0 -1
- data/lib/sorcery/test_helpers/internal/rails.rb +53 -0
- data/lib/sorcery/test_helpers/internal/sinatra.rb +74 -0
- data/lib/sorcery/test_helpers/internal/sinatra_modular.rb +74 -0
- data/lib/sorcery/test_helpers/internal.rb +56 -0
- data/lib/sorcery/test_helpers/rails.rb +5 -43
- data/lib/sorcery/test_helpers/sinatra.rb +62 -102
- data/lib/sorcery/test_helpers.rb +0 -47
- data/lib/sorcery.rb +27 -1
- data/sorcery.gemspec +162 -10
- data/spec/Gemfile +1 -1
- data/spec/Gemfile.lock +9 -4
- data/spec/rails3/Gemfile +1 -1
- data/spec/rails3/Gemfile.lock +6 -9
- data/spec/rails3/spec/controller_activity_logging_spec.rb +2 -4
- data/spec/rails3/spec/controller_oauth_spec.rb +3 -3
- data/spec/rails3/spec/controller_session_timeout_spec.rb +20 -19
- data/spec/rails3/spec/controller_spec.rb +6 -16
- data/spec/rails3/spec/spec_helper.rb +2 -2
- data/spec/rails3/spec/user_activation_spec.rb +2 -12
- data/spec/rails3/spec/user_brute_force_protection_spec.rb +2 -30
- data/spec/rails3/spec/user_remember_me_spec.rb +3 -11
- data/spec/rails3/spec/user_reset_password_spec.rb +15 -16
- data/spec/rails3/spec/user_spec.rb +18 -13
- data/spec/rails3_mongoid/.gitignore +4 -0
- data/spec/rails3_mongoid/.rspec +1 -0
- data/spec/rails3_mongoid/Gemfile +14 -0
- data/spec/rails3_mongoid/Gemfile.lock +143 -0
- data/spec/rails3_mongoid/Rakefile +11 -0
- data/spec/rails3_mongoid/app/controllers/application_controller.rb +108 -0
- data/spec/rails3_mongoid/app/helpers/application_helper.rb +2 -0
- data/spec/rails3_mongoid/app/mailers/sorcery_mailer.rb +25 -0
- data/spec/rails3_mongoid/app/models/authentication.rb +7 -0
- data/spec/rails3_mongoid/app/models/user.rb +5 -0
- data/spec/rails3_mongoid/app/views/layouts/application.html.erb +14 -0
- data/spec/rails3_mongoid/app/views/sorcery_mailer/activation_email.html.erb +17 -0
- data/spec/rails3_mongoid/app/views/sorcery_mailer/activation_email.text.erb +9 -0
- data/spec/rails3_mongoid/app/views/sorcery_mailer/activation_success_email.html.erb +17 -0
- data/spec/rails3_mongoid/app/views/sorcery_mailer/activation_success_email.text.erb +9 -0
- data/spec/rails3_mongoid/app/views/sorcery_mailer/reset_password_email.html.erb +16 -0
- data/spec/rails3_mongoid/app/views/sorcery_mailer/reset_password_email.text.erb +8 -0
- data/spec/rails3_mongoid/config/application.rb +51 -0
- data/spec/rails3_mongoid/config/boot.rb +13 -0
- data/spec/rails3_mongoid/config/environment.rb +5 -0
- data/spec/rails3_mongoid/config/environments/development.rb +26 -0
- data/spec/rails3_mongoid/config/environments/in_memory.rb +0 -0
- data/spec/rails3_mongoid/config/environments/production.rb +49 -0
- data/spec/rails3_mongoid/config/environments/test.rb +35 -0
- data/spec/rails3_mongoid/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/rails3_mongoid/config/initializers/inflections.rb +10 -0
- data/spec/rails3_mongoid/config/initializers/mime_types.rb +5 -0
- data/spec/rails3_mongoid/config/initializers/secret_token.rb +7 -0
- data/spec/rails3_mongoid/config/initializers/session_store.rb +8 -0
- data/spec/rails3_mongoid/config/locales/en.yml +5 -0
- data/spec/rails3_mongoid/config/mongoid.yml +7 -0
- data/spec/rails3_mongoid/config/routes.rb +59 -0
- data/spec/rails3_mongoid/config.ru +4 -0
- data/spec/rails3_mongoid/db/schema.rb +23 -0
- data/spec/rails3_mongoid/db/seeds.rb +7 -0
- data/spec/rails3_mongoid/lib/tasks/.gitkeep +0 -0
- data/spec/rails3_mongoid/public/404.html +26 -0
- data/spec/rails3_mongoid/public/422.html +26 -0
- data/spec/rails3_mongoid/public/500.html +26 -0
- data/spec/rails3_mongoid/public/favicon.ico +0 -0
- data/spec/rails3_mongoid/public/images/rails.png +0 -0
- data/spec/rails3_mongoid/public/javascripts/application.js +2 -0
- data/spec/rails3_mongoid/public/javascripts/controls.js +965 -0
- data/spec/rails3_mongoid/public/javascripts/dragdrop.js +974 -0
- data/spec/rails3_mongoid/public/javascripts/effects.js +1123 -0
- data/spec/rails3_mongoid/public/javascripts/prototype.js +6001 -0
- data/spec/rails3_mongoid/public/javascripts/rails.js +175 -0
- data/spec/rails3_mongoid/public/robots.txt +5 -0
- data/spec/rails3_mongoid/public/stylesheets/.gitkeep +0 -0
- data/spec/rails3_mongoid/script/rails +6 -0
- data/spec/rails3_mongoid/spec/spec.opts +2 -0
- data/spec/rails3_mongoid/spec/spec_helper.orig.rb +27 -0
- data/spec/rails3_mongoid/spec/spec_helper.rb +55 -0
- data/spec/rails3_mongoid/spec/user_activation_spec.rb +178 -0
- data/spec/rails3_mongoid/spec/user_activity_logging_spec.rb +31 -0
- data/spec/rails3_mongoid/spec/user_brute_force_protection_spec.rb +41 -0
- data/spec/rails3_mongoid/spec/user_oauth_spec.rb +34 -0
- data/spec/rails3_mongoid/spec/user_remember_me_spec.rb +51 -0
- data/spec/rails3_mongoid/spec/user_reset_password_spec.rb +182 -0
- data/spec/rails3_mongoid/spec/user_spec.rb +319 -0
- data/spec/rails3_mongoid/vendor/plugins/.gitkeep +0 -0
- data/spec/sinatra/Gemfile +4 -1
- data/spec/sinatra/Gemfile.lock +22 -39
- data/spec/sinatra/Rakefile +1 -1
- data/spec/sinatra/filters.rb +20 -14
- data/spec/sinatra/modular.rb +157 -0
- data/spec/sinatra/spec/controller_session_timeout_spec.rb +23 -22
- data/spec/sinatra/spec/spec_helper.rb +2 -2
- data/spec/sinatra_modular/Gemfile +15 -0
- data/spec/sinatra_modular/Gemfile.lock +117 -0
- data/spec/sinatra_modular/Rakefile +11 -0
- data/spec/sinatra_modular/authentication.rb +3 -0
- data/spec/sinatra_modular/db/migrate/activation/20101224223622_add_activation_to_users.rb +17 -0
- data/spec/sinatra_modular/db/migrate/activity_logging/20101224223624_add_activity_logging_to_users.rb +17 -0
- data/spec/sinatra_modular/db/migrate/brute_force_protection/20101224223626_add_brute_force_protection_to_users.rb +11 -0
- data/spec/sinatra_modular/db/migrate/core/20101224223620_create_users.rb +16 -0
- data/spec/sinatra_modular/db/migrate/external/20101224223628_create_authentications.rb +14 -0
- data/spec/sinatra_modular/db/migrate/remember_me/20101224223623_add_remember_me_token_to_users.rb +15 -0
- data/spec/sinatra_modular/db/migrate/reset_password/20101224223622_add_reset_password_to_users.rb +13 -0
- data/spec/sinatra_modular/filters.rb +27 -0
- data/spec/sinatra_modular/modular.rb +157 -0
- data/spec/sinatra_modular/myapp.rb +133 -0
- data/spec/sinatra_modular/sorcery_mailer.rb +25 -0
- data/spec/sinatra_modular/spec_modular/controller_activity_logging_spec.rb +85 -0
- data/spec/sinatra_modular/spec_modular/controller_brute_force_protection_spec.rb +70 -0
- data/spec/sinatra_modular/spec_modular/controller_http_basic_auth_spec.rb +53 -0
- data/spec/sinatra_modular/spec_modular/controller_oauth2_spec.rb +120 -0
- data/spec/sinatra_modular/spec_modular/controller_oauth_spec.rb +121 -0
- data/spec/sinatra_modular/spec_modular/controller_remember_me_spec.rb +64 -0
- data/spec/sinatra_modular/spec_modular/controller_session_timeout_spec.rb +57 -0
- data/spec/sinatra_modular/spec_modular/controller_spec.rb +116 -0
- data/spec/sinatra_modular/spec_modular/spec.opts +2 -0
- data/spec/sinatra_modular/spec_modular/spec_helper.rb +51 -0
- data/spec/sinatra_modular/user.rb +6 -0
- data/spec/sinatra_modular/views/test_login.erb +4 -0
- metadata +205 -37
- data/spec/untitled folder +0 -18
data/lib/sorcery/model.rb
CHANGED
|
@@ -14,27 +14,38 @@ module Sorcery
|
|
|
14
14
|
self.class_eval do
|
|
15
15
|
extend ClassMethods # included here, before submodules, so they can be overriden by them.
|
|
16
16
|
include InstanceMethods
|
|
17
|
-
|
|
18
|
-
# set the user_class for the controller methods which call it
|
|
19
|
-
::Sorcery::Controller::Config.user_class = self
|
|
17
|
+
|
|
20
18
|
@sorcery_config.submodules = ::Sorcery::Controller::Config.submodules
|
|
21
19
|
@sorcery_config.submodules.each do |mod|
|
|
22
20
|
begin
|
|
23
21
|
include Submodules.const_get(mod.to_s.split("_").map {|p| p.capitalize}.join(""))
|
|
24
22
|
rescue NameError
|
|
25
|
-
# don't stop on a missing submodule.
|
|
23
|
+
# don't stop on a missing submodule. Needed because some submodules are only defined in the controller side.
|
|
26
24
|
end
|
|
27
25
|
end
|
|
28
26
|
end
|
|
29
|
-
|
|
27
|
+
|
|
28
|
+
# This runs the options block set in the initializer on the model class.
|
|
30
29
|
::Sorcery::Controller::Config.user_config.tap{|blk| blk.call(@sorcery_config) if blk}
|
|
31
|
-
|
|
30
|
+
|
|
31
|
+
# Mongoid support
|
|
32
|
+
self.class_eval do
|
|
33
|
+
field sorcery_config.username_attribute_name, type: String
|
|
34
|
+
#field sorcery_config.password_attribute_name, type: String
|
|
35
|
+
field sorcery_config.email_attribute_name, type: String unless sorcery_config.username_attribute_name == sorcery_config.email_attribute_name
|
|
36
|
+
field sorcery_config.crypted_password_attribute_name, type: String
|
|
37
|
+
field sorcery_config.salt_attribute_name, type: String
|
|
38
|
+
end if defined?(Mongoid) and self.ancestors.include?(Mongoid::Document)
|
|
39
|
+
|
|
40
|
+
# add virtual password accessor and ORM callbacks
|
|
32
41
|
self.class_eval do
|
|
33
42
|
attr_accessor @sorcery_config.password_attribute_name
|
|
34
43
|
attr_protected @sorcery_config.crypted_password_attribute_name, @sorcery_config.salt_attribute_name
|
|
35
44
|
before_save :encrypt_password, :if => Proc.new { |record| record.send(sorcery_config.password_attribute_name).present? }
|
|
36
45
|
after_save :clear_virtual_password, :if => Proc.new { |record| record.send(sorcery_config.password_attribute_name).present? }
|
|
37
46
|
end
|
|
47
|
+
|
|
48
|
+
@sorcery_config.after_config << :add_config_inheritance if @sorcery_config.subclasses_inherit_config
|
|
38
49
|
@sorcery_config.after_config.each { |c| send(c) }
|
|
39
50
|
end
|
|
40
51
|
end
|
|
@@ -53,7 +64,7 @@ module Sorcery
|
|
|
53
64
|
# returns the user if success, nil otherwise.
|
|
54
65
|
def authenticate(*credentials)
|
|
55
66
|
raise ArgumentError, "at least 2 arguments required" if credentials.size < 2
|
|
56
|
-
user =
|
|
67
|
+
user = find_by_credentials(credentials)
|
|
57
68
|
_salt = user.send(@sorcery_config.salt_attribute_name) if user && !@sorcery_config.salt_attribute_name.nil? && !@sorcery_config.encryption_provider.nil?
|
|
58
69
|
user if user && @sorcery_config.before_authenticate.all? {|c| user.send(c)} && credentials_match?(user.send(@sorcery_config.crypted_password_attribute_name),credentials[1],_salt)
|
|
59
70
|
end
|
|
@@ -76,6 +87,20 @@ module Sorcery
|
|
|
76
87
|
@sorcery_config.encryption_provider.matches?(crypted, *tokens)
|
|
77
88
|
end
|
|
78
89
|
|
|
90
|
+
def add_config_inheritance
|
|
91
|
+
self.class_eval do
|
|
92
|
+
def self.inherited(subclass)
|
|
93
|
+
subclass.class_eval do
|
|
94
|
+
class << self
|
|
95
|
+
attr_accessor :sorcery_config
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
subclass.sorcery_config = sorcery_config
|
|
99
|
+
super
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
79
104
|
end
|
|
80
105
|
|
|
81
106
|
module InstanceMethods
|
|
@@ -93,10 +118,10 @@ module Sorcery
|
|
|
93
118
|
protected
|
|
94
119
|
|
|
95
120
|
# creates new salt and saves it.
|
|
96
|
-
# encrypts password with salt and
|
|
121
|
+
# encrypts password with salt and saves it.
|
|
97
122
|
def encrypt_password
|
|
98
123
|
config = sorcery_config
|
|
99
|
-
|
|
124
|
+
self.send(:"#{config.salt_attribute_name}=", new_salt = generate_random_token) if !config.salt_attribute_name.nil?
|
|
100
125
|
self.send(:"#{config.crypted_password_attribute_name}=", self.class.encrypt(self.send(config.password_attribute_name),new_salt))
|
|
101
126
|
end
|
|
102
127
|
|
|
@@ -117,7 +142,7 @@ module Sorcery
|
|
|
117
142
|
|
|
118
143
|
# Random code, used for salt and temporary tokens.
|
|
119
144
|
def generate_random_token
|
|
120
|
-
|
|
145
|
+
Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
|
|
121
146
|
end
|
|
122
147
|
end
|
|
123
148
|
|
|
@@ -133,6 +158,7 @@ module Sorcery
|
|
|
133
158
|
:salt_attribute_name, # change default salt attribute.
|
|
134
159
|
:stretches, # how many times to apply encryption to the password.
|
|
135
160
|
:encryption_key, # encryption key used to encrypt reversible encryptions such as AES256.
|
|
161
|
+
:subclasses_inherit_config, # make this configuration inheritable for subclasses. Useful for ActiveRecord's STI.
|
|
136
162
|
|
|
137
163
|
:submodules, # configured in config/application.rb
|
|
138
164
|
:before_authenticate, # an array of method names to call before authentication completes. used internally.
|
|
@@ -156,6 +182,7 @@ module Sorcery
|
|
|
156
182
|
:@salt_join_token => "",
|
|
157
183
|
:@salt_attribute_name => :salt,
|
|
158
184
|
:@stretches => nil,
|
|
185
|
+
:@subclasses_inherit_config => false,
|
|
159
186
|
:@before_authenticate => [],
|
|
160
187
|
:@after_config => []
|
|
161
188
|
}
|
data/lib/sorcery/sinatra.rb
CHANGED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
module Sorcery
|
|
2
|
+
module TestHelpers
|
|
3
|
+
module Internal
|
|
4
|
+
module Rails
|
|
5
|
+
include ::Sorcery::TestHelpers::Rails
|
|
6
|
+
|
|
7
|
+
SUBMODUELS_AUTO_ADDED_CONTROLLER_FILTERS = [:register_last_activity_time_to_db, :deny_banned_user, :validate_session]
|
|
8
|
+
|
|
9
|
+
def sorcery_reload!(submodules = [], options = {})
|
|
10
|
+
reload_user_class
|
|
11
|
+
|
|
12
|
+
# return to no-module configuration
|
|
13
|
+
::Sorcery::Controller::Config.init!
|
|
14
|
+
::Sorcery::Controller::Config.reset!
|
|
15
|
+
|
|
16
|
+
# remove all plugin before_filters so they won't fail other tests.
|
|
17
|
+
# I don't like this way, but I didn't find another.
|
|
18
|
+
# hopefully it won't break until Rails 4.
|
|
19
|
+
ApplicationController._process_action_callbacks.delete_if {|c| SUBMODUELS_AUTO_ADDED_CONTROLLER_FILTERS.include?(c.filter) }
|
|
20
|
+
|
|
21
|
+
# configure
|
|
22
|
+
::Sorcery::Controller::Config.submodules = submodules
|
|
23
|
+
::Sorcery::Controller::Config.user_class = nil
|
|
24
|
+
ActionController::Base.send(:include,::Sorcery::Controller)
|
|
25
|
+
::Sorcery::Controller::Config.user_class = User
|
|
26
|
+
|
|
27
|
+
::Sorcery::Controller::Config.user_config do |user|
|
|
28
|
+
options.each do |property,value|
|
|
29
|
+
user.send(:"#{property}=", value)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
User.authenticates_with_sorcery!
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def sorcery_controller_property_set(property, value)
|
|
36
|
+
::Sorcery::Controller::Config.send(:"#{property}=", value)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def sorcery_controller_external_property_set(provider, property, value)
|
|
40
|
+
::Sorcery::Controller::Config.send(provider).send(:"#{property}=", value)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# This helper is used to fake multiple users signing in in tests.
|
|
44
|
+
# It does so by clearing @current_user, thus allowing a new user to login,
|
|
45
|
+
# all this without calling the :logout action explicitly.
|
|
46
|
+
# A dirty dirty hack.
|
|
47
|
+
def clear_user_without_logout
|
|
48
|
+
subject.instance_variable_set(:@current_user,nil)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
module Sorcery
|
|
2
|
+
module TestHelpers
|
|
3
|
+
module Internal
|
|
4
|
+
module Sinatra
|
|
5
|
+
include ::Sorcery::TestHelpers::Sinatra::CookieSessionMethods
|
|
6
|
+
include ::Sorcery::TestHelpers::Sinatra::InstanceMethods
|
|
7
|
+
|
|
8
|
+
class ::Sinatra::Application
|
|
9
|
+
class << self
|
|
10
|
+
attr_accessor :sorcery_vars
|
|
11
|
+
end
|
|
12
|
+
@sorcery_vars = {}
|
|
13
|
+
|
|
14
|
+
# NOTE: see before and after test filters in filters.rb
|
|
15
|
+
|
|
16
|
+
def save_instance_vars
|
|
17
|
+
instance_variables.each do |var|
|
|
18
|
+
self.class.sorcery_vars[:"#{var.to_s.delete("@")}"] = instance_variable_get(var)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
::RSpec::Matchers.define :redirect_to do |expected|
|
|
24
|
+
match do |actual|
|
|
25
|
+
actual.status == 302 && actual.location == expected
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def clear_user_without_logout
|
|
30
|
+
get_sinatra_app(subject).instance_variable_set(:@current_user, nil)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def assigns
|
|
34
|
+
::Sinatra::Application.sorcery_vars
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def sorcery_reload!(submodules = [], options = {})
|
|
38
|
+
reload_user_class
|
|
39
|
+
|
|
40
|
+
# return to no-module configuration
|
|
41
|
+
::Sorcery::Controller::Config.init!
|
|
42
|
+
::Sorcery::Controller::Config.reset!
|
|
43
|
+
|
|
44
|
+
# clear all filters
|
|
45
|
+
::Sinatra::Application.instance_variable_set(:@filters, {:before => [], :after => []})
|
|
46
|
+
load File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec', 'sinatra', 'filters.rb')
|
|
47
|
+
::Sinatra::Application.send(:include, ::Filters)
|
|
48
|
+
|
|
49
|
+
# configure
|
|
50
|
+
::Sorcery::Controller::Config.submodules = submodules
|
|
51
|
+
::Sorcery::Controller::Config.user_class = nil
|
|
52
|
+
::Sinatra::Application.send(:include, Sorcery::Controller::Adapters::Sinatra)
|
|
53
|
+
::Sinatra::Application.send(:include, Sorcery::Controller)
|
|
54
|
+
::Sorcery::Controller::Config.user_class = User
|
|
55
|
+
|
|
56
|
+
::Sorcery::Controller::Config.user_config do |user|
|
|
57
|
+
options.each do |property, value|
|
|
58
|
+
user.send(:"#{property}=", value)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
User.authenticates_with_sorcery!
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def sorcery_controller_property_set(property, value)
|
|
65
|
+
::Sorcery::Controller::Config.send(:"#{property}=", value)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def sorcery_controller_external_property_set(provider, property, value)
|
|
69
|
+
::Sorcery::Controller::Config.send(provider).send(:"#{property}=", value)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
module Sorcery
|
|
2
|
+
module TestHelpers
|
|
3
|
+
module Internal
|
|
4
|
+
module SinatraModular
|
|
5
|
+
include ::Sorcery::TestHelpers::Sinatra::CookieSessionMethods
|
|
6
|
+
include ::Sorcery::TestHelpers::Sinatra::InstanceMethods
|
|
7
|
+
|
|
8
|
+
class ::Modular
|
|
9
|
+
class << self
|
|
10
|
+
attr_accessor :sorcery_vars
|
|
11
|
+
end
|
|
12
|
+
@sorcery_vars = {}
|
|
13
|
+
|
|
14
|
+
# NOTE: see before and after test filters in filters.rb
|
|
15
|
+
|
|
16
|
+
def save_instance_vars
|
|
17
|
+
instance_variables.each do |var|
|
|
18
|
+
self.class.sorcery_vars[:"#{var.to_s.delete("@")}"] = instance_variable_get(var)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
::RSpec::Matchers.define :redirect_to do |expected|
|
|
24
|
+
match do |actual|
|
|
25
|
+
actual.status == 302 && actual.location == expected
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def clear_user_without_logout
|
|
30
|
+
get_sinatra_app(subject).instance_variable_set(:@current_user, nil)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def assigns
|
|
34
|
+
::Modular.sorcery_vars
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def sorcery_reload!(submodules = [], options = {})
|
|
38
|
+
reload_user_class
|
|
39
|
+
|
|
40
|
+
# return to no-module configuration
|
|
41
|
+
::Sorcery::Controller::Config.init!
|
|
42
|
+
::Sorcery::Controller::Config.reset!
|
|
43
|
+
|
|
44
|
+
# clear all filters
|
|
45
|
+
::Sinatra::Base.instance_variable_set(:@filters, {:before => [], :after => []})
|
|
46
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec', 'sinatra', 'filters')
|
|
47
|
+
::Sinatra::Base.send(:include, ::Filters)
|
|
48
|
+
|
|
49
|
+
# configure
|
|
50
|
+
::Sorcery::Controller::Config.submodules = submodules
|
|
51
|
+
::Sorcery::Controller::Config.user_class = nil
|
|
52
|
+
::Sinatra::Base.send(:include, Sorcery::Controller::Adapters::Sinatra)
|
|
53
|
+
::Sinatra::Base.send(:include, Sorcery::Controller)
|
|
54
|
+
::Sorcery::Controller::Config.user_class = User
|
|
55
|
+
|
|
56
|
+
::Sorcery::Controller::Config.user_config do |user|
|
|
57
|
+
options.each do |property, value|
|
|
58
|
+
user.send(:"#{property}=", value)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
User.authenticates_with_sorcery!
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def sorcery_controller_property_set(property, value)
|
|
65
|
+
::Sorcery::Controller::Config.send(:"#{property}=", value)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def sorcery_controller_external_property_set(provider, property, value)
|
|
69
|
+
::Sorcery::Controller::Config.send(provider).send(:"#{property}=", value)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
module Sorcery
|
|
2
|
+
module TestHelpers
|
|
3
|
+
# Internal TestHelpers are used to test the gem, internally, and should not be used to test apps *using* sorcery.
|
|
4
|
+
# This file will be included in the spec_helper file.
|
|
5
|
+
module Internal
|
|
6
|
+
def self.included(base)
|
|
7
|
+
# reducing default cost for specs speed
|
|
8
|
+
CryptoProviders::BCrypt.class_eval do
|
|
9
|
+
class << self
|
|
10
|
+
def cost
|
|
11
|
+
1
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# a patch to fix a bug in testing that happens when you 'destroy' a session twice.
|
|
18
|
+
# After the first destroy, the session is an ordinary hash, and then when destroy is called again there's an exception.
|
|
19
|
+
class ::Hash
|
|
20
|
+
def destroy
|
|
21
|
+
clear
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def create_new_user(attributes_hash = nil)
|
|
26
|
+
user_attributes_hash = attributes_hash || {:username => 'gizmo', :email => "bla@bla.com", :password => 'secret'}
|
|
27
|
+
@user = User.new(user_attributes_hash)
|
|
28
|
+
@user.save!
|
|
29
|
+
@user
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def create_new_external_user(provider, attributes_hash = nil)
|
|
33
|
+
user_attributes_hash = attributes_hash || {:username => 'gizmo'}
|
|
34
|
+
@user = User.new(user_attributes_hash)
|
|
35
|
+
@user.save!
|
|
36
|
+
@user.authentications.create!({:provider => provider, :uid => 123})
|
|
37
|
+
@user
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def sorcery_model_property_set(property, *values)
|
|
41
|
+
User.class_eval do
|
|
42
|
+
sorcery_config.send(:"#{property}=", *values)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
# reload user class between specs
|
|
49
|
+
# so it will be possible to test the different submodules in isolation
|
|
50
|
+
def reload_user_class
|
|
51
|
+
Object.send(:remove_const,:User)
|
|
52
|
+
load 'user.rb'
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -1,54 +1,16 @@
|
|
|
1
1
|
module Sorcery
|
|
2
2
|
module TestHelpers
|
|
3
3
|
module Rails
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def sorcery_reload!(submodules = [], options = {})
|
|
7
|
-
reload_user_class
|
|
8
|
-
|
|
9
|
-
# return to no-module configuration
|
|
10
|
-
::Sorcery::Controller::Config.init!
|
|
11
|
-
::Sorcery::Controller::Config.reset!
|
|
12
|
-
|
|
13
|
-
# remove all plugin before_filters so they won't fail other tests.
|
|
14
|
-
# I don't like this way, but I didn't find another.
|
|
15
|
-
# hopefully it won't break until Rails 4.
|
|
16
|
-
ApplicationController._process_action_callbacks.delete_if {|c| SUBMODUELS_AUTO_ADDED_CONTROLLER_FILTERS.include?(c.filter) }
|
|
17
|
-
|
|
18
|
-
# configure
|
|
19
|
-
::Sorcery::Controller::Config.submodules = submodules
|
|
20
|
-
::Sorcery::Controller::Config.user_class = nil
|
|
21
|
-
ActionController::Base.send(:include,::Sorcery::Controller)
|
|
22
|
-
|
|
23
|
-
::Sorcery::Controller::Config.user_config do |user|
|
|
24
|
-
options.each do |property,value|
|
|
25
|
-
user.send(:"#{property}=", value)
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
User.authenticates_with_sorcery!
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def sorcery_controller_property_set(property, value)
|
|
32
|
-
::Sorcery::Controller::Config.send(:"#{property}=", value)
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
def sorcery_controller_external_property_set(provider, property, value)
|
|
36
|
-
::Sorcery::Controller::Config.send(provider).send(:"#{property}=", value)
|
|
37
|
-
end
|
|
38
|
-
|
|
4
|
+
# logins a user and calls all callbacks
|
|
39
5
|
def login_user(user = nil)
|
|
40
6
|
user ||= @user
|
|
41
|
-
|
|
42
|
-
|
|
7
|
+
@controller.send(:login_user,user)
|
|
8
|
+
@controller.send(:after_login!,user,[user.send(user.sorcery_config.username_attribute_name),'secret'])
|
|
43
9
|
end
|
|
44
10
|
|
|
45
11
|
def logout_user
|
|
46
|
-
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def clear_user_without_logout
|
|
50
|
-
subject.instance_variable_set(:@current_user,nil)
|
|
12
|
+
@controller.send(:logout)
|
|
51
13
|
end
|
|
52
14
|
end
|
|
53
15
|
end
|
|
54
|
-
end
|
|
16
|
+
end
|
|
@@ -1,127 +1,87 @@
|
|
|
1
1
|
module Sorcery
|
|
2
2
|
module TestHelpers
|
|
3
3
|
module Sinatra
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
def self.included(base)
|
|
5
|
+
base.send(:include, InstanceMethods)
|
|
6
|
+
base.send(:include, CookieSessionMethods)
|
|
7
|
+
::Sinatra::Base.class_eval do
|
|
8
|
+
class << self
|
|
9
|
+
attr_accessor :rack_test_session
|
|
9
10
|
end
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
class ::Sinatra::Application
|
|
14
|
-
class << self
|
|
15
|
-
attr_accessor :sorcery_vars
|
|
16
|
-
end
|
|
17
|
-
@sorcery_vars = {}
|
|
18
11
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
def self.inherited(subclass)
|
|
13
|
+
super
|
|
14
|
+
subclass.class_eval do
|
|
15
|
+
class << self
|
|
16
|
+
attr_accessor :rack_test_session
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
include CookieSessionMethods
|
|
22
|
+
|
|
23
|
+
def rack_test_session
|
|
24
|
+
self.class.rack_test_session
|
|
24
25
|
end
|
|
25
26
|
end
|
|
26
27
|
end
|
|
27
|
-
|
|
28
|
-
::RSpec::Matchers.define :redirect_to do |expected|
|
|
29
|
-
match do |actual|
|
|
30
|
-
actual.status == 302 && actual.location == expected
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def get_sinatra_app(app)
|
|
35
|
-
while app.class != ::Sinatra::Application do
|
|
36
|
-
app = app.instance_variable_get(:@app)
|
|
37
|
-
end
|
|
38
|
-
app
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def login_user(user=nil)
|
|
42
|
-
user ||= @user
|
|
43
|
-
get_sinatra_app(subject).send(:login_user,user)
|
|
44
|
-
get_sinatra_app(subject).send(:after_login!,user,[user.username,'secret'])
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
def logout_user
|
|
48
|
-
get_sinatra_app(subject).send(:logout)
|
|
49
|
-
end
|
|
50
28
|
|
|
51
|
-
|
|
52
|
-
get_sinatra_app(
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
def assigns
|
|
56
|
-
::Sinatra::Application.sorcery_vars
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
class SessionData
|
|
60
|
-
def initialize(cookies)
|
|
61
|
-
@cookies = cookies
|
|
62
|
-
@data = cookies['rack.session']
|
|
63
|
-
if @data
|
|
64
|
-
@data = @data.unpack("m*").first
|
|
65
|
-
@data = Marshal.load(@data)
|
|
66
|
-
else
|
|
67
|
-
@data = {}
|
|
29
|
+
module InstanceMethods
|
|
30
|
+
def get_sinatra_app(app)
|
|
31
|
+
while !app.kind_of? ::Sinatra::Base do
|
|
32
|
+
app = app.instance_variable_get(:@app)
|
|
68
33
|
end
|
|
34
|
+
app
|
|
69
35
|
end
|
|
70
36
|
|
|
71
|
-
def
|
|
72
|
-
@
|
|
37
|
+
def login_user(user=nil)
|
|
38
|
+
user ||= @user
|
|
39
|
+
get_sinatra_app(app).send(:login_user, user)
|
|
40
|
+
get_sinatra_app(app).send(:after_login!, user, [user.send(user.sorcery_config.username_attribute_name), 'secret'])
|
|
73
41
|
end
|
|
74
42
|
|
|
75
|
-
def
|
|
76
|
-
|
|
77
|
-
session_data = Marshal.dump(@data)
|
|
78
|
-
session_data = [session_data].pack("m*")
|
|
79
|
-
@cookies.merge("rack.session=#{Rack::Utils.escape(session_data)}", URI.parse("//example.org//"))
|
|
80
|
-
raise "session variable not set" unless @cookies['rack.session'] == session_data
|
|
43
|
+
def logout_user
|
|
44
|
+
get_sinatra_app(app).send(:logout)
|
|
81
45
|
end
|
|
82
46
|
end
|
|
83
47
|
|
|
84
|
-
|
|
85
|
-
SessionData
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
48
|
+
module CookieSessionMethods
|
|
49
|
+
class SessionData
|
|
50
|
+
def initialize(cookies)
|
|
51
|
+
@cookies = cookies
|
|
52
|
+
@data = cookies['rack.session']
|
|
53
|
+
if @data
|
|
54
|
+
@data = @data.unpack("m*").first
|
|
55
|
+
@data = Marshal.load(@data)
|
|
56
|
+
else
|
|
57
|
+
@data = {}
|
|
58
|
+
end
|
|
59
|
+
end
|
|
94
60
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
61
|
+
def [](key)
|
|
62
|
+
@data[key]
|
|
63
|
+
end
|
|
98
64
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
::Sinatra::Application.send(:include, Sorcery::Controller)
|
|
110
|
-
|
|
111
|
-
::Sorcery::Controller::Config.user_config do |user|
|
|
112
|
-
options.each do |property,value|
|
|
113
|
-
user.send(:"#{property}=", value)
|
|
65
|
+
def []=(key, value)
|
|
66
|
+
@data[key] = value
|
|
67
|
+
session_data = Marshal.dump(@data)
|
|
68
|
+
session_data = [session_data].pack("m*")
|
|
69
|
+
@cookies.merge("rack.session=#{Rack::Utils.escape(session_data)}", URI.parse("//example.org//"))
|
|
70
|
+
raise "session variable not set" unless @cookies['rack.session'] == session_data
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def clear
|
|
74
|
+
@data = {}
|
|
114
75
|
end
|
|
115
76
|
end
|
|
116
|
-
User.authenticates_with_sorcery!
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
def sorcery_controller_property_set(property, value)
|
|
120
|
-
::Sorcery::Controller::Config.send(:"#{property}=", value)
|
|
121
|
-
end
|
|
122
77
|
|
|
123
|
-
|
|
124
|
-
|
|
78
|
+
def session
|
|
79
|
+
SessionData.new(cookies)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def cookies
|
|
83
|
+
rack_test_session.instance_variable_get(:@rack_mock_session).cookie_jar
|
|
84
|
+
end
|
|
125
85
|
end
|
|
126
86
|
end
|
|
127
87
|
end
|
data/lib/sorcery/test_helpers.rb
CHANGED
|
@@ -1,52 +1,5 @@
|
|
|
1
1
|
module Sorcery
|
|
2
|
-
# This file will be included in the spec_helper file.
|
|
3
2
|
module TestHelpers
|
|
4
|
-
def self.included(base)
|
|
5
|
-
# reducing default cost for specs speed
|
|
6
|
-
CryptoProviders::BCrypt.class_eval do
|
|
7
|
-
class << self
|
|
8
|
-
def cost
|
|
9
|
-
1
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
# a patch to fix a bug in testing that happens when you 'destroy' a session twice.
|
|
16
|
-
# After the first destroy, the session is an ordinary hash, and then when destroy is called again there's an exception.
|
|
17
|
-
class ::Hash
|
|
18
|
-
def destroy
|
|
19
|
-
clear
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def create_new_user(attributes_hash = nil)
|
|
24
|
-
user_attributes_hash = attributes_hash || {:username => 'gizmo', :email => "bla@bla.com", :password => 'secret'}
|
|
25
|
-
@user = User.new(user_attributes_hash)
|
|
26
|
-
@user.save!
|
|
27
|
-
@user
|
|
28
|
-
end
|
|
29
3
|
|
|
30
|
-
def create_new_external_user(provider, attributes_hash = nil)
|
|
31
|
-
user_attributes_hash = attributes_hash || {:username => 'gizmo', :authentications_attributes => [{:provider => provider, :uid => 123}]}
|
|
32
|
-
@user = User.new(user_attributes_hash)
|
|
33
|
-
@user.save!
|
|
34
|
-
@user
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
def sorcery_model_property_set(property, *values)
|
|
38
|
-
User.class_eval do
|
|
39
|
-
sorcery_config.send(:"#{property}=", *values)
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
private
|
|
44
|
-
|
|
45
|
-
# reload user class between specs
|
|
46
|
-
# so it will be possible to test the different submodules in isolation
|
|
47
|
-
def reload_user_class
|
|
48
|
-
Object.send(:remove_const,:User)
|
|
49
|
-
load 'user.rb'
|
|
50
|
-
end
|
|
51
4
|
end
|
|
52
5
|
end
|