devise-warbler 2.2.3

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.
Files changed (208) hide show
  1. data/.gitignore +10 -0
  2. data/.travis.yml +35 -0
  3. data/CHANGELOG.rdoc +923 -0
  4. data/CONTRIBUTING.md +14 -0
  5. data/Gemfile +32 -0
  6. data/Gemfile.lock +156 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.md +396 -0
  9. data/Rakefile +34 -0
  10. data/app/controllers/devise/confirmations_controller.rb +43 -0
  11. data/app/controllers/devise/omniauth_callbacks_controller.rb +30 -0
  12. data/app/controllers/devise/passwords_controller.rb +65 -0
  13. data/app/controllers/devise/registrations_controller.rb +119 -0
  14. data/app/controllers/devise/sessions_controller.rb +48 -0
  15. data/app/controllers/devise/unlocks_controller.rb +44 -0
  16. data/app/controllers/devise_controller.rb +184 -0
  17. data/app/helpers/devise_helper.rb +25 -0
  18. data/app/mailers/devise/mailer.rb +15 -0
  19. data/app/views/devise/_links.erb +3 -0
  20. data/app/views/devise/confirmations/new.html.erb +12 -0
  21. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  22. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  23. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  24. data/app/views/devise/passwords/edit.html.erb +16 -0
  25. data/app/views/devise/passwords/new.html.erb +12 -0
  26. data/app/views/devise/registrations/edit.html.erb +29 -0
  27. data/app/views/devise/registrations/new.html.erb +18 -0
  28. data/app/views/devise/sessions/new.html.erb +17 -0
  29. data/app/views/devise/shared/_links.erb +25 -0
  30. data/app/views/devise/unlocks/new.html.erb +12 -0
  31. data/config/locales/en.yml +59 -0
  32. data/devise.gemspec +24 -0
  33. data/gemfiles/Gemfile.rails-3.1.x +35 -0
  34. data/gemfiles/Gemfile.rails-3.1.x.lock +167 -0
  35. data/lib/devise.rb +451 -0
  36. data/lib/devise/controllers/helpers.rb +285 -0
  37. data/lib/devise/controllers/rememberable.rb +56 -0
  38. data/lib/devise/controllers/scoped_views.rb +17 -0
  39. data/lib/devise/controllers/url_helpers.rb +67 -0
  40. data/lib/devise/delegator.rb +16 -0
  41. data/lib/devise/failure_app.rb +187 -0
  42. data/lib/devise/hooks/activatable.rb +11 -0
  43. data/lib/devise/hooks/forgetable.rb +9 -0
  44. data/lib/devise/hooks/lockable.rb +7 -0
  45. data/lib/devise/hooks/rememberable.rb +6 -0
  46. data/lib/devise/hooks/timeoutable.rb +25 -0
  47. data/lib/devise/hooks/trackable.rb +9 -0
  48. data/lib/devise/mailers/helpers.rb +95 -0
  49. data/lib/devise/mapping.rb +172 -0
  50. data/lib/devise/models.rb +128 -0
  51. data/lib/devise/models/authenticatable.rb +276 -0
  52. data/lib/devise/models/confirmable.rb +267 -0
  53. data/lib/devise/models/database_authenticatable.rb +126 -0
  54. data/lib/devise/models/lockable.rb +193 -0
  55. data/lib/devise/models/omniauthable.rb +27 -0
  56. data/lib/devise/models/recoverable.rb +140 -0
  57. data/lib/devise/models/registerable.rb +25 -0
  58. data/lib/devise/models/rememberable.rb +125 -0
  59. data/lib/devise/models/timeoutable.rb +49 -0
  60. data/lib/devise/models/token_authenticatable.rb +89 -0
  61. data/lib/devise/models/trackable.rb +35 -0
  62. data/lib/devise/models/validatable.rb +66 -0
  63. data/lib/devise/modules.rb +29 -0
  64. data/lib/devise/omniauth.rb +28 -0
  65. data/lib/devise/omniauth/config.rb +45 -0
  66. data/lib/devise/omniauth/url_helpers.rb +18 -0
  67. data/lib/devise/orm/active_record.rb +3 -0
  68. data/lib/devise/orm/mongoid.rb +3 -0
  69. data/lib/devise/param_filter.rb +40 -0
  70. data/lib/devise/rails.rb +51 -0
  71. data/lib/devise/rails/routes.rb +448 -0
  72. data/lib/devise/rails/warden_compat.rb +43 -0
  73. data/lib/devise/strategies/authenticatable.rb +176 -0
  74. data/lib/devise/strategies/base.rb +20 -0
  75. data/lib/devise/strategies/database_authenticatable.rb +20 -0
  76. data/lib/devise/strategies/rememberable.rb +55 -0
  77. data/lib/devise/strategies/token_authenticatable.rb +56 -0
  78. data/lib/devise/test_helpers.rb +131 -0
  79. data/lib/devise/time_inflector.rb +14 -0
  80. data/lib/devise/version.rb +3 -0
  81. data/lib/generators/active_record/devise_generator.rb +79 -0
  82. data/lib/generators/active_record/templates/migration.rb +19 -0
  83. data/lib/generators/active_record/templates/migration_existing.rb +26 -0
  84. data/lib/generators/devise/devise_generator.rb +24 -0
  85. data/lib/generators/devise/install_generator.rb +24 -0
  86. data/lib/generators/devise/orm_helpers.rb +32 -0
  87. data/lib/generators/devise/views_generator.rb +122 -0
  88. data/lib/generators/mongoid/devise_generator.rb +57 -0
  89. data/lib/generators/templates/README +35 -0
  90. data/lib/generators/templates/devise.rb +240 -0
  91. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  92. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  93. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  94. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +16 -0
  95. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  96. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  97. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +27 -0
  98. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  99. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  100. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +16 -0
  101. data/test/controllers/custom_strategy_test.rb +62 -0
  102. data/test/controllers/helpers_test.rb +253 -0
  103. data/test/controllers/internal_helpers_test.rb +110 -0
  104. data/test/controllers/sessions_controller_test.rb +85 -0
  105. data/test/controllers/url_helpers_test.rb +59 -0
  106. data/test/delegator_test.rb +19 -0
  107. data/test/devise_test.rb +83 -0
  108. data/test/failure_app_test.rb +221 -0
  109. data/test/generators/active_record_generator_test.rb +75 -0
  110. data/test/generators/devise_generator_test.rb +39 -0
  111. data/test/generators/install_generator_test.rb +13 -0
  112. data/test/generators/mongoid_generator_test.rb +23 -0
  113. data/test/generators/views_generator_test.rb +67 -0
  114. data/test/helpers/devise_helper_test.rb +51 -0
  115. data/test/integration/authenticatable_test.rb +687 -0
  116. data/test/integration/confirmable_test.rb +299 -0
  117. data/test/integration/database_authenticatable_test.rb +84 -0
  118. data/test/integration/http_authenticatable_test.rb +97 -0
  119. data/test/integration/lockable_test.rb +242 -0
  120. data/test/integration/omniauthable_test.rb +133 -0
  121. data/test/integration/recoverable_test.rb +334 -0
  122. data/test/integration/registerable_test.rb +347 -0
  123. data/test/integration/rememberable_test.rb +165 -0
  124. data/test/integration/timeoutable_test.rb +140 -0
  125. data/test/integration/token_authenticatable_test.rb +161 -0
  126. data/test/integration/trackable_test.rb +92 -0
  127. data/test/mailers/confirmation_instructions_test.rb +106 -0
  128. data/test/mailers/reset_password_instructions_test.rb +87 -0
  129. data/test/mailers/unlock_instructions_test.rb +82 -0
  130. data/test/mapping_test.rb +127 -0
  131. data/test/models/authenticatable_test.rb +13 -0
  132. data/test/models/confirmable_test.rb +398 -0
  133. data/test/models/database_authenticatable_test.rb +207 -0
  134. data/test/models/lockable_test.rb +273 -0
  135. data/test/models/omniauthable_test.rb +7 -0
  136. data/test/models/recoverable_test.rb +205 -0
  137. data/test/models/registerable_test.rb +7 -0
  138. data/test/models/rememberable_test.rb +174 -0
  139. data/test/models/serializable_test.rb +49 -0
  140. data/test/models/timeoutable_test.rb +46 -0
  141. data/test/models/token_authenticatable_test.rb +55 -0
  142. data/test/models/trackable_test.rb +13 -0
  143. data/test/models/validatable_test.rb +117 -0
  144. data/test/models_test.rb +158 -0
  145. data/test/omniauth/config_test.rb +57 -0
  146. data/test/omniauth/url_helpers_test.rb +51 -0
  147. data/test/orm/active_record.rb +9 -0
  148. data/test/orm/mongoid.rb +13 -0
  149. data/test/rails_app/Rakefile +10 -0
  150. data/test/rails_app/app/active_record/admin.rb +6 -0
  151. data/test/rails_app/app/active_record/shim.rb +2 -0
  152. data/test/rails_app/app/active_record/user.rb +6 -0
  153. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  154. data/test/rails_app/app/controllers/admins_controller.rb +11 -0
  155. data/test/rails_app/app/controllers/application_controller.rb +9 -0
  156. data/test/rails_app/app/controllers/home_controller.rb +25 -0
  157. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  158. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  159. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +14 -0
  160. data/test/rails_app/app/controllers/users_controller.rb +23 -0
  161. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  162. data/test/rails_app/app/mailers/users/mailer.rb +8 -0
  163. data/test/rails_app/app/mongoid/admin.rb +29 -0
  164. data/test/rails_app/app/mongoid/shim.rb +24 -0
  165. data/test/rails_app/app/mongoid/user.rb +42 -0
  166. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  167. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  168. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  169. data/test/rails_app/app/views/home/index.html.erb +1 -0
  170. data/test/rails_app/app/views/home/join.html.erb +1 -0
  171. data/test/rails_app/app/views/home/private.html.erb +1 -0
  172. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  173. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  174. data/test/rails_app/app/views/users/index.html.erb +1 -0
  175. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  176. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  177. data/test/rails_app/config.ru +4 -0
  178. data/test/rails_app/config/application.rb +41 -0
  179. data/test/rails_app/config/boot.rb +8 -0
  180. data/test/rails_app/config/database.yml +18 -0
  181. data/test/rails_app/config/environment.rb +5 -0
  182. data/test/rails_app/config/environments/development.rb +18 -0
  183. data/test/rails_app/config/environments/production.rb +33 -0
  184. data/test/rails_app/config/environments/test.rb +33 -0
  185. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  186. data/test/rails_app/config/initializers/devise.rb +178 -0
  187. data/test/rails_app/config/initializers/inflections.rb +2 -0
  188. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  189. data/test/rails_app/config/routes.rb +100 -0
  190. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +74 -0
  191. data/test/rails_app/db/schema.rb +52 -0
  192. data/test/rails_app/lib/shared_admin.rb +14 -0
  193. data/test/rails_app/lib/shared_user.rb +26 -0
  194. data/test/rails_app/public/404.html +26 -0
  195. data/test/rails_app/public/422.html +26 -0
  196. data/test/rails_app/public/500.html +26 -0
  197. data/test/rails_app/public/favicon.ico +0 -0
  198. data/test/rails_app/script/rails +10 -0
  199. data/test/routes_test.rb +248 -0
  200. data/test/support/assertions.rb +40 -0
  201. data/test/support/helpers.rb +91 -0
  202. data/test/support/integration.rb +92 -0
  203. data/test/support/locale/en.yml +4 -0
  204. data/test/support/webrat/integrations/rails.rb +24 -0
  205. data/test/test_helper.rb +27 -0
  206. data/test/test_helpers_test.rb +151 -0
  207. data/test/test_models.rb +27 -0
  208. metadata +423 -0
data/lib/devise.rb ADDED
@@ -0,0 +1,451 @@
1
+ require 'rails'
2
+ require 'active_support/core_ext/numeric/time'
3
+ require 'active_support/dependencies'
4
+ require 'orm_adapter'
5
+ require 'set'
6
+ require 'securerandom'
7
+
8
+ module Devise
9
+ autoload :Delegator, 'devise/delegator'
10
+ autoload :FailureApp, 'devise/failure_app'
11
+ autoload :OmniAuth, 'devise/omniauth'
12
+ autoload :ParamFilter, 'devise/param_filter'
13
+ autoload :TestHelpers, 'devise/test_helpers'
14
+ autoload :TimeInflector, 'devise/time_inflector'
15
+
16
+ module Controllers
17
+ autoload :Helpers, 'devise/controllers/helpers'
18
+ autoload :Rememberable, 'devise/controllers/rememberable'
19
+ autoload :ScopedViews, 'devise/controllers/scoped_views'
20
+ autoload :UrlHelpers, 'devise/controllers/url_helpers'
21
+ end
22
+
23
+ module Mailers
24
+ autoload :Helpers, 'devise/mailers/helpers'
25
+ end
26
+
27
+ module Strategies
28
+ autoload :Base, 'devise/strategies/base'
29
+ autoload :Authenticatable, 'devise/strategies/authenticatable'
30
+ end
31
+
32
+ # Constants which holds devise configuration for extensions. Those should
33
+ # not be modified by the "end user" (this is why they are constants).
34
+ ALL = []
35
+ CONTROLLERS = ActiveSupport::OrderedHash.new
36
+ ROUTES = ActiveSupport::OrderedHash.new
37
+ STRATEGIES = ActiveSupport::OrderedHash.new
38
+ URL_HELPERS = ActiveSupport::OrderedHash.new
39
+
40
+ # Strategies that do not require user input.
41
+ NO_INPUT = []
42
+
43
+ # True values used to check params
44
+ TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE']
45
+
46
+ # Custom domain or key for cookies. Not set by default
47
+ mattr_accessor :rememberable_options
48
+ @@rememberable_options = {}
49
+
50
+ # The number of times to encrypt password.
51
+ mattr_accessor :stretches
52
+ @@stretches = 10
53
+
54
+ # Keys used when authenticating a user.
55
+ mattr_accessor :authentication_keys
56
+ @@authentication_keys = [ :email ]
57
+
58
+ # Request keys used when authenticating a user.
59
+ mattr_accessor :request_keys
60
+ @@request_keys = []
61
+
62
+ # Keys that should be case-insensitive.
63
+ mattr_accessor :case_insensitive_keys
64
+ @@case_insensitive_keys = [ :email ]
65
+
66
+ # Keys that should have whitespace stripped.
67
+ mattr_accessor :strip_whitespace_keys
68
+ @@strip_whitespace_keys = []
69
+
70
+ # If http authentication is enabled by default.
71
+ mattr_accessor :http_authenticatable
72
+ @@http_authenticatable = false
73
+
74
+ # If http headers should be returned for ajax requests. True by default.
75
+ mattr_accessor :http_authenticatable_on_xhr
76
+ @@http_authenticatable_on_xhr = true
77
+
78
+ # If params authenticatable is enabled by default.
79
+ mattr_accessor :params_authenticatable
80
+ @@params_authenticatable = true
81
+
82
+ # The realm used in Http Basic Authentication.
83
+ mattr_accessor :http_authentication_realm
84
+ @@http_authentication_realm = "Application"
85
+
86
+ # Email regex used to validate email formats. It simply asserts that
87
+ # an one (and only one) @ exists in the given string. This is mainly
88
+ # to give user feedback and not to assert the e-mail validity.
89
+ mattr_accessor :email_regexp
90
+ @@email_regexp = /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/
91
+
92
+ # Range validation for password length
93
+ mattr_accessor :password_length
94
+ @@password_length = 6..128
95
+
96
+ # The time the user will be remembered without asking for credentials again.
97
+ mattr_accessor :remember_for
98
+ @@remember_for = 2.weeks
99
+
100
+ # If true, extends the user's remember period when remembered via cookie.
101
+ mattr_accessor :extend_remember_period
102
+ @@extend_remember_period = false
103
+
104
+ # Time interval you can access your account before confirming your account.
105
+ # nil - allows unconfirmed access for unlimited time
106
+ mattr_accessor :allow_unconfirmed_access_for
107
+ @@allow_unconfirmed_access_for = 0.days
108
+
109
+ # Time interval the confirmation token is valid. nil = unlimited
110
+ mattr_accessor :confirm_within
111
+ @@confirm_within = nil
112
+
113
+ # Defines which key will be used when confirming an account.
114
+ mattr_accessor :confirmation_keys
115
+ @@confirmation_keys = [ :email ]
116
+
117
+ # Defines if email should be reconfirmable.
118
+ # False by default for backwards compatibility.
119
+ mattr_accessor :reconfirmable
120
+ @@reconfirmable = false
121
+
122
+ # Time interval to timeout the user session without activity.
123
+ mattr_accessor :timeout_in
124
+ @@timeout_in = 30.minutes
125
+
126
+ # Authentication token expiration on timeout
127
+ mattr_accessor :expire_auth_token_on_timeout
128
+ @@expire_auth_token_on_timeout = false
129
+
130
+ # Used to encrypt password. Please generate one with rake secret.
131
+ mattr_accessor :pepper
132
+ @@pepper = nil
133
+
134
+ # Scoped views. Since it relies on fallbacks to render default views, it's
135
+ # turned off by default.
136
+ mattr_accessor :scoped_views
137
+ @@scoped_views = false
138
+
139
+ # Defines which strategy can be used to lock an account.
140
+ # Values: :failed_attempts, :none
141
+ mattr_accessor :lock_strategy
142
+ @@lock_strategy = :failed_attempts
143
+
144
+ # Defines which key will be used when locking and unlocking an account
145
+ mattr_accessor :unlock_keys
146
+ @@unlock_keys = [ :email ]
147
+
148
+ # Defines which strategy can be used to unlock an account.
149
+ # Values: :email, :time, :both
150
+ mattr_accessor :unlock_strategy
151
+ @@unlock_strategy = :both
152
+
153
+ # Number of authentication tries before locking an account
154
+ mattr_accessor :maximum_attempts
155
+ @@maximum_attempts = 20
156
+
157
+ # Time interval to unlock the account if :time is defined as unlock_strategy.
158
+ mattr_accessor :unlock_in
159
+ @@unlock_in = 1.hour
160
+
161
+ # Defines which key will be used when recovering the password for an account
162
+ mattr_accessor :reset_password_keys
163
+ @@reset_password_keys = [ :email ]
164
+
165
+ # Time interval you can reset your password with a reset password key
166
+ mattr_accessor :reset_password_within
167
+ @@reset_password_within = 6.hours
168
+
169
+ # The default scope which is used by warden.
170
+ mattr_accessor :default_scope
171
+ @@default_scope = nil
172
+
173
+ # Address which sends Devise e-mails.
174
+ mattr_accessor :mailer_sender
175
+ @@mailer_sender = nil
176
+
177
+ # Authentication token params key name of choice. E.g. /users/sign_in?some_key=...
178
+ mattr_accessor :token_authentication_key
179
+ @@token_authentication_key = :auth_token
180
+
181
+ # Skip session storage for the following strategies
182
+ mattr_accessor :skip_session_storage
183
+ @@skip_session_storage = []
184
+
185
+ # Which formats should be treated as navigational.
186
+ mattr_accessor :navigational_formats
187
+ @@navigational_formats = ["*/*", :html]
188
+
189
+ # When set to true, signing out a user signs out all other scopes.
190
+ mattr_accessor :sign_out_all_scopes
191
+ @@sign_out_all_scopes = true
192
+
193
+ # The default method used while signing out
194
+ mattr_accessor :sign_out_via
195
+ @@sign_out_via = :get
196
+
197
+ # The parent controller all Devise controllers inherits from.
198
+ # Defaults to ApplicationController. This should be set early
199
+ # in the initialization process and should be set to a string.
200
+ mattr_accessor :parent_controller
201
+ @@parent_controller = "ApplicationController"
202
+
203
+ # The parent mailer all Devise mailers inherit from.
204
+ # Defaults to ActionMailer::Base. This should be set early
205
+ # in the initialization process and should be set to a string.
206
+ mattr_accessor :parent_mailer
207
+ @@parent_mailer = "ActionMailer::Base"
208
+
209
+ # The router Devise should use to generate routes. Defaults
210
+ # to :main_app. Should be overriden by engines in order
211
+ # to provide custom routes.
212
+ mattr_accessor :router_name
213
+ @@router_name = nil
214
+
215
+ # Set the omniauth path prefix so it can be overriden when
216
+ # Devise is used in a mountable engine
217
+ mattr_accessor :omniauth_path_prefix
218
+ @@omniauth_path_prefix = nil
219
+
220
+ def self.encryptor=(value)
221
+ warn "\n[DEVISE] To select a encryption which isn't bcrypt, you should use devise-encryptable gem.\n"
222
+ end
223
+
224
+ def self.use_salt_as_remember_token=(value)
225
+ warn "\n[DEVISE] Devise.use_salt_as_remember_token is deprecated and has no effect. Please remove it.\n"
226
+ end
227
+
228
+ def self.apply_schema=(value)
229
+ warn "\n[DEVISE] Devise.apply_schema is deprecated and has no effect. Please remove it.\n"
230
+ end
231
+
232
+ # PRIVATE CONFIGURATION
233
+
234
+ # Store scopes mappings.
235
+ mattr_reader :mappings
236
+ @@mappings = ActiveSupport::OrderedHash.new
237
+
238
+ # Omniauth configurations.
239
+ mattr_reader :omniauth_configs
240
+ @@omniauth_configs = ActiveSupport::OrderedHash.new
241
+
242
+ # Define a set of modules that are called when a mapping is added.
243
+ mattr_reader :helpers
244
+ @@helpers = Set.new
245
+ @@helpers << Devise::Controllers::Helpers
246
+
247
+ # Private methods to interface with Warden.
248
+ mattr_accessor :warden_config
249
+ @@warden_config = nil
250
+ @@warden_config_block = nil
251
+
252
+ # When true, enter in paranoid mode to avoid user enumeration.
253
+ mattr_accessor :paranoid
254
+ @@paranoid = false
255
+
256
+ # Default way to setup Devise. Run rails generate devise_install to create
257
+ # a fresh initializer with all configuration values.
258
+ def self.setup
259
+ yield self
260
+ end
261
+
262
+ class Getter
263
+ def initialize name
264
+ @name = name
265
+ end
266
+
267
+ def get
268
+ ActiveSupport::Dependencies.constantize(@name)
269
+ end
270
+ end
271
+
272
+ def self.ref(arg)
273
+ if defined?(ActiveSupport::Dependencies::ClassCache)
274
+ ActiveSupport::Dependencies::reference(arg)
275
+ Getter.new(arg)
276
+ else
277
+ ActiveSupport::Dependencies.ref(arg)
278
+ end
279
+ end
280
+
281
+ def self.available_router_name
282
+ router_name || :main_app
283
+ end
284
+
285
+ def self.omniauth_providers
286
+ omniauth_configs.keys
287
+ end
288
+
289
+ # Get the mailer class from the mailer reference object.
290
+ def self.mailer
291
+ @@mailer_ref.get
292
+ end
293
+
294
+ # Set the mailer reference object to access the mailer.
295
+ def self.mailer=(class_name)
296
+ @@mailer_ref = ref(class_name)
297
+ end
298
+ self.mailer = "Devise::Mailer"
299
+
300
+ # Small method that adds a mapping to Devise.
301
+ def self.add_mapping(resource, options)
302
+ mapping = Devise::Mapping.new(resource, options)
303
+ @@mappings[mapping.name] = mapping
304
+ @@default_scope ||= mapping.name
305
+ @@helpers.each { |h| h.define_helpers(mapping) }
306
+ mapping
307
+ end
308
+
309
+ # Make Devise aware of an 3rd party Devise-module (like invitable). For convenience.
310
+ #
311
+ # == Options:
312
+ #
313
+ # +model+ - String representing the load path to a custom *model* for this module (to autoload.)
314
+ # +controller+ - Symbol representing the name of an exisiting or custom *controller* for this module.
315
+ # +route+ - Symbol representing the named *route* helper for this module.
316
+ # +strategy+ - Symbol representing if this module got a custom *strategy*.
317
+ #
318
+ # All values, except :model, accept also a boolean and will have the same name as the given module
319
+ # name.
320
+ #
321
+ # == Examples:
322
+ #
323
+ # Devise.add_module(:party_module)
324
+ # Devise.add_module(:party_module, :strategy => true, :controller => :sessions)
325
+ # Devise.add_module(:party_module, :model => 'party_module/model')
326
+ #
327
+ def self.add_module(module_name, options = {})
328
+ ALL << module_name
329
+ options.assert_valid_keys(:strategy, :model, :controller, :route, :no_input)
330
+
331
+ if strategy = options[:strategy]
332
+ strategy = (strategy == true ? module_name : strategy)
333
+ STRATEGIES[module_name] = strategy
334
+ end
335
+
336
+ if controller = options[:controller]
337
+ controller = (controller == true ? module_name : controller)
338
+ CONTROLLERS[module_name] = controller
339
+ end
340
+
341
+ NO_INPUT << strategy if options[:no_input]
342
+
343
+ if route = options[:route]
344
+ case route
345
+ when TrueClass
346
+ key, value = module_name, []
347
+ when Symbol
348
+ key, value = route, []
349
+ when Hash
350
+ key, value = route.keys.first, route.values.flatten
351
+ else
352
+ raise ArgumentError, ":route should be true, a Symbol or a Hash"
353
+ end
354
+
355
+ URL_HELPERS[key] ||= []
356
+ URL_HELPERS[key].concat(value)
357
+ URL_HELPERS[key].uniq!
358
+
359
+ ROUTES[module_name] = key
360
+ end
361
+
362
+ if options[:model]
363
+ path = (options[:model] == true ? "devise/models/#{module_name}" : options[:model])
364
+ camelized = ActiveSupport::Inflector.camelize(module_name.to_s)
365
+ Devise::Models.send(:autoload, camelized.to_sym, path)
366
+ end
367
+
368
+ Devise::Mapping.add_module module_name
369
+ end
370
+
371
+ # Sets warden configuration using a block that will be invoked on warden
372
+ # initialization.
373
+ #
374
+ # Devise.initialize do |config|
375
+ # config.allow_unconfirmed_access_for = 2.days
376
+ #
377
+ # config.warden do |manager|
378
+ # # Configure warden to use other strategies, like oauth.
379
+ # manager.oauth(:twitter)
380
+ # end
381
+ # end
382
+ def self.warden(&block)
383
+ @@warden_config_block = block
384
+ end
385
+
386
+ # Specify an omniauth provider.
387
+ #
388
+ # config.omniauth :github, APP_ID, APP_SECRET
389
+ #
390
+ def self.omniauth(provider, *args)
391
+ @@helpers << Devise::OmniAuth::UrlHelpers
392
+ config = Devise::OmniAuth::Config.new(provider, args)
393
+ @@omniauth_configs[config.strategy_name.to_sym] = config
394
+ end
395
+
396
+ # Include helpers in the given scope to AC and AV.
397
+ def self.include_helpers(scope)
398
+ ActiveSupport.on_load(:action_controller) do
399
+ include scope::Helpers if defined?(scope::Helpers)
400
+ include scope::UrlHelpers
401
+ end
402
+
403
+ ActiveSupport.on_load(:action_view) do
404
+ include scope::UrlHelpers
405
+ end
406
+ end
407
+
408
+ # Regenerates url helpers considering Devise.mapping
409
+ def self.regenerate_helpers!
410
+ Devise::Controllers::UrlHelpers.remove_helpers!
411
+ Devise::Controllers::UrlHelpers.generate_helpers!
412
+ end
413
+
414
+ # A method used internally to setup warden manager from the Rails initialize
415
+ # block.
416
+ def self.configure_warden! #:nodoc:
417
+ @@warden_configured ||= begin
418
+ warden_config.failure_app = Devise::Delegator.new
419
+ warden_config.default_scope = Devise.default_scope
420
+ warden_config.intercept_401 = false
421
+
422
+ Devise.mappings.each_value do |mapping|
423
+ warden_config.scope_defaults mapping.name, :strategies => mapping.strategies
424
+ end
425
+
426
+ @@warden_config_block.try :call, Devise.warden_config
427
+ true
428
+ end
429
+ end
430
+
431
+ # Generate a friendly string randomically to be used as token.
432
+ def self.friendly_token
433
+ SecureRandom.base64(15).tr('+/=lIO0', 'pqrsxyz')
434
+ end
435
+
436
+ # constant-time comparison algorithm to prevent timing attacks
437
+ def self.secure_compare(a, b)
438
+ return false if a.blank? || b.blank? || a.bytesize != b.bytesize
439
+ l = a.unpack "C#{a.bytesize}"
440
+
441
+ res = 0
442
+ b.each_byte { |byte| res |= byte ^ l.shift }
443
+ res == 0
444
+ end
445
+ end
446
+
447
+ require 'warden'
448
+ require 'devise/mapping'
449
+ require 'devise/models'
450
+ require 'devise/modules'
451
+ require 'devise/rails'
@@ -0,0 +1,285 @@
1
+ module Devise
2
+ module Controllers
3
+ # Those helpers are convenience methods added to ApplicationController.
4
+ module Helpers
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ helper_method :warden, :signed_in?, :devise_controller?
9
+ end
10
+
11
+ module ClassMethods
12
+ def log_process_action(payload)
13
+ payload[:status] ||= 401 unless payload[:exception]
14
+ super
15
+ end
16
+ end
17
+
18
+ # Define authentication filters and accessor helpers based on mappings.
19
+ # These filters should be used inside the controllers as before_filters,
20
+ # so you can control the scope of the user who should be signed in to
21
+ # access that specific controller/action.
22
+ # Example:
23
+ #
24
+ # Roles:
25
+ # User
26
+ # Admin
27
+ #
28
+ # Generated methods:
29
+ # authenticate_user! # Signs user in or redirect
30
+ # authenticate_admin! # Signs admin in or redirect
31
+ # user_signed_in? # Checks whether there is a user signed in or not
32
+ # admin_signed_in? # Checks whether there is an admin signed in or not
33
+ # current_user # Current signed in user
34
+ # current_admin # Current signed in admin
35
+ # user_session # Session data available only to the user scope
36
+ # admin_session # Session data available only to the admin scope
37
+ #
38
+ # Use:
39
+ # before_filter :authenticate_user! # Tell devise to use :user map
40
+ # before_filter :authenticate_admin! # Tell devise to use :admin map
41
+ #
42
+ def self.define_helpers(mapping) #:nodoc:
43
+ mapping = mapping.name
44
+
45
+ class_eval <<-METHODS, __FILE__, __LINE__ + 1
46
+ def authenticate_#{mapping}!(opts={})
47
+ opts[:scope] = :#{mapping}
48
+ warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
49
+ end
50
+
51
+ def #{mapping}_signed_in?
52
+ !!current_#{mapping}
53
+ end
54
+
55
+ def current_#{mapping}
56
+ @current_#{mapping} ||= warden.authenticate(:scope => :#{mapping})
57
+ end
58
+
59
+ def #{mapping}_session
60
+ current_#{mapping} && warden.session(:#{mapping})
61
+ end
62
+ METHODS
63
+
64
+ ActiveSupport.on_load(:action_controller) do
65
+ helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session"
66
+ end
67
+ end
68
+
69
+ # The main accessor for the warden proxy instance
70
+ def warden
71
+ request.env['warden']
72
+ end
73
+
74
+ # Return true if it's a devise_controller. false to all controllers unless
75
+ # the controllers defined inside devise. Useful if you want to apply a before
76
+ # filter to all controllers, except the ones in devise:
77
+ #
78
+ # before_filter :my_filter, :unless => :devise_controller?
79
+ def devise_controller?
80
+ is_a?(DeviseController)
81
+ end
82
+
83
+ # Tell warden that params authentication is allowed for that specific page.
84
+ def allow_params_authentication!
85
+ request.env["devise.allow_params_authentication"] = true
86
+ end
87
+
88
+ # Return true if the given scope is signed in session. If no scope given, return
89
+ # true if any scope is signed in. Does not run authentication hooks.
90
+ def signed_in?(scope=nil)
91
+ [ scope || Devise.mappings.keys ].flatten.any? do |_scope|
92
+ warden.authenticate?(:scope => _scope)
93
+ end
94
+ end
95
+
96
+ # Sign in a user that already was authenticated. This helper is useful for logging
97
+ # users in after sign up.
98
+ #
99
+ # All options given to sign_in is passed forward to the set_user method in warden.
100
+ # The only exception is the :bypass option, which bypass warden callbacks and stores
101
+ # the user straight in session. This option is useful in cases the user is already
102
+ # signed in, but we want to refresh the credentials in session.
103
+ #
104
+ # Examples:
105
+ #
106
+ # sign_in :user, @user # sign_in(scope, resource)
107
+ # sign_in @user # sign_in(resource)
108
+ # sign_in @user, :event => :authentication # sign_in(resource, options)
109
+ # sign_in @user, :bypass => true # sign_in(resource, options)
110
+ #
111
+ def sign_in(resource_or_scope, *args)
112
+ options = args.extract_options!
113
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
114
+ resource = args.last || resource_or_scope
115
+
116
+ expire_session_data_after_sign_in!
117
+
118
+ if options[:bypass]
119
+ warden.session_serializer.store(resource, scope)
120
+ elsif warden.user(scope) == resource && !options.delete(:force)
121
+ # Do nothing. User already signed in and we are not forcing it.
122
+ true
123
+ else
124
+ warden.set_user(resource, options.merge!(:scope => scope))
125
+ end
126
+ end
127
+
128
+ # Sign out a given user or scope. This helper is useful for signing out a user
129
+ # after deleting accounts. Returns true if there was a logout and false if there
130
+ # is no user logged in on the referred scope
131
+ #
132
+ # Examples:
133
+ #
134
+ # sign_out :user # sign_out(scope)
135
+ # sign_out @user # sign_out(resource)
136
+ #
137
+ def sign_out(resource_or_scope=nil)
138
+ return sign_out_all_scopes unless resource_or_scope
139
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
140
+ user = warden.user(:scope => scope, :run_callbacks => false) # If there is no user
141
+
142
+ warden.raw_session.inspect # Without this inspect here. The session does not clear.
143
+ warden.logout(scope)
144
+ warden.clear_strategies_cache!(:scope => scope)
145
+ instance_variable_set(:"@current_#{scope}", nil)
146
+
147
+ !!user
148
+ end
149
+
150
+ # Sign out all active users or scopes. This helper is useful for signing out all roles
151
+ # in one click. This signs out ALL scopes in warden. Returns true if there was at least one logout
152
+ # and false if there was no user logged in on all scopes.
153
+ def sign_out_all_scopes(lock=true)
154
+ users = Devise.mappings.keys.map { |s| warden.user(:scope => s, :run_callbacks => false) }
155
+
156
+ warden.raw_session.inspect
157
+ warden.logout
158
+ expire_devise_cached_variables!
159
+ warden.clear_strategies_cache!
160
+ warden.lock! if lock
161
+
162
+ users.any?
163
+ end
164
+
165
+ # Returns and delete (if it's navigational format) the url stored in the session for
166
+ # the given scope. Useful for giving redirect backs after sign up:
167
+ #
168
+ # Example:
169
+ #
170
+ # redirect_to stored_location_for(:user) || root_path
171
+ #
172
+ def stored_location_for(resource_or_scope)
173
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
174
+
175
+ if is_navigational_format?
176
+ session.delete("#{scope}_return_to")
177
+ else
178
+ session["#{scope}_return_to"]
179
+ end
180
+ end
181
+
182
+ # The scope root url to be used when he's signed in. By default, it first
183
+ # tries to find a resource_root_path, otherwise it uses the root_path.
184
+ def signed_in_root_path(resource_or_scope)
185
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
186
+ home_path = "#{scope}_root_path"
187
+ if respond_to?(home_path, true)
188
+ send(home_path)
189
+ elsif respond_to?(:root_path)
190
+ root_path
191
+ else
192
+ "/"
193
+ end
194
+ end
195
+
196
+ # The default url to be used after signing in. This is used by all Devise
197
+ # controllers and you can overwrite it in your ApplicationController to
198
+ # provide a custom hook for a custom resource.
199
+ #
200
+ # By default, it first tries to find a valid resource_return_to key in the
201
+ # session, then it fallbacks to resource_root_path, otherwise it uses the
202
+ # root path. For a user scope, you can define the default url in
203
+ # the following way:
204
+ #
205
+ # map.user_root '/users', :controller => 'users' # creates user_root_path
206
+ #
207
+ # map.namespace :user do |user|
208
+ # user.root :controller => 'users' # creates user_root_path
209
+ # end
210
+ #
211
+ # If the resource root path is not defined, root_path is used. However,
212
+ # if this default is not enough, you can customize it, for example:
213
+ #
214
+ # def after_sign_in_path_for(resource)
215
+ # stored_location_for(resource) ||
216
+ # if resource.is_a?(User) && resource.can_publish?
217
+ # publisher_url
218
+ # else
219
+ # super
220
+ # end
221
+ # end
222
+ #
223
+ def after_sign_in_path_for(resource_or_scope)
224
+ stored_location_for(resource_or_scope) || signed_in_root_path(resource_or_scope)
225
+ end
226
+
227
+ # Method used by sessions controller to sign out a user. You can overwrite
228
+ # it in your ApplicationController to provide a custom hook for a custom
229
+ # scope. Notice that differently from +after_sign_in_path_for+ this method
230
+ # receives a symbol with the scope, and not the resource.
231
+ #
232
+ # By default it is the root_path.
233
+ def after_sign_out_path_for(resource_or_scope)
234
+ respond_to?(:root_path) ? root_path : "/"
235
+ end
236
+
237
+ # Sign in a user and tries to redirect first to the stored location and
238
+ # then to the url specified by after_sign_in_path_for. It accepts the same
239
+ # parameters as the sign_in method.
240
+ def sign_in_and_redirect(resource_or_scope, *args)
241
+ options = args.extract_options!
242
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
243
+ resource = args.last || resource_or_scope
244
+ sign_in(scope, resource, options)
245
+ redirect_to after_sign_in_path_for(resource)
246
+ end
247
+
248
+ def expire_session_data_after_sign_in!
249
+ session.keys.grep(/^devise\./).each { |k| session.delete(k) }
250
+ end
251
+
252
+ # Sign out a user and tries to redirect to the url specified by
253
+ # after_sign_out_path_for.
254
+ def sign_out_and_redirect(resource_or_scope)
255
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
256
+ redirect_path = after_sign_out_path_for(scope)
257
+ Devise.sign_out_all_scopes ? sign_out : sign_out(scope)
258
+ redirect_to redirect_path
259
+ end
260
+
261
+ # Overwrite Rails' handle unverified request to sign out all scopes,
262
+ # clear run strategies and remove cached variables.
263
+ def handle_unverified_request
264
+ sign_out_all_scopes(false)
265
+ request.env["devise.skip_storage"] = true
266
+ expire_devise_cached_variables!
267
+ super # call the default behaviour which resets the session
268
+ end
269
+
270
+ def request_format
271
+ @request_format ||= request.format.try(:ref)
272
+ end
273
+
274
+ def is_navigational_format?
275
+ Devise.navigational_formats.include?(request_format)
276
+ end
277
+
278
+ private
279
+
280
+ def expire_devise_cached_variables!
281
+ Devise.mappings.each { |_,m| instance_variable_set("@current_#{m.name}", nil) }
282
+ end
283
+ end
284
+ end
285
+ end