quo_vadis 1.4.2 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (191) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +11 -7
  3. data/CHANGELOG.md +43 -0
  4. data/Gemfile +11 -1
  5. data/LICENSE.txt +21 -0
  6. data/README.md +460 -127
  7. data/Rakefile +15 -9
  8. data/app/controllers/quo_vadis/confirmations_controller.rb +102 -0
  9. data/app/controllers/quo_vadis/logs_controller.rb +20 -0
  10. data/app/controllers/quo_vadis/password_resets_controller.rb +68 -0
  11. data/app/controllers/quo_vadis/passwords_controller.rb +28 -0
  12. data/app/controllers/quo_vadis/recovery_codes_controller.rb +54 -0
  13. data/app/controllers/quo_vadis/sessions_controller.rb +50 -132
  14. data/app/controllers/quo_vadis/totps_controller.rb +72 -0
  15. data/app/controllers/quo_vadis/twofas_controller.rb +26 -0
  16. data/app/mailers/quo_vadis/mailer.rb +73 -0
  17. data/app/models/quo_vadis/account.rb +72 -0
  18. data/app/models/quo_vadis/account_confirmation_token.rb +17 -0
  19. data/app/models/quo_vadis/log.rb +59 -0
  20. data/app/models/quo_vadis/password.rb +68 -0
  21. data/app/models/quo_vadis/password_reset_token.rb +17 -0
  22. data/app/models/quo_vadis/recovery_code.rb +26 -0
  23. data/app/models/quo_vadis/session.rb +55 -0
  24. data/app/models/quo_vadis/token.rb +42 -0
  25. data/app/models/quo_vadis/totp.rb +56 -0
  26. data/app/views/quo_vadis/confirmations/edit.html.erb +10 -0
  27. data/app/views/quo_vadis/confirmations/edit_email.html.erb +14 -0
  28. data/app/views/quo_vadis/confirmations/index.html.erb +14 -0
  29. data/app/views/quo_vadis/confirmations/new.html.erb +16 -0
  30. data/app/views/quo_vadis/logs/index.html.erb +30 -0
  31. data/app/views/quo_vadis/mailer/account_confirmation.text.erb +4 -0
  32. data/app/views/quo_vadis/mailer/email_change_notification.text.erb +8 -0
  33. data/app/views/quo_vadis/mailer/identifier_change_notification.text.erb +8 -0
  34. data/app/views/quo_vadis/mailer/password_change_notification.text.erb +8 -0
  35. data/app/views/quo_vadis/mailer/password_reset_notification.text.erb +8 -0
  36. data/app/views/quo_vadis/mailer/recovery_codes_generation_notification.text.erb +8 -0
  37. data/app/views/quo_vadis/mailer/reset_password.text.erb +4 -0
  38. data/app/views/quo_vadis/mailer/totp_reuse_notification.text.erb +6 -0
  39. data/app/views/quo_vadis/mailer/totp_setup_notification.text.erb +8 -0
  40. data/app/views/quo_vadis/mailer/twofa_deactivated_notification.text.erb +8 -0
  41. data/app/views/quo_vadis/password_resets/edit.html.erb +17 -0
  42. data/app/views/quo_vadis/password_resets/index.html.erb +5 -0
  43. data/app/views/quo_vadis/password_resets/new.html.erb +12 -0
  44. data/app/views/quo_vadis/passwords/edit.html.erb +22 -0
  45. data/app/views/quo_vadis/recovery_codes/challenge.html.erb +11 -0
  46. data/app/views/quo_vadis/recovery_codes/index.html.erb +25 -0
  47. data/app/views/quo_vadis/sessions/index.html.erb +26 -0
  48. data/app/views/quo_vadis/sessions/new.html.erb +24 -0
  49. data/app/views/quo_vadis/totps/challenge.html.erb +11 -0
  50. data/app/views/quo_vadis/totps/new.html.erb +17 -0
  51. data/app/views/quo_vadis/twofas/show.html.erb +20 -0
  52. data/bin/console +15 -0
  53. data/bin/rails +21 -0
  54. data/bin/setup +8 -0
  55. data/config/locales/quo_vadis.en.yml +80 -24
  56. data/config/routes.rb +46 -12
  57. data/db/migrate/202102150904_setup.rb +48 -0
  58. data/lib/generators/quo_vadis/install_generator.rb +4 -23
  59. data/lib/quo_vadis.rb +103 -97
  60. data/lib/quo_vadis/controller.rb +227 -0
  61. data/lib/quo_vadis/crypt.rb +43 -0
  62. data/lib/quo_vadis/current_request_details.rb +11 -0
  63. data/lib/quo_vadis/defaults.rb +18 -0
  64. data/lib/quo_vadis/encrypted_type.rb +17 -0
  65. data/lib/quo_vadis/engine.rb +9 -11
  66. data/lib/quo_vadis/hmacable.rb +26 -0
  67. data/lib/quo_vadis/ip_masking.rb +31 -0
  68. data/lib/quo_vadis/model.rb +83 -0
  69. data/lib/quo_vadis/version.rb +3 -1
  70. data/quo_vadis.gemspec +20 -25
  71. data/test/dummy/README.markdown +1 -0
  72. data/test/dummy/Rakefile +2 -6
  73. data/test/dummy/app/controllers/application_controller.rb +0 -1
  74. data/test/dummy/app/controllers/articles_controller.rb +8 -11
  75. data/test/dummy/app/controllers/sign_ups_controller.rb +42 -0
  76. data/test/dummy/app/controllers/users_controller.rb +13 -5
  77. data/test/dummy/app/models/application_record.rb +3 -0
  78. data/test/dummy/app/models/article.rb +2 -1
  79. data/test/dummy/app/models/person.rb +5 -2
  80. data/test/dummy/app/models/user.rb +4 -1
  81. data/test/dummy/app/views/articles/also_secret.html.erb +1 -0
  82. data/test/dummy/app/views/articles/index.html.erb +1 -1
  83. data/test/dummy/app/views/articles/secret.html.erb +1 -0
  84. data/test/dummy/app/views/articles/very_secret.html.erb +2 -0
  85. data/test/dummy/app/views/layouts/application.html.erb +41 -25
  86. data/test/dummy/app/views/sign_ups/new.html.erb +37 -0
  87. data/test/dummy/app/views/sign_ups/show.html.erb +5 -0
  88. data/test/dummy/app/views/users/new.html.erb +32 -9
  89. data/test/dummy/config.ru +6 -3
  90. data/test/dummy/config/application.rb +18 -9
  91. data/test/dummy/config/boot.rb +3 -9
  92. data/test/dummy/config/database.yml +2 -14
  93. data/test/dummy/config/environment.rb +2 -3
  94. data/test/dummy/config/initializers/quo_vadis.rb +2 -76
  95. data/test/dummy/config/routes.rb +11 -3
  96. data/test/dummy/db/migrate/202102121932_create_users.rb +10 -0
  97. data/test/dummy/db/migrate/202102121935_create_people.rb +10 -0
  98. data/test/dummy/db/schema.rb +80 -21
  99. data/test/fixtures/quo_vadis/mailer/account_confirmation.text +4 -0
  100. data/test/fixtures/quo_vadis/mailer/email_change_notification.text +8 -0
  101. data/test/fixtures/quo_vadis/mailer/identifier_change_notification.text +8 -0
  102. data/test/fixtures/quo_vadis/mailer/password_change_notification.text +8 -0
  103. data/test/fixtures/quo_vadis/mailer/password_reset_notification.text +8 -0
  104. data/test/fixtures/quo_vadis/mailer/recovery_codes_generation_notification.text +8 -0
  105. data/test/fixtures/quo_vadis/mailer/reset_password.text +4 -0
  106. data/test/fixtures/quo_vadis/mailer/totp_reuse_notification.text +6 -0
  107. data/test/fixtures/quo_vadis/mailer/totp_setup_notification.text +8 -0
  108. data/test/fixtures/quo_vadis/mailer/twofa_deactivated_notification.text +8 -0
  109. data/test/integration/account_confirmation_test.rb +145 -0
  110. data/test/integration/controller_test.rb +280 -0
  111. data/test/integration/logging_test.rb +244 -0
  112. data/test/integration/password_change_test.rb +99 -0
  113. data/test/integration/password_login_test.rb +137 -0
  114. data/test/integration/password_reset_test.rb +136 -0
  115. data/test/integration/recovery_codes_test.rb +48 -0
  116. data/test/integration/sessions_test.rb +86 -0
  117. data/test/integration/sign_up_test.rb +26 -12
  118. data/test/integration/totps_test.rb +96 -0
  119. data/test/integration/twofa_test.rb +82 -0
  120. data/test/mailers/mailer_test.rb +200 -0
  121. data/test/models/account_test.rb +50 -0
  122. data/test/models/crypt_test.rb +22 -0
  123. data/test/models/log_test.rb +16 -0
  124. data/test/models/mask_ip_test.rb +27 -0
  125. data/test/models/model_test.rb +66 -0
  126. data/test/models/password_test.rb +179 -0
  127. data/test/models/recovery_code_test.rb +54 -0
  128. data/test/models/session_test.rb +67 -0
  129. data/test/models/token_test.rb +70 -0
  130. data/test/models/totp_test.rb +68 -0
  131. data/test/quo_vadis_test.rb +39 -3
  132. data/test/test_helper.rb +42 -70
  133. metadata +120 -204
  134. data/app/controllers/controller_mixin.rb +0 -109
  135. data/app/mailers/quo_vadis/notifier.rb +0 -30
  136. data/app/models/model_mixin.rb +0 -128
  137. data/lib/generators/quo_vadis/templates/migration.rb.erb +0 -18
  138. data/lib/generators/quo_vadis/templates/quo_vadis.rb.erb +0 -96
  139. data/test/dummy/.gitignore +0 -2
  140. data/test/dummy/app/helpers/application_helper.rb +0 -2
  141. data/test/dummy/app/helpers/articles_helper.rb +0 -2
  142. data/test/dummy/app/views/articles/new.html.erb +0 -11
  143. data/test/dummy/app/views/layouts/sessions.html.erb +0 -3
  144. data/test/dummy/app/views/quo_vadis/notifier/change_password.text.erb +0 -9
  145. data/test/dummy/app/views/quo_vadis/notifier/invite.text.erb +0 -8
  146. data/test/dummy/app/views/sessions/edit.html.erb +0 -11
  147. data/test/dummy/app/views/sessions/forgotten.html.erb +0 -13
  148. data/test/dummy/app/views/sessions/invite.html.erb +0 -31
  149. data/test/dummy/app/views/sessions/new.html.erb +0 -15
  150. data/test/dummy/config/environments/development.rb +0 -26
  151. data/test/dummy/config/environments/production.rb +0 -49
  152. data/test/dummy/config/environments/test.rb +0 -37
  153. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  154. data/test/dummy/config/initializers/inflections.rb +0 -10
  155. data/test/dummy/config/initializers/mime_types.rb +0 -5
  156. data/test/dummy/config/initializers/rack_patch.rb +0 -16
  157. data/test/dummy/config/initializers/secret_token.rb +0 -7
  158. data/test/dummy/config/initializers/session_store.rb +0 -8
  159. data/test/dummy/config/locales/en.yml +0 -5
  160. data/test/dummy/config/locales/quo_vadis.en.yml +0 -21
  161. data/test/dummy/db/migrate/20110124125037_create_users.rb +0 -13
  162. data/test/dummy/db/migrate/20110124131535_create_articles.rb +0 -14
  163. data/test/dummy/db/migrate/20110127094709_add_authentication_to_users.rb +0 -18
  164. data/test/dummy/db/migrate/20111004112209_create_people.rb +0 -13
  165. data/test/dummy/db/migrate/20111004132342_add_authentication_to_people.rb +0 -18
  166. data/test/dummy/public/404.html +0 -26
  167. data/test/dummy/public/422.html +0 -26
  168. data/test/dummy/public/500.html +0 -26
  169. data/test/dummy/public/javascripts/application.js +0 -2
  170. data/test/dummy/public/javascripts/controls.js +0 -965
  171. data/test/dummy/public/javascripts/dragdrop.js +0 -974
  172. data/test/dummy/public/javascripts/effects.js +0 -1123
  173. data/test/dummy/public/javascripts/prototype.js +0 -6001
  174. data/test/dummy/public/javascripts/rails.js +0 -175
  175. data/test/dummy/public/stylesheets/.gitkeep +0 -0
  176. data/test/dummy/script/rails +0 -6
  177. data/test/integration/activation_test.rb +0 -108
  178. data/test/integration/authenticate_test.rb +0 -39
  179. data/test/integration/blocked_test.rb +0 -23
  180. data/test/integration/config_test.rb +0 -118
  181. data/test/integration/cookie_test.rb +0 -67
  182. data/test/integration/csrf_test.rb +0 -41
  183. data/test/integration/forgotten_test.rb +0 -93
  184. data/test/integration/helper_test.rb +0 -18
  185. data/test/integration/locale_test.rb +0 -197
  186. data/test/integration/navigation_test.rb +0 -7
  187. data/test/integration/sign_in_person_test.rb +0 -26
  188. data/test/integration/sign_in_test.rb +0 -24
  189. data/test/integration/sign_out_test.rb +0 -20
  190. data/test/support/integration_case.rb +0 -11
  191. data/test/unit/user_test.rb +0 -75
@@ -0,0 +1,280 @@
1
+ require 'test_helper'
2
+
3
+ class ControllerTest < IntegrationTest
4
+
5
+ setup do
6
+ User.first_or_create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
7
+ QuoVadis.two_factor_authentication_mandatory false
8
+ end
9
+
10
+
11
+ teardown do
12
+ QuoVadis.session_lifetime_extend_to_end_of_day false
13
+ QuoVadis.session_idle_timeout :lifetime
14
+ end
15
+
16
+
17
+ test 'require_authentication when not logged in' do
18
+ get secret_articles_path
19
+
20
+ assert_redirected_to quo_vadis.login_path
21
+ assert_equal 'Please log in first.', flash[:notice]
22
+ end
23
+
24
+
25
+ test 'require_authentication when logged in' do
26
+ login
27
+ get secret_articles_path
28
+
29
+ assert_response :success
30
+ assert_equal secret_articles_path, path
31
+ end
32
+
33
+
34
+ test 'require_authentication remembers original path' do
35
+ get also_secret_articles_path
36
+ assert_equal '/articles/also_secret', session[:qv_bookmark]
37
+ end
38
+
39
+
40
+ test 'require_two_factor_authentication when not logged in' do
41
+ QuoVadis.two_factor_authentication_mandatory true
42
+ get very_secret_articles_path
43
+
44
+ assert_redirected_to quo_vadis.login_path
45
+ assert_equal 'Please log in first.', flash[:notice]
46
+ end
47
+
48
+
49
+ test 'require_two_factor_authentication when logged in but second factor not required' do
50
+ QuoVadis.two_factor_authentication_mandatory false
51
+ login
52
+ get very_secret_articles_path
53
+
54
+ assert_response :success
55
+ assert_equal very_secret_articles_path, path
56
+ end
57
+
58
+
59
+ test 'require_two_factor_authentication when logged in and second factor required' do
60
+ QuoVadis.two_factor_authentication_mandatory true
61
+ login
62
+ get very_secret_articles_path
63
+
64
+ assert_redirected_to quo_vadis.challenge_totps_path
65
+ follow_redirect!
66
+
67
+ # This second redirect is part of the totps controller but I think
68
+ # it makes sense to test here.
69
+ assert_redirected_to quo_vadis.new_totp_path
70
+ assert_equal 'Please set up two factor authentication.', flash[:alert]
71
+ end
72
+
73
+
74
+ test 'require_two_factor_authentication when already authenticated with two factors' do
75
+ User.first.qv_account.create_totp! last_used_at: Time.now
76
+ QuoVadis.two_factor_authentication_mandatory true
77
+ login
78
+ QuoVadis::Session.last.authenticated_with_second_factor
79
+
80
+ get very_secret_articles_path
81
+ assert_equal very_secret_articles_path, path
82
+ end
83
+
84
+
85
+ test 'second_factor_required?' do
86
+ # 2FA optional, user has not set up 2nd factor
87
+ QuoVadis.two_factor_authentication_mandatory false
88
+ login
89
+ get articles_path
90
+ assert controller.logged_in?
91
+ refute controller.qv.second_factor_required?
92
+
93
+ # 2FA optional, user has set up 2nd factor
94
+ QuoVadis.two_factor_authentication_mandatory false
95
+ User.first.qv_account.create_totp! last_used_at: Time.now
96
+ login
97
+ get articles_path
98
+ assert controller.logged_in?
99
+ assert controller.qv.second_factor_required?
100
+
101
+ # 2FA mandatory
102
+ QuoVadis.two_factor_authentication_mandatory true
103
+ login
104
+ get articles_path
105
+ assert controller.logged_in?
106
+ assert controller.qv.second_factor_required?
107
+ end
108
+
109
+
110
+ test 'logged_in?' do
111
+ # not logged in
112
+ get articles_path
113
+ refute @controller.logged_in?
114
+
115
+ # logged in
116
+ login
117
+ get articles_path
118
+ assert @controller.logged_in?
119
+
120
+ # session remotely deleted
121
+ QuoVadis::Session.destroy jar.encrypted[QuoVadis.cookie_name]
122
+ get articles_path
123
+ refute @controller.logged_in?
124
+
125
+ # login and logout
126
+ login
127
+ get articles_path
128
+ logout
129
+ refute @controller.logged_in?
130
+ end
131
+
132
+
133
+ test 'login' do
134
+ QuoVadis.two_factor_authentication_mandatory false
135
+
136
+ get articles_path
137
+ session_id = session.id
138
+ assert_nil jar.encrypted[QuoVadis.cookie_name]
139
+
140
+ assert_difference 'QuoVadis::Session.count' do
141
+ # We want to test the controller mixin's login method, not the session
142
+ # controller's login action, i.e. the following:
143
+ #
144
+ # @controller.login user
145
+ #
146
+ # But we store the QuoVadis session id in an encrypted cookie, and we can
147
+ # only access cookies as part of a request-response cycle. So we have to
148
+ # trigger the mixin's login method via the session controller's login action.
149
+ login
150
+ end
151
+
152
+ refute_equal session_id, session.id
153
+ qv_session = QuoVadis::Session.last
154
+ assert_equal qv_session.id, jar.encrypted[QuoVadis.cookie_name]
155
+ end
156
+
157
+
158
+ test 'logout' do
159
+ login
160
+
161
+ session_id = session.id
162
+ qv_session_id = jar.encrypted[QuoVadis.cookie_name]
163
+ assert QuoVadis::Session.exists?(qv_session_id)
164
+
165
+ assert_difference 'QuoVadis::Session.count', -1 do
166
+ # We want to test the controller mixin's logout method, not the session
167
+ # controller's logout action, i.e. the following:
168
+ #
169
+ # @controller.logout
170
+ #
171
+ # But we store the QuoVadis session id in an encrypted cookie, and we can
172
+ # only access cookies as part of a request-response cycle. So we have to
173
+ # trigger the mixin's logout method via the session controller's logout action.
174
+ logout
175
+ end
176
+
177
+ refute_equal session_id, session.id
178
+ assert_nil jar.encrypted[QuoVadis.cookie_name]
179
+ refute QuoVadis::Session.exists?(qv_session_id)
180
+ end
181
+
182
+
183
+ test 'qv_logout_other_sessions' do
184
+ desktop = session_login
185
+ phone = session_login
186
+
187
+ desktop.controller.qv.logout_other_sessions
188
+
189
+ phone.get articles_path
190
+ refute phone.controller.logged_in?
191
+
192
+ desktop.get articles_path
193
+ assert desktop.controller.logged_in?
194
+ end
195
+
196
+
197
+ test 'authenticated_model' do
198
+ # not logged in
199
+ get articles_path
200
+ assert_nil @controller.authenticated_model
201
+
202
+ # logged in
203
+ login
204
+ get articles_path
205
+ assert_equal User.first, @controller.authenticated_model
206
+ end
207
+
208
+
209
+ test 'request_confirmation' do
210
+ get articles_path
211
+
212
+ assert_emails 1 do
213
+ controller.request_confirmation User.last
214
+ end
215
+ assert_equal 'A link to confirm your account has been emailed to you.', flash[:notice]
216
+ end
217
+
218
+
219
+ test 'session lifetime exceeded' do
220
+ QuoVadis.session_lifetime 1.week
221
+ login
222
+
223
+ travel 1.week
224
+ travel (-5).minutes
225
+ get articles_path
226
+ assert controller.logged_in?
227
+
228
+ travel 10.minutes
229
+ get articles_path
230
+ refute controller.logged_in?
231
+ end
232
+
233
+
234
+ test 'session lifetime extended to end of day' do
235
+ QuoVadis.session_lifetime 1.week
236
+ QuoVadis.session_lifetime_extend_to_end_of_day true
237
+ login
238
+
239
+ travel 1.week
240
+ travel 10.minutes
241
+ get articles_path
242
+ assert controller.logged_in?
243
+
244
+ travel 1.day
245
+ get articles_path
246
+ refute controller.logged_in?
247
+ end
248
+
249
+
250
+ test 'idle timeout exceeded' do
251
+ QuoVadis.session_lifetime 1.week
252
+ QuoVadis.session_idle_timeout 1.day
253
+ login
254
+
255
+ get articles_path
256
+ assert controller.logged_in?
257
+
258
+ travel 2.days
259
+
260
+ get articles_path
261
+ refute controller.logged_in?
262
+ end
263
+
264
+
265
+ private
266
+
267
+ def login
268
+ post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc')
269
+ end
270
+
271
+ def logout
272
+ delete quo_vadis.logout_path
273
+ end
274
+
275
+ def session_login
276
+ open_session do |sess|
277
+ sess.post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc')
278
+ end
279
+ end
280
+ end
@@ -0,0 +1,244 @@
1
+ require 'test_helper'
2
+
3
+ class LoggingTest < IntegrationTest
4
+
5
+ setup do
6
+ user = User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
7
+ @account = user.qv_account
8
+ end
9
+
10
+
11
+ test 'logs endpoint' do
12
+ QuoVadis::Log.create account: @account, action: 'password.change', ip: '1.2.3.4', metadata: {foo: 'bar', baz: 'qux'}
13
+ login
14
+ get quo_vadis.logs_path
15
+ assert_response :success
16
+
17
+ assert_select 'tbody tr' do
18
+ assert_select 'td', 'Changed password'
19
+ assert_select 'td', '1.2.3.4'
20
+ assert_select 'td', 'foo: bar, baz: qux'
21
+ end
22
+
23
+ assert_select 'tbody tr' do
24
+ assert_select 'td', 'Logged in'
25
+ assert_select 'td', '127.0.0.1'
26
+ assert_select 'td', ''
27
+ end
28
+ end
29
+
30
+
31
+ test 'login.success' do
32
+ assert_log QuoVadis::Log::LOGIN_SUCCESS do
33
+ login
34
+ end
35
+ end
36
+
37
+
38
+ test 'login.failure' do
39
+ assert_log QuoVadis::Log::LOGIN_FAILURE do
40
+ post quo_vadis.login_path(email: 'bob@example.com', password: 'wrong')
41
+ end
42
+ end
43
+
44
+
45
+ test 'login.unknown' do
46
+ assert_log QuoVadis::Log::LOGIN_UNKNOWN, {'identifier' => 'wrong'}, nil do
47
+ post quo_vadis.login_path(email: 'wrong', password: 'wrong')
48
+ end
49
+ end
50
+
51
+
52
+ test 'totp.setup' do
53
+ login
54
+ get quo_vadis.new_totp_path
55
+ totp = controller.instance_variable_get :@totp
56
+ assert_log QuoVadis::Log::TOTP_SETUP do
57
+ post quo_vadis.totps_path(totp: {
58
+ key: totp.key,
59
+ hmac_key: totp.hmac_key,
60
+ otp: ROTP::TOTP.new(totp.key).now
61
+ })
62
+ end
63
+ end
64
+
65
+
66
+ test 'totp.success' do
67
+ login
68
+ totp = User.last.qv_account.create_totp(last_used_at: 1.minute.ago)
69
+ assert_log QuoVadis::Log::TOTP_SUCCESS do
70
+ post quo_vadis.authenticate_totps_path(totp: ROTP::TOTP.new(totp.key).now)
71
+ end
72
+ end
73
+
74
+
75
+ test 'totp.failure' do
76
+ login
77
+ User.last.qv_account.create_totp(last_used_at: 1.minute.ago)
78
+ assert_log QuoVadis::Log::TOTP_FAILURE do
79
+ post quo_vadis.authenticate_totps_path(totp: '000000')
80
+ end
81
+ end
82
+
83
+
84
+ test 'totp.reuse' do
85
+ login
86
+ totp = User.last.qv_account.create_totp(last_used_at: 1.minute.ago)
87
+ post quo_vadis.authenticate_totps_path(totp: ROTP::TOTP.new(totp.key).now)
88
+ assert_log QuoVadis::Log::TOTP_REUSE do
89
+ post quo_vadis.authenticate_totps_path(totp: ROTP::TOTP.new(totp.key).now)
90
+ end
91
+ end
92
+
93
+
94
+ test 'recovery_code.success' do
95
+ login
96
+ codes = @account.generate_recovery_codes
97
+ assert_log QuoVadis::Log::RECOVERY_CODE_SUCCESS do
98
+ post quo_vadis.authenticate_recovery_codes_path(code: codes.first)
99
+ end
100
+ end
101
+
102
+
103
+ test 'recovery_code.failure' do
104
+ login
105
+ assert_log QuoVadis::Log::RECOVERY_CODE_FAILURE do
106
+ post quo_vadis.authenticate_recovery_codes_path(code: 'nope')
107
+ end
108
+ end
109
+
110
+
111
+ test 'recovery_code.generate' do
112
+ login
113
+ assert_log QuoVadis::Log::RECOVERY_CODE_GENERATE do
114
+ post quo_vadis.generate_recovery_codes_path
115
+ end
116
+ end
117
+
118
+
119
+ test '2fa.deactivated' do
120
+ login
121
+ assert_log QuoVadis::Log::TWOFA_DEACTIVATED do
122
+ delete quo_vadis.twofa_path
123
+ end
124
+ end
125
+
126
+
127
+ test 'identifier.change on account' do
128
+ assert_log QuoVadis::Log::IDENTIFIER_CHANGE, {'from' => 'bob@example.com', 'to' => 'x'} do
129
+ QuoVadis::CurrentRequestDetails.set(ip: '127.0.0.1') do
130
+ @account.update identifier: 'x'
131
+ end
132
+ end
133
+ end
134
+
135
+
136
+ test 'email.change aka identifier.change on model' do
137
+ # In our setup the identifier is the email so we expect changing the
138
+ # email to change the identifier too.
139
+ assert_difference 'QuoVadis::Log.count', 2 do
140
+ QuoVadis::CurrentRequestDetails.set(ip: '127.0.0.1') do
141
+ @account.model.update email: 'x'
142
+ end
143
+ end
144
+ log = QuoVadis::Log.first
145
+ assert_equal @account, log.account
146
+ assert_equal QuoVadis::Log::IDENTIFIER_CHANGE, log.action
147
+ assert_equal '127.0.0.1', log.ip
148
+ assert_equal({'from' => 'bob@example.com', 'to' => 'x'}, log.metadata)
149
+ log = QuoVadis::Log.last
150
+ assert_equal @account, log.account
151
+ assert_equal QuoVadis::Log::EMAIL_CHANGE, log.action
152
+ assert_equal '127.0.0.1', log.ip
153
+ assert_equal({'from' => 'bob@example.com', 'to' => 'x'}, log.metadata)
154
+ end
155
+
156
+
157
+ test 'password.change' do
158
+ login
159
+ assert_log QuoVadis::Log::PASSWORD_CHANGE do
160
+ put quo_vadis.password_path(password: {password: '123456789abc', new_password: 'xxxxxxxxxxxx', new_password_confirmation: 'xxxxxxxxxxxx'})
161
+ end
162
+ end
163
+
164
+
165
+ test 'password.reset' do
166
+ assert_difference 'QuoVadis::Log.count', 2 do
167
+ token = QuoVadis::PasswordResetToken.generate @account
168
+ put quo_vadis.password_reset_path(token, password: {password: 'xxxxxxxxxxxx', password_confirmation: 'xxxxxxxxxxxx'})
169
+ end
170
+ assert_equal QuoVadis::Log::PASSWORD_RESET, QuoVadis::Log.first.action
171
+ assert_equal QuoVadis::Log::LOGIN_SUCCESS, log.action
172
+ end
173
+
174
+
175
+ test 'account.confirmation' do
176
+ assert_difference 'QuoVadis::Log.count', 2 do
177
+ token = QuoVadis::AccountConfirmationToken.generate @account
178
+ put quo_vadis.confirmation_path(token)
179
+ end
180
+ assert_equal QuoVadis::Log::ACCOUNT_CONFIRMATION, QuoVadis::Log.first.action
181
+ assert_equal QuoVadis::Log::LOGIN_SUCCESS, log.action
182
+ end
183
+
184
+
185
+ test 'logout.other' do
186
+ login_new_session
187
+ phone = login_new_session
188
+
189
+ # logout first session from phone
190
+ assert_log QuoVadis::Log::LOGOUT_OTHER do
191
+ phone.delete quo_vadis.session_path(QuoVadis::Session.first.id)
192
+ end
193
+ end
194
+
195
+
196
+ test 'logout' do
197
+ post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc')
198
+ assert_log QuoVadis::Log::LOGOUT do
199
+ delete quo_vadis.logout_path
200
+ end
201
+ end
202
+
203
+
204
+ test 'revoke' do
205
+ login_new_session
206
+ assert_log QuoVadis::Log::REVOKE do
207
+ QuoVadis::CurrentRequestDetails.ip = '127.0.0.1' # fake out the IP assertion
208
+ @account.revoke
209
+ end
210
+ end
211
+
212
+
213
+ private
214
+
215
+ def assert_log(action, metadata = {}, account = @account, &block)
216
+ assert_difference 'QuoVadis::Log.count' do
217
+ yield
218
+ end
219
+
220
+ if account.nil?
221
+ assert_nil log.account
222
+ else
223
+ assert_equal account, log.account
224
+ end
225
+ assert_equal action, log.action
226
+ assert_equal '127.0.0.1', log.ip
227
+ assert_equal metadata, log.metadata
228
+ end
229
+
230
+ def log
231
+ QuoVadis::Log.last
232
+ end
233
+
234
+ def login
235
+ post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc')
236
+ end
237
+
238
+ def login_new_session
239
+ open_session do |sess|
240
+ sess.post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc')
241
+ end
242
+ end
243
+
244
+ end