cloudfoundry-devise 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (210) hide show
  1. data/.gitignore +12 -0
  2. data/.travis.yml +13 -0
  3. data/CHANGELOG.rdoc +755 -0
  4. data/Gemfile +35 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +366 -0
  7. data/Rakefile +34 -0
  8. data/app/controllers/devise/confirmations_controller.rb +46 -0
  9. data/app/controllers/devise/omniauth_callbacks_controller.rb +26 -0
  10. data/app/controllers/devise/passwords_controller.rb +50 -0
  11. data/app/controllers/devise/registrations_controller.rb +114 -0
  12. data/app/controllers/devise/sessions_controller.rb +49 -0
  13. data/app/controllers/devise/unlocks_controller.rb +34 -0
  14. data/app/helpers/devise_helper.rb +25 -0
  15. data/app/mailers/devise/mailer.rb +15 -0
  16. data/app/views/devise/confirmations/new.html.erb +12 -0
  17. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  18. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  19. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  20. data/app/views/devise/passwords/edit.html.erb +16 -0
  21. data/app/views/devise/passwords/new.html.erb +12 -0
  22. data/app/views/devise/registrations/edit.html.erb +25 -0
  23. data/app/views/devise/registrations/new.html.erb +18 -0
  24. data/app/views/devise/sessions/new.html.erb +17 -0
  25. data/app/views/devise/shared/_links.erb +25 -0
  26. data/app/views/devise/unlocks/new.html.erb +12 -0
  27. data/cloudfoundry-devise.gemspec +25 -0
  28. data/config/locales/en.yml +59 -0
  29. data/lib/devise.rb +453 -0
  30. data/lib/devise/controllers/helpers.rb +260 -0
  31. data/lib/devise/controllers/internal_helpers.rb +161 -0
  32. data/lib/devise/controllers/rememberable.rb +52 -0
  33. data/lib/devise/controllers/scoped_views.rb +33 -0
  34. data/lib/devise/controllers/shared_helpers.rb +26 -0
  35. data/lib/devise/controllers/url_helpers.rb +53 -0
  36. data/lib/devise/delegator.rb +16 -0
  37. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  38. data/lib/devise/encryptors/base.rb +20 -0
  39. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  40. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  41. data/lib/devise/encryptors/sha1.rb +25 -0
  42. data/lib/devise/encryptors/sha512.rb +25 -0
  43. data/lib/devise/failure_app.rb +149 -0
  44. data/lib/devise/hooks/activatable.rb +11 -0
  45. data/lib/devise/hooks/forgetable.rb +9 -0
  46. data/lib/devise/hooks/rememberable.rb +6 -0
  47. data/lib/devise/hooks/timeoutable.rb +24 -0
  48. data/lib/devise/hooks/trackable.rb +9 -0
  49. data/lib/devise/mailers/helpers.rb +86 -0
  50. data/lib/devise/mapping.rb +175 -0
  51. data/lib/devise/models.rb +91 -0
  52. data/lib/devise/models/authenticatable.rb +181 -0
  53. data/lib/devise/models/confirmable.rb +220 -0
  54. data/lib/devise/models/database_authenticatable.rb +122 -0
  55. data/lib/devise/models/encryptable.rb +72 -0
  56. data/lib/devise/models/lockable.rb +169 -0
  57. data/lib/devise/models/omniauthable.rb +23 -0
  58. data/lib/devise/models/recoverable.rb +136 -0
  59. data/lib/devise/models/registerable.rb +21 -0
  60. data/lib/devise/models/rememberable.rb +114 -0
  61. data/lib/devise/models/serializable.rb +43 -0
  62. data/lib/devise/models/timeoutable.rb +45 -0
  63. data/lib/devise/models/token_authenticatable.rb +72 -0
  64. data/lib/devise/models/trackable.rb +30 -0
  65. data/lib/devise/models/validatable.rb +62 -0
  66. data/lib/devise/modules.rb +30 -0
  67. data/lib/devise/omniauth.rb +28 -0
  68. data/lib/devise/omniauth/config.rb +45 -0
  69. data/lib/devise/omniauth/url_helpers.rb +33 -0
  70. data/lib/devise/orm/active_record.rb +44 -0
  71. data/lib/devise/orm/mongoid.rb +31 -0
  72. data/lib/devise/param_filter.rb +41 -0
  73. data/lib/devise/path_checker.rb +18 -0
  74. data/lib/devise/rails.rb +73 -0
  75. data/lib/devise/rails/routes.rb +385 -0
  76. data/lib/devise/rails/warden_compat.rb +120 -0
  77. data/lib/devise/schema.rb +109 -0
  78. data/lib/devise/strategies/authenticatable.rb +155 -0
  79. data/lib/devise/strategies/base.rb +15 -0
  80. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  81. data/lib/devise/strategies/rememberable.rb +53 -0
  82. data/lib/devise/strategies/token_authenticatable.rb +57 -0
  83. data/lib/devise/test_helpers.rb +90 -0
  84. data/lib/devise/version.rb +3 -0
  85. data/lib/generators/active_record/devise_generator.rb +71 -0
  86. data/lib/generators/active_record/templates/migration.rb +29 -0
  87. data/lib/generators/active_record/templates/migration_existing.rb +26 -0
  88. data/lib/generators/devise/devise_generator.rb +22 -0
  89. data/lib/generators/devise/install_generator.rb +24 -0
  90. data/lib/generators/devise/orm_helpers.rb +31 -0
  91. data/lib/generators/devise/views_generator.rb +98 -0
  92. data/lib/generators/mongoid/devise_generator.rb +60 -0
  93. data/lib/generators/templates/README +32 -0
  94. data/lib/generators/templates/devise.rb +215 -0
  95. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  96. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  97. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  98. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
  99. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  100. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  101. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
  102. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  103. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  104. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
  105. data/test/controllers/helpers_test.rb +254 -0
  106. data/test/controllers/internal_helpers_test.rb +96 -0
  107. data/test/controllers/sessions_controller_test.rb +16 -0
  108. data/test/controllers/url_helpers_test.rb +59 -0
  109. data/test/delegator_test.rb +19 -0
  110. data/test/devise_test.rb +72 -0
  111. data/test/encryptors_test.rb +30 -0
  112. data/test/failure_app_test.rb +207 -0
  113. data/test/generators/active_record_generator_test.rb +47 -0
  114. data/test/generators/devise_generator_test.rb +39 -0
  115. data/test/generators/install_generator_test.rb +13 -0
  116. data/test/generators/mongoid_generator_test.rb +23 -0
  117. data/test/generators/views_generator_test.rb +52 -0
  118. data/test/helpers/devise_helper_test.rb +51 -0
  119. data/test/indifferent_hash.rb +33 -0
  120. data/test/integration/authenticatable_test.rb +590 -0
  121. data/test/integration/confirmable_test.rb +262 -0
  122. data/test/integration/database_authenticatable_test.rb +82 -0
  123. data/test/integration/http_authenticatable_test.rb +82 -0
  124. data/test/integration/lockable_test.rb +212 -0
  125. data/test/integration/omniauthable_test.rb +133 -0
  126. data/test/integration/recoverable_test.rb +287 -0
  127. data/test/integration/registerable_test.rb +335 -0
  128. data/test/integration/rememberable_test.rb +158 -0
  129. data/test/integration/timeoutable_test.rb +98 -0
  130. data/test/integration/token_authenticatable_test.rb +148 -0
  131. data/test/integration/trackable_test.rb +92 -0
  132. data/test/mailers/confirmation_instructions_test.rb +95 -0
  133. data/test/mailers/reset_password_instructions_test.rb +83 -0
  134. data/test/mailers/unlock_instructions_test.rb +77 -0
  135. data/test/mapping_test.rb +128 -0
  136. data/test/models/confirmable_test.rb +334 -0
  137. data/test/models/database_authenticatable_test.rb +167 -0
  138. data/test/models/encryptable_test.rb +67 -0
  139. data/test/models/lockable_test.rb +225 -0
  140. data/test/models/recoverable_test.rb +198 -0
  141. data/test/models/rememberable_test.rb +168 -0
  142. data/test/models/serializable_test.rb +38 -0
  143. data/test/models/timeoutable_test.rb +42 -0
  144. data/test/models/token_authenticatable_test.rb +49 -0
  145. data/test/models/trackable_test.rb +5 -0
  146. data/test/models/validatable_test.rb +113 -0
  147. data/test/models_test.rb +109 -0
  148. data/test/omniauth/config_test.rb +57 -0
  149. data/test/omniauth/url_helpers_test.rb +58 -0
  150. data/test/orm/active_record.rb +9 -0
  151. data/test/orm/mongoid.rb +14 -0
  152. data/test/rails_app/Rakefile +10 -0
  153. data/test/rails_app/app/active_record/admin.rb +6 -0
  154. data/test/rails_app/app/active_record/shim.rb +2 -0
  155. data/test/rails_app/app/active_record/user.rb +6 -0
  156. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  157. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  158. data/test/rails_app/app/controllers/application_controller.rb +8 -0
  159. data/test/rails_app/app/controllers/home_controller.rb +25 -0
  160. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  161. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  162. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +14 -0
  163. data/test/rails_app/app/controllers/users_controller.rb +23 -0
  164. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  165. data/test/rails_app/app/mailers/users/mailer.rb +3 -0
  166. data/test/rails_app/app/mongoid/admin.rb +24 -0
  167. data/test/rails_app/app/mongoid/shim.rb +24 -0
  168. data/test/rails_app/app/mongoid/user.rb +45 -0
  169. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  170. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  171. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  172. data/test/rails_app/app/views/home/index.html.erb +1 -0
  173. data/test/rails_app/app/views/home/join.html.erb +1 -0
  174. data/test/rails_app/app/views/home/private.html.erb +1 -0
  175. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  176. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  177. data/test/rails_app/app/views/users/index.html.erb +1 -0
  178. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  179. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  180. data/test/rails_app/config.ru +4 -0
  181. data/test/rails_app/config/application.rb +41 -0
  182. data/test/rails_app/config/boot.rb +8 -0
  183. data/test/rails_app/config/database.yml +18 -0
  184. data/test/rails_app/config/environment.rb +5 -0
  185. data/test/rails_app/config/environments/development.rb +18 -0
  186. data/test/rails_app/config/environments/production.rb +33 -0
  187. data/test/rails_app/config/environments/test.rb +33 -0
  188. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  189. data/test/rails_app/config/initializers/devise.rb +197 -0
  190. data/test/rails_app/config/initializers/inflections.rb +2 -0
  191. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  192. data/test/rails_app/config/routes.rb +87 -0
  193. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +71 -0
  194. data/test/rails_app/db/schema.rb +52 -0
  195. data/test/rails_app/lib/shared_admin.rb +10 -0
  196. data/test/rails_app/lib/shared_user.rb +26 -0
  197. data/test/rails_app/public/404.html +26 -0
  198. data/test/rails_app/public/422.html +26 -0
  199. data/test/rails_app/public/500.html +26 -0
  200. data/test/rails_app/public/favicon.ico +0 -0
  201. data/test/rails_app/script/rails +10 -0
  202. data/test/routes_test.rb +240 -0
  203. data/test/support/assertions.rb +27 -0
  204. data/test/support/helpers.rb +109 -0
  205. data/test/support/integration.rb +88 -0
  206. data/test/support/locale/en.yml +4 -0
  207. data/test/support/webrat/integrations/rails.rb +24 -0
  208. data/test/test_helper.rb +27 -0
  209. data/test/test_helpers_test.rb +134 -0
  210. metadata +295 -0
@@ -0,0 +1,23 @@
1
+ require 'devise/omniauth'
2
+
3
+ module Devise
4
+ module Models
5
+ # Adds OmniAuth support to your model.
6
+ #
7
+ # == Options
8
+ #
9
+ # Oauthable adds the following options to devise_for:
10
+ #
11
+ # * +omniauth_providers+: Which providers are avaialble to this model. It expects an array:
12
+ #
13
+ # devise_for :database_authenticatable, :omniauthable, :omniauth_providers => [:twitter]
14
+ #
15
+ module Omniauthable
16
+ extend ActiveSupport::Concern
17
+
18
+ module ClassMethods
19
+ Devise::Models.config(self, :omniauth_providers)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,136 @@
1
+ module Devise
2
+ module Models
3
+
4
+ # Recoverable takes care of reseting the user password and send reset instructions.
5
+ #
6
+ # ==Options
7
+ #
8
+ # Recoverable adds the following options to devise_for:
9
+ #
10
+ # * +reset_password_keys+: the keys you want to use when recovering the password for an account
11
+ #
12
+ # == Examples
13
+ #
14
+ # # resets the user password and save the record, true if valid passwords are given, otherwise false
15
+ # User.find(1).reset_password!('password123', 'password123')
16
+ #
17
+ # # only resets the user password, without saving the record
18
+ # user = User.find(1)
19
+ # user.reset_password('password123', 'password123')
20
+ #
21
+ # # creates a new token and send it with instructions about how to reset the password
22
+ # User.find(1).send_reset_password_instructions
23
+ #
24
+ module Recoverable
25
+ extend ActiveSupport::Concern
26
+
27
+ # Update password saving the record and clearing token. Returns true if
28
+ # the passwords are valid and the record was saved, false otherwise.
29
+ def reset_password!(new_password, new_password_confirmation)
30
+ self.password = new_password
31
+ self.password_confirmation = new_password_confirmation
32
+
33
+ if valid?
34
+ clear_reset_password_token
35
+ after_password_reset
36
+ end
37
+
38
+ save
39
+ end
40
+
41
+ # Resets reset password token and send reset password instructions by email
42
+ def send_reset_password_instructions
43
+ generate_reset_password_token! if should_generate_token?
44
+ self.devise_mailer.reset_password_instructions(self).deliver
45
+ end
46
+
47
+ # Checks if the reset password token sent is within the limit time.
48
+ # We do this by calculating if the difference between today and the
49
+ # sending date does not exceed the confirm in time configured.
50
+ # Returns true if the resource is not responding to reset_password_sent_at at all.
51
+ # reset_password_within is a model configuration, must always be an integer value.
52
+ #
53
+ # Example:
54
+ #
55
+ # # reset_password_within = 1.day and reset_password_sent_at = today
56
+ # reset_password_period_valid? # returns true
57
+ #
58
+ # # reset_password_within = 5.days and reset_password_sent_at = 4.days.ago
59
+ # reset_password_period_valid? # returns true
60
+ #
61
+ # # reset_password_within = 5.days and reset_password_sent_at = 5.days.ago
62
+ # reset_password_period_valid? # returns false
63
+ #
64
+ # # reset_password_within = 0.days
65
+ # reset_password_period_valid? # will always return false
66
+ #
67
+ def reset_password_period_valid?
68
+ reset_password_sent_at && reset_password_sent_at.utc >= self.class.reset_password_within.ago
69
+ end
70
+
71
+ protected
72
+
73
+ def should_generate_token?
74
+ reset_password_token.nil? || !reset_password_period_valid?
75
+ end
76
+
77
+ # Generates a new random token for reset password
78
+ def generate_reset_password_token
79
+ self.reset_password_token = self.class.reset_password_token
80
+ self.reset_password_sent_at = Time.now.utc
81
+ self.reset_password_token
82
+ end
83
+
84
+ # Resets the reset password token with and save the record without
85
+ # validating
86
+ def generate_reset_password_token!
87
+ generate_reset_password_token && save(:validate => false)
88
+ end
89
+
90
+ # Removes reset_password token
91
+ def clear_reset_password_token
92
+ self.reset_password_token = nil
93
+ self.reset_password_sent_at = nil
94
+ end
95
+
96
+ def after_password_reset
97
+ end
98
+
99
+ module ClassMethods
100
+ # Attempt to find a user by its email. If a record is found, send new
101
+ # password instructions to it. If not user is found, returns a new user
102
+ # with an email not found error.
103
+ # Attributes must contain the user email
104
+ def send_reset_password_instructions(attributes={})
105
+ recoverable = find_or_initialize_with_errors(reset_password_keys, attributes, :not_found)
106
+ recoverable.send_reset_password_instructions if recoverable.persisted?
107
+ recoverable
108
+ end
109
+
110
+ # Generate a token checking if one does not already exist in the database.
111
+ def reset_password_token
112
+ generate_token(:reset_password_token)
113
+ end
114
+
115
+ # Attempt to find a user by its reset_password_token to reset its
116
+ # password. If a user is found and token is still valid, reset its password and automatically
117
+ # try saving the record. If not user is found, returns a new user
118
+ # containing an error in reset_password_token attribute.
119
+ # Attributes must contain reset_password_token, password and confirmation
120
+ def reset_password_by_token(attributes={})
121
+ recoverable = find_or_initialize_with_error_by(:reset_password_token, attributes[:reset_password_token])
122
+ if recoverable.persisted?
123
+ if recoverable.reset_password_period_valid?
124
+ recoverable.reset_password!(attributes[:password], attributes[:password_confirmation])
125
+ else
126
+ recoverable.errors.add(:reset_password_token, :expired)
127
+ end
128
+ end
129
+ recoverable
130
+ end
131
+
132
+ Devise::Models.config(self, :reset_password_keys, :reset_password_within)
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,21 @@
1
+ module Devise
2
+ module Models
3
+ # Registerable is responsible for everything related to registering a new
4
+ # resource (ie user sign up).
5
+ module Registerable
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ # A convenience method that receives both parameters and session to
10
+ # initialize a user. This can be used by OAuth, for example, to send
11
+ # in the user token and be stored on initialization.
12
+ #
13
+ # By default discards all information sent by the session by calling
14
+ # new with params.
15
+ def new_with_session(params, session)
16
+ new(params)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,114 @@
1
+ require 'devise/strategies/rememberable'
2
+ require 'devise/hooks/rememberable'
3
+ require 'devise/hooks/forgetable'
4
+
5
+ module Devise
6
+ module Models
7
+ # Rememberable manages generating and clearing token for remember the user
8
+ # from a saved cookie. Rememberable also has utility methods for dealing
9
+ # with serializing the user into the cookie and back from the cookie, trying
10
+ # to lookup the record based on the saved information.
11
+ # You probably wouldn't use rememberable methods directly, they are used
12
+ # mostly internally for handling the remember token.
13
+ #
14
+ # == Options
15
+ #
16
+ # Rememberable adds the following options in devise_for:
17
+ #
18
+ # * +remember_for+: the time you want the user will be remembered without
19
+ # asking for credentials. After this time the user will be blocked and
20
+ # will have to enter his credentials again. This configuration is also
21
+ # used to calculate the expires time for the cookie created to remember
22
+ # the user. By default remember_for is 2.weeks.
23
+ #
24
+ # * +extend_remember_period+: if true, extends the user's remember period
25
+ # when remembered via cookie. False by default.
26
+ #
27
+ # * +cookie_options+: configuration options passed to the created cookie.
28
+ #
29
+ # == Examples
30
+ #
31
+ # User.find(1).remember_me! # regenerating the token
32
+ # User.find(1).forget_me! # clearing the token
33
+ #
34
+ # # generating info to put into cookies
35
+ # User.serialize_into_cookie(user)
36
+ #
37
+ # # lookup the user based on the incoming cookie information
38
+ # User.serialize_from_cookie(cookie_string)
39
+ module Rememberable
40
+ extend ActiveSupport::Concern
41
+
42
+ attr_accessor :remember_me, :extend_remember_period
43
+
44
+ # Generate a new remember token and save the record without validations
45
+ # unless remember_across_browsers is true and the user already has a valid token.
46
+ def remember_me!(extend_period=false)
47
+ self.remember_created_at = Time.now.utc if generate_remember_timestamp?(extend_period)
48
+ save(:validate => false)
49
+ end
50
+
51
+ # If the record is persisted, remove the remember token (but only if
52
+ # it exists), and save the record without validations.
53
+ def forget_me!
54
+ if persisted?
55
+ self.remember_token = nil if respond_to?(:remember_token=)
56
+ self.remember_created_at = nil
57
+ save(:validate => false)
58
+ end
59
+ end
60
+
61
+ # Remember token should be expired if expiration time not overpass now.
62
+ def remember_expired?
63
+ remember_created_at.nil? || (remember_expires_at <= Time.now.utc)
64
+ end
65
+
66
+ # Remember token expires at created time + remember_for configuration
67
+ def remember_expires_at
68
+ remember_created_at + self.class.remember_for
69
+ end
70
+
71
+ def rememberable_value
72
+ if respond_to?(:authenticatable_salt) && (salt = authenticatable_salt)
73
+ salt
74
+ else
75
+ raise "The #{self.class.name} class does not respond to remember_token and " <<
76
+ "authenticatable_salt returns nil. In order to use rememberable, you must " <<
77
+ "add a remember_token field to your model or ensure a password is always set."
78
+ end
79
+ end
80
+
81
+ def cookie_options
82
+ self.class.cookie_options
83
+ end
84
+
85
+ protected
86
+
87
+ # Generate a timestamp if extend_remember_period is true, if no remember_token
88
+ # exists, or if an existing remember token has expired.
89
+ def generate_remember_timestamp?(extend_period) #:nodoc:
90
+ extend_period || remember_created_at.nil? || remember_expired?
91
+ end
92
+
93
+ module ClassMethods
94
+ # Create the cookie key using the record id and remember_token
95
+ def serialize_into_cookie(record)
96
+ [record.to_key, record.rememberable_value]
97
+ end
98
+
99
+ # Recreate the user based on the stored cookie
100
+ def serialize_from_cookie(id, remember_token)
101
+ record = to_adapter.get(id)
102
+ record if record && record.rememberable_value == remember_token && !record.remember_expired?
103
+ end
104
+
105
+ # Generate a token checking if one does not already exist in the database.
106
+ def remember_token
107
+ generate_token(:remember_token)
108
+ end
109
+
110
+ Devise::Models.config(self, :remember_for, :extend_remember_period, :cookie_options)
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,43 @@
1
+ module Devise
2
+ module Models
3
+ # This module redefine to_xml and serializable_hash in models for more
4
+ # secure defaults. By default, it removes from the serializable model
5
+ # all attributes that are *not* accessible. You can remove this default
6
+ # by using :force_except and passing a new list of attributes you want
7
+ # to exempt. All attributes given to :except will simply add names to
8
+ # exempt to Devise internal list.
9
+ module Serializable
10
+ extend ActiveSupport::Concern
11
+
12
+ # TODO: to_xml does not call serializable_hash. Hopefully someone will fix this in AR.
13
+ %w(to_xml serializable_hash).each do |method|
14
+ class_eval <<-RUBY, __FILE__, __LINE__
15
+ def #{method}(options=nil)
16
+ options ||= {}
17
+ if options.key?(:force_except)
18
+ options[:except] = options.delete(:force_except)
19
+ super(options)
20
+ elsif self.class.blacklist_keys?
21
+ except = Array(options[:except])
22
+ super(options.merge(:except => except + self.class.blacklist_keys))
23
+ else
24
+ super
25
+ end
26
+ end
27
+ RUBY
28
+ end
29
+
30
+ module ClassMethods
31
+ # Return true if we can retrieve blacklist keys from the record.
32
+ def blacklist_keys?
33
+ @has_except_keys ||= respond_to?(:accessible_attributes) && !accessible_attributes.to_a.empty?
34
+ end
35
+
36
+ # Returns keys that should be removed when serializing the record.
37
+ def blacklist_keys
38
+ @blacklist_keys ||= to_adapter.column_names.map(&:to_s) - accessible_attributes.to_a.map(&:to_s)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,45 @@
1
+ require 'devise/hooks/timeoutable'
2
+
3
+ module Devise
4
+ module Models
5
+ # Timeoutable takes care of veryfing whether a user session has already
6
+ # expired or not. When a session expires after the configured time, the user
7
+ # will be asked for credentials again, it means, he/she will be redirected
8
+ # to the sign in page.
9
+ #
10
+ # == Options
11
+ #
12
+ # Timeoutable adds the following options to devise_for:
13
+ #
14
+ # * +timeout_in+: the interval to timeout the user session without activity.
15
+ #
16
+ # == Examples
17
+ #
18
+ # user.timedout?(30.minutes.ago)
19
+ #
20
+ module Timeoutable
21
+ extend ActiveSupport::Concern
22
+
23
+ # Checks whether the user session has expired based on configured time.
24
+ def timedout?(last_access)
25
+ return false if remember_exists_and_not_expired?
26
+ !timeout_in.nil? && last_access && last_access <= timeout_in.ago
27
+ end
28
+
29
+ def timeout_in
30
+ self.class.timeout_in
31
+ end
32
+
33
+ private
34
+
35
+ def remember_exists_and_not_expired?
36
+ return false unless respond_to?(:remember_created_at)
37
+ remember_created_at && !remember_expired?
38
+ end
39
+
40
+ module ClassMethods
41
+ Devise::Models.config(self, :timeout_in)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,72 @@
1
+ require 'devise/strategies/token_authenticatable'
2
+
3
+ module Devise
4
+ module Models
5
+ # The TokenAuthenticatable module is responsible for generating an authentication token and
6
+ # validating the authenticity of the same while signing in.
7
+ #
8
+ # This module only provides a few helpers to help you manage the token, but it is up to you
9
+ # to choose how to use it. For example, if you want to have a new token every time the user
10
+ # saves his account, you can do the following:
11
+ #
12
+ # before_save :reset_authentication_token
13
+ #
14
+ # On the other hand, if you want to generate token unless one exists, you should use instead:
15
+ #
16
+ # before_save :ensure_authentication_token
17
+ #
18
+ # If you want to delete the token after it is used, you can do so in the
19
+ # after_token_authentication callback.
20
+ #
21
+ # == Options
22
+ #
23
+ # TokenAuthenticatable adds the following options to devise_for:
24
+ #
25
+ # * +token_authentication_key+: Defines name of the authentication token params key. E.g. /users/sign_in?some_key=...
26
+ #
27
+ # * +stateless_token+: By default, when you sign up with a token, Devise will store the user in session
28
+ # as any other authentication strategy. You can set stateless_token to true to avoid this.
29
+ #
30
+ module TokenAuthenticatable
31
+ extend ActiveSupport::Concern
32
+
33
+ # Generate new authentication token (a.k.a. "single access token").
34
+ def reset_authentication_token
35
+ self.authentication_token = self.class.authentication_token
36
+ end
37
+
38
+ # Generate new authentication token and save the record.
39
+ def reset_authentication_token!
40
+ reset_authentication_token
41
+ save(:validate => false)
42
+ end
43
+
44
+ # Generate authentication token unless already exists.
45
+ def ensure_authentication_token
46
+ reset_authentication_token if authentication_token.blank?
47
+ end
48
+
49
+ # Generate authentication token unless already exists and save the record.
50
+ def ensure_authentication_token!
51
+ reset_authentication_token! if authentication_token.blank?
52
+ end
53
+
54
+ # Hook called after token authentication.
55
+ def after_token_authentication
56
+ end
57
+
58
+ module ClassMethods
59
+ def find_for_token_authentication(conditions)
60
+ find_for_authentication(:authentication_token => conditions[token_authentication_key])
61
+ end
62
+
63
+ # Generate a token checking if one does not already exist in the database.
64
+ def authentication_token
65
+ generate_token(:authentication_token)
66
+ end
67
+
68
+ ::Devise::Models.config(self, :token_authentication_key, :stateless_token)
69
+ end
70
+ end
71
+ end
72
+ end