rmello-devise 2.1.0.2

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