af-devise 2.1.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.
Files changed (207) hide show
  1. data/.gitignore +10 -0
  2. data/.travis.yml +15 -0
  3. data/CHANGELOG.rdoc +885 -0
  4. data/CONTRIBUTING.md +14 -0
  5. data/Gemfile +29 -0
  6. data/Gemfile.lock +155 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.md +394 -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 +50 -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 +25 -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 +25 -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 +444 -0
  36. data/lib/devise/controllers/helpers.rb +285 -0
  37. data/lib/devise/controllers/rememberable.rb +52 -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 +91 -0
  49. data/lib/devise/mapping.rb +172 -0
  50. data/lib/devise/models.rb +128 -0
  51. data/lib/devise/models/authenticatable.rb +268 -0
  52. data/lib/devise/models/confirmable.rb +270 -0
  53. data/lib/devise/models/database_authenticatable.rb +127 -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 +41 -0
  70. data/lib/devise/rails.rb +54 -0
  71. data/lib/devise/rails/routes.rb +446 -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 +116 -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 +15 -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 +22 -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 +15 -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 +72 -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 +52 -0
  114. data/test/helpers/devise_helper_test.rb +51 -0
  115. data/test/integration/authenticatable_test.rb +633 -0
  116. data/test/integration/confirmable_test.rb +298 -0
  117. data/test/integration/database_authenticatable_test.rb +82 -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 +345 -0
  123. data/test/integration/rememberable_test.rb +158 -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 +102 -0
  128. data/test/mailers/reset_password_instructions_test.rb +83 -0
  129. data/test/mailers/unlock_instructions_test.rb +77 -0
  130. data/test/mapping_test.rb +127 -0
  131. data/test/models/authenticatable_test.rb +7 -0
  132. data/test/models/confirmable_test.rb +391 -0
  133. data/test/models/database_authenticatable_test.rb +196 -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 +179 -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 +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 +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. metadata +421 -0
@@ -0,0 +1,444 @@
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 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[^@]+@([^@\.]+\.)+[^@\.]+\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
+ mattr_accessor :allow_unconfirmed_access_for
106
+ @@allow_unconfirmed_access_for = 0.days
107
+
108
+ # Time interval the confirmation token is valid. nil = unlimited
109
+ mattr_accessor :confirm_within
110
+ @@confirm_within = nil
111
+
112
+ # Defines which key will be used when confirming an account.
113
+ mattr_accessor :confirmation_keys
114
+ @@confirmation_keys = [ :email ]
115
+
116
+ # Defines if email should be reconfirmable.
117
+ # False by default for backwards compatibility.
118
+ mattr_accessor :reconfirmable
119
+ @@reconfirmable = false
120
+
121
+ # Time interval to timeout the user session without activity.
122
+ mattr_accessor :timeout_in
123
+ @@timeout_in = 30.minutes
124
+
125
+ # Authentication token expiration on timeout
126
+ mattr_accessor :expire_auth_token_on_timeout
127
+ @@expire_auth_token_on_timeout = false
128
+
129
+ # Used to encrypt password. Please generate one with rake secret.
130
+ mattr_accessor :pepper
131
+ @@pepper = nil
132
+
133
+ # Scoped views. Since it relies on fallbacks to render default views, it's
134
+ # turned off by default.
135
+ mattr_accessor :scoped_views
136
+ @@scoped_views = false
137
+
138
+ # Defines which strategy can be used to lock an account.
139
+ # Values: :failed_attempts, :none
140
+ mattr_accessor :lock_strategy
141
+ @@lock_strategy = :failed_attempts
142
+
143
+ # Defines which key will be used when locking and unlocking an account
144
+ mattr_accessor :unlock_keys
145
+ @@unlock_keys = [ :email ]
146
+
147
+ # Defines which strategy can be used to unlock an account.
148
+ # Values: :email, :time, :both
149
+ mattr_accessor :unlock_strategy
150
+ @@unlock_strategy = :both
151
+
152
+ # Number of authentication tries before locking an account
153
+ mattr_accessor :maximum_attempts
154
+ @@maximum_attempts = 20
155
+
156
+ # Time interval to unlock the account if :time is defined as unlock_strategy.
157
+ mattr_accessor :unlock_in
158
+ @@unlock_in = 1.hour
159
+
160
+ # Defines which key will be used when recovering the password for an account
161
+ mattr_accessor :reset_password_keys
162
+ @@reset_password_keys = [ :email ]
163
+
164
+ # Time interval you can reset your password with a reset password key
165
+ mattr_accessor :reset_password_within
166
+ @@reset_password_within = 6.hours
167
+
168
+ # The default scope which is used by warden.
169
+ mattr_accessor :default_scope
170
+ @@default_scope = nil
171
+
172
+ # Address which sends Devise e-mails.
173
+ mattr_accessor :mailer_sender
174
+ @@mailer_sender = nil
175
+
176
+ # Authentication token params key name of choice. E.g. /users/sign_in?some_key=...
177
+ mattr_accessor :token_authentication_key
178
+ @@token_authentication_key = :auth_token
179
+
180
+ # Skip session storage for the following strategies
181
+ mattr_accessor :skip_session_storage
182
+ @@skip_session_storage = []
183
+
184
+ # Which formats should be treated as navigational.
185
+ mattr_accessor :navigational_formats
186
+ @@navigational_formats = ["*/*", :html]
187
+
188
+ # When set to true, signing out a user signs out all other scopes.
189
+ mattr_accessor :sign_out_all_scopes
190
+ @@sign_out_all_scopes = true
191
+
192
+ # The default method used while signing out
193
+ mattr_accessor :sign_out_via
194
+ @@sign_out_via = :get
195
+
196
+ # The parent controller all Devise controllers inherits from.
197
+ # Defaults to ApplicationController. This should be set early
198
+ # in the initialization process and should be set to a string.
199
+ mattr_accessor :parent_controller
200
+ @@parent_controller = "ApplicationController"
201
+
202
+ # The router Devise should use to generate routes. Defaults
203
+ # to :main_app. Should be overriden by engines in order
204
+ # to provide custom routes.
205
+ mattr_accessor :router_name
206
+ @@router_name = nil
207
+
208
+ # Set the omniauth path prefix so it can be overriden when
209
+ # Devise is used in a mountable engine
210
+ mattr_accessor :omniauth_path_prefix
211
+ @@omniauth_path_prefix = nil
212
+
213
+ def self.encryptor=(value)
214
+ warn "\n[DEVISE] To select a encryption which isn't bcrypt, you should use devise-encryptable gem.\n"
215
+ end
216
+
217
+ def self.use_salt_as_remember_token=(value)
218
+ warn "\n[DEVISE] Devise.use_salt_as_remember_token is deprecated and has no effect. Please remove it.\n"
219
+ end
220
+
221
+ def self.apply_schema=(value)
222
+ warn "\n[DEVISE] Devise.apply_schema is deprecated and has no effect. Please remove it.\n"
223
+ end
224
+
225
+ # PRIVATE CONFIGURATION
226
+
227
+ # Store scopes mappings.
228
+ mattr_reader :mappings
229
+ @@mappings = ActiveSupport::OrderedHash.new
230
+
231
+ # Omniauth configurations.
232
+ mattr_reader :omniauth_configs
233
+ @@omniauth_configs = ActiveSupport::OrderedHash.new
234
+
235
+ # Define a set of modules that are called when a mapping is added.
236
+ mattr_reader :helpers
237
+ @@helpers = Set.new
238
+ @@helpers << Devise::Controllers::Helpers
239
+
240
+ # Private methods to interface with Warden.
241
+ mattr_accessor :warden_config
242
+ @@warden_config = nil
243
+ @@warden_config_block = nil
244
+
245
+ # When true, enter in paranoid mode to avoid user enumeration.
246
+ mattr_accessor :paranoid
247
+ @@paranoid = false
248
+
249
+ # Default way to setup Devise. Run rails generate devise_install to create
250
+ # a fresh initializer with all configuration values.
251
+ def self.setup
252
+ yield self
253
+ end
254
+
255
+ class Getter
256
+ def initialize name
257
+ @name = name
258
+ end
259
+
260
+ def get
261
+ ActiveSupport::Dependencies.constantize(@name)
262
+ end
263
+ end
264
+
265
+ def self.ref(arg)
266
+ if defined?(ActiveSupport::Dependencies::ClassCache)
267
+ ActiveSupport::Dependencies::reference(arg)
268
+ Getter.new(arg)
269
+ else
270
+ ActiveSupport::Dependencies.ref(arg)
271
+ end
272
+ end
273
+
274
+ def self.available_router_name
275
+ router_name || :main_app
276
+ end
277
+
278
+ def self.omniauth_providers
279
+ omniauth_configs.keys
280
+ end
281
+
282
+ # Get the mailer class from the mailer reference object.
283
+ def self.mailer
284
+ @@mailer_ref.get
285
+ end
286
+
287
+ # Set the mailer reference object to access the mailer.
288
+ def self.mailer=(class_name)
289
+ @@mailer_ref = ref(class_name)
290
+ end
291
+ self.mailer = "Devise::Mailer"
292
+
293
+ # Small method that adds a mapping to Devise.
294
+ def self.add_mapping(resource, options)
295
+ mapping = Devise::Mapping.new(resource, options)
296
+ @@mappings[mapping.name] = mapping
297
+ @@default_scope ||= mapping.name
298
+ @@helpers.each { |h| h.define_helpers(mapping) }
299
+ mapping
300
+ end
301
+
302
+ # Make Devise aware of an 3rd party Devise-module (like invitable). For convenience.
303
+ #
304
+ # == Options:
305
+ #
306
+ # +model+ - String representing the load path to a custom *model* for this module (to autoload.)
307
+ # +controller+ - Symbol representing the name of an exisiting or custom *controller* for this module.
308
+ # +route+ - Symbol representing the named *route* helper for this module.
309
+ # +strategy+ - Symbol representing if this module got a custom *strategy*.
310
+ #
311
+ # All values, except :model, accept also a boolean and will have the same name as the given module
312
+ # name.
313
+ #
314
+ # == Examples:
315
+ #
316
+ # Devise.add_module(:party_module)
317
+ # Devise.add_module(:party_module, :strategy => true, :controller => :sessions)
318
+ # Devise.add_module(:party_module, :model => 'party_module/model')
319
+ #
320
+ def self.add_module(module_name, options = {})
321
+ ALL << module_name
322
+ options.assert_valid_keys(:strategy, :model, :controller, :route, :no_input)
323
+
324
+ if strategy = options[:strategy]
325
+ strategy = (strategy == true ? module_name : strategy)
326
+ STRATEGIES[module_name] = strategy
327
+ end
328
+
329
+ if controller = options[:controller]
330
+ controller = (controller == true ? module_name : controller)
331
+ CONTROLLERS[module_name] = controller
332
+ end
333
+
334
+ NO_INPUT << strategy if options[:no_input]
335
+
336
+ if route = options[:route]
337
+ case route
338
+ when TrueClass
339
+ key, value = module_name, []
340
+ when Symbol
341
+ key, value = route, []
342
+ when Hash
343
+ key, value = route.keys.first, route.values.flatten
344
+ else
345
+ raise ArgumentError, ":route should be true, a Symbol or a Hash"
346
+ end
347
+
348
+ URL_HELPERS[key] ||= []
349
+ URL_HELPERS[key].concat(value)
350
+ URL_HELPERS[key].uniq!
351
+
352
+ ROUTES[module_name] = key
353
+ end
354
+
355
+ if options[:model]
356
+ path = (options[:model] == true ? "devise/models/#{module_name}" : options[:model])
357
+ camelized = ActiveSupport::Inflector.camelize(module_name.to_s)
358
+ Devise::Models.send(:autoload, camelized.to_sym, path)
359
+ end
360
+
361
+ Devise::Mapping.add_module module_name
362
+ end
363
+
364
+ # Sets warden configuration using a block that will be invoked on warden
365
+ # initialization.
366
+ #
367
+ # Devise.initialize do |config|
368
+ # config.allow_unconfirmed_access_for = 2.days
369
+ #
370
+ # config.warden do |manager|
371
+ # # Configure warden to use other strategies, like oauth.
372
+ # manager.oauth(:twitter)
373
+ # end
374
+ # end
375
+ def self.warden(&block)
376
+ @@warden_config_block = block
377
+ end
378
+
379
+ # Specify an omniauth provider.
380
+ #
381
+ # config.omniauth :github, APP_ID, APP_SECRET
382
+ #
383
+ def self.omniauth(provider, *args)
384
+ @@helpers << Devise::OmniAuth::UrlHelpers
385
+ config = Devise::OmniAuth::Config.new(provider, args)
386
+ @@omniauth_configs[config.strategy_name.to_sym] = config
387
+ end
388
+
389
+ # Include helpers in the given scope to AC and AV.
390
+ def self.include_helpers(scope)
391
+ ActiveSupport.on_load(:action_controller) do
392
+ include scope::Helpers if defined?(scope::Helpers)
393
+ include scope::UrlHelpers
394
+ end
395
+
396
+ ActiveSupport.on_load(:action_view) do
397
+ include scope::UrlHelpers
398
+ end
399
+ end
400
+
401
+ # Regenerates url helpers considering Devise.mapping
402
+ def self.regenerate_helpers!
403
+ Devise::Controllers::UrlHelpers.remove_helpers!
404
+ Devise::Controllers::UrlHelpers.generate_helpers!
405
+ end
406
+
407
+ # A method used internally to setup warden manager from the Rails initialize
408
+ # block.
409
+ def self.configure_warden! #:nodoc:
410
+ @@warden_configured ||= begin
411
+ warden_config.failure_app = Devise::Delegator.new
412
+ warden_config.default_scope = Devise.default_scope
413
+ warden_config.intercept_401 = false
414
+
415
+ Devise.mappings.each_value do |mapping|
416
+ warden_config.scope_defaults mapping.name, :strategies => mapping.strategies
417
+ end
418
+
419
+ @@warden_config_block.try :call, Devise.warden_config
420
+ true
421
+ end
422
+ end
423
+
424
+ # Generate a friendly string randomically to be used as token.
425
+ def self.friendly_token
426
+ SecureRandom.base64(15).tr('+/=lIO0', 'pqrsxyz')
427
+ end
428
+
429
+ # constant-time comparison algorithm to prevent timing attacks
430
+ def self.secure_compare(a, b)
431
+ return false if a.blank? || b.blank? || a.bytesize != b.bytesize
432
+ l = a.unpack "C#{a.bytesize}"
433
+
434
+ res = 0
435
+ b.each_byte { |byte| res |= byte ^ l.shift }
436
+ res == 0
437
+ end
438
+ end
439
+
440
+ require 'warden'
441
+ require 'devise/mapping'
442
+ require 'devise/models'
443
+ require 'devise/modules'
444
+ 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