kaze 2.1.0 → 2.2.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +69 -1
- data/README.md +42 -2
- data/lib/kaze/commands/app/base_stack.rb +14 -0
- data/lib/kaze/commands/app/hotwire_stack.rb +4 -1
- data/lib/kaze/commands/app/inertia_react_stack.rb +3 -1
- data/lib/kaze/commands/app/inertia_vue_stack.rb +3 -1
- data/lib/kaze/version.rb +1 -1
- data/stubs/default/app/forms/auth/login_form.rb +6 -1
- data/stubs/default/app/forms/auth/two_factor_challenge_form.rb +24 -0
- data/stubs/default/app/forms/confirm_two_factor_form.rb +5 -0
- data/stubs/default/app/forms/disable_two_factor_form.rb +7 -0
- data/stubs/default/app/models/concerns/two_factor_authenticatable.rb +112 -0
- data/stubs/default/app/models/session_guard.rb +8 -1
- data/stubs/default/app/models/two_factor_challenge.rb +55 -0
- data/stubs/default/app/models/user.rb +1 -0
- data/stubs/default/config/initializers/active_record_encryption.rb +20 -0
- data/stubs/default/config/routes.rb +9 -0
- data/stubs/default/db/migrate/20240101000001_add_two_factor_to_users.rb +15 -0
- data/stubs/default/test/integration/auth/rate_limiting_test.rb +64 -0
- data/stubs/default/test/integration/auth/two_factor_challenge_test.rb +114 -0
- data/stubs/default/test/integration/settings/two_factor_test.rb +104 -0
- data/stubs/default/test/models/two_factor_authenticatable_test.rb +143 -0
- data/stubs/default/test/test_helper.rb +16 -0
- data/stubs/hotwire/app/controllers/auth/authenticated_session_controller.rb +32 -1
- data/stubs/hotwire/app/controllers/auth/two_factor_challenge_controller.rb +59 -0
- data/stubs/hotwire/app/controllers/concerns/set_two_factor_challenge.rb +8 -0
- data/stubs/hotwire/app/controllers/settings/two_factor_controller.rb +48 -0
- data/stubs/hotwire/app/views/auth/forgot_password.html.erb +1 -1
- data/stubs/hotwire/app/views/auth/login.html.erb +1 -1
- data/stubs/hotwire/app/views/auth/register.html.erb +2 -2
- data/stubs/hotwire/app/views/auth/reset_password.html.erb +3 -3
- data/stubs/hotwire/app/views/auth/two_factor_challenge.html.erb +21 -0
- data/stubs/hotwire/app/views/auth/verify_email.html.erb +2 -2
- data/stubs/hotwire/app/views/layouts/_settings.html.erb +1 -0
- data/stubs/hotwire/app/views/layouts/_sidebar_user.html.erb +1 -1
- data/stubs/hotwire/app/views/settings/password/_update_password_form.html.erb +6 -6
- data/stubs/hotwire/app/views/settings/profile/_confirm_user_deletion_modal.html.erb +1 -1
- data/stubs/hotwire/app/views/settings/profile/_delete_user_form.html.erb +1 -1
- data/stubs/hotwire/app/views/settings/two_factor/_confirming.html.erb +20 -0
- data/stubs/hotwire/app/views/settings/two_factor/_disabled.html.erb +8 -0
- data/stubs/hotwire/app/views/settings/two_factor/_enabled.html.erb +29 -0
- data/stubs/hotwire/app/views/settings/two_factor/edit.html.erb +16 -0
- data/stubs/inertia-common/app/controllers/auth/authenticated_session_controller.rb +30 -1
- data/stubs/inertia-common/app/controllers/auth/two_factor_challenge_controller.rb +53 -0
- data/stubs/inertia-common/app/controllers/concerns/set_two_factor_challenge.rb +8 -0
- data/stubs/inertia-common/app/controllers/settings/two_factor_controller.rb +60 -0
- data/stubs/inertia-react-ts/app/javascript/Layouts/SettingsLayout.tsx +5 -1
- data/stubs/inertia-react-ts/app/javascript/Pages/Auth/ForgotPassword.tsx +2 -2
- data/stubs/inertia-react-ts/app/javascript/Pages/Auth/Register.tsx +1 -1
- data/stubs/inertia-react-ts/app/javascript/Pages/Auth/ResetPassword.tsx +3 -3
- data/stubs/inertia-react-ts/app/javascript/Pages/Auth/TwoFactorChallenge.tsx +71 -0
- data/stubs/inertia-react-ts/app/javascript/Pages/Auth/VerifyEmail.tsx +3 -3
- data/stubs/inertia-react-ts/app/javascript/Pages/Settings/Partials/DeleteUserForm.tsx +2 -2
- data/stubs/inertia-react-ts/app/javascript/Pages/Settings/Partials/UpdatePasswordForm.tsx +3 -3
- data/stubs/inertia-react-ts/app/javascript/Pages/Settings/TwoFactor.tsx +199 -0
- data/stubs/inertia-vue-ts/app/javascript/Layouts/SettingsLayout.vue +5 -1
- data/stubs/inertia-vue-ts/app/javascript/Pages/Auth/ForgotPassword.vue +2 -2
- data/stubs/inertia-vue-ts/app/javascript/Pages/Auth/Register.vue +1 -1
- data/stubs/inertia-vue-ts/app/javascript/Pages/Auth/ResetPassword.vue +3 -3
- data/stubs/inertia-vue-ts/app/javascript/Pages/Auth/TwoFactorChallenge.vue +60 -0
- data/stubs/inertia-vue-ts/app/javascript/Pages/Auth/VerifyEmail.vue +3 -3
- data/stubs/inertia-vue-ts/app/javascript/Pages/Settings/Partials/DeleteUserForm.vue +2 -2
- data/stubs/inertia-vue-ts/app/javascript/Pages/Settings/Partials/UpdatePasswordForm.vue +3 -3
- data/stubs/inertia-vue-ts/app/javascript/Pages/Settings/TwoFactor.vue +147 -0
- metadata +41 -1
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class Auth::RateLimitingTest < ActionDispatch::IntegrationTest
|
|
4
|
+
setup do
|
|
5
|
+
@user = FactoryBot.create(:user)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
test 'login attempts are capped' do
|
|
9
|
+
11.times do
|
|
10
|
+
post login_path, params: { email: @user.email, password: 'wrong-password' }
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# The cap holds even when the password is finally right, so guessing cannot
|
|
14
|
+
# be mixed with a known-good attempt to slip through. Asserting this rather
|
|
15
|
+
# than the status, which differs between the stacks.
|
|
16
|
+
post login_path, params: { email: @user.email, password: 'password' }
|
|
17
|
+
|
|
18
|
+
assert_guest
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
test 'the login cap is per email, so one account being attacked does not lock out another' do
|
|
22
|
+
other = FactoryBot.create(:user)
|
|
23
|
+
|
|
24
|
+
11.times do
|
|
25
|
+
post login_path, params: { email: @user.email, password: 'wrong-password' }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
post login_path, params: { email: other.email, password: 'password' }
|
|
29
|
+
|
|
30
|
+
assert_authenticated
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
test 'two factor attempts are capped' do
|
|
34
|
+
@user.prepare_two_factor
|
|
35
|
+
@user.confirm_two_factor(ROTP::TOTP.new(@user.two_factor_secret).now)
|
|
36
|
+
|
|
37
|
+
post login_path, params: { email: @user.email, password: 'password' }
|
|
38
|
+
|
|
39
|
+
6.times do
|
|
40
|
+
post two_factor_challenge_store_path, params: { code: '000000' }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# The real code does not get through either, so an attacker cannot burn
|
|
44
|
+
# guesses and then wait for a window.
|
|
45
|
+
post two_factor_challenge_store_path, params: { code: ROTP::TOTP.new(@user.two_factor_secret).now }
|
|
46
|
+
|
|
47
|
+
assert_guest
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
test 'a correct code within the cap still works' do
|
|
51
|
+
@user.prepare_two_factor
|
|
52
|
+
@user.confirm_two_factor(ROTP::TOTP.new(@user.two_factor_secret).now)
|
|
53
|
+
|
|
54
|
+
post login_path, params: { email: @user.email, password: 'password' }
|
|
55
|
+
|
|
56
|
+
2.times do
|
|
57
|
+
post two_factor_challenge_store_path, params: { code: '000000' }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
post two_factor_challenge_store_path, params: { code: ROTP::TOTP.new(@user.two_factor_secret).now }
|
|
61
|
+
|
|
62
|
+
assert_authenticated
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class Auth::TwoFactorChallengeTest < ActionDispatch::IntegrationTest
|
|
4
|
+
setup do
|
|
5
|
+
@user = FactoryBot.create(:user)
|
|
6
|
+
@user.prepare_two_factor
|
|
7
|
+
@user.confirm_two_factor(code_for(@user))
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# The point of the whole thing: a correct password alone is not a session.
|
|
11
|
+
test 'a correct password does not log in a user with two factor enabled' do
|
|
12
|
+
post login_path, params: { email: @user.email, password: 'password' }
|
|
13
|
+
|
|
14
|
+
assert_guest
|
|
15
|
+
assert_redirected_to two_factor_challenge_path
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
test 'a user without two factor still logs straight in' do
|
|
19
|
+
other = FactoryBot.create(:user)
|
|
20
|
+
|
|
21
|
+
post login_path, params: { email: other.email, password: 'password' }
|
|
22
|
+
|
|
23
|
+
assert_authenticated
|
|
24
|
+
assert_redirected_to dashboard_path
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
test 'the challenge screen can be rendered once a password has been given' do
|
|
28
|
+
post login_path, params: { email: @user.email, password: 'password' }
|
|
29
|
+
|
|
30
|
+
get two_factor_challenge_path
|
|
31
|
+
|
|
32
|
+
assert_response :success
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
test 'the challenge screen is not reachable without giving a password first' do
|
|
36
|
+
get two_factor_challenge_path
|
|
37
|
+
|
|
38
|
+
assert_redirected_to login_path
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
test 'a correct code completes the login' do
|
|
42
|
+
post login_path, params: { email: @user.email, password: 'password' }
|
|
43
|
+
|
|
44
|
+
post two_factor_challenge_store_path, params: { code: code_for(@user) }
|
|
45
|
+
|
|
46
|
+
assert_authenticated
|
|
47
|
+
assert_redirected_to dashboard_path
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
test 'a wrong code does not complete the login' do
|
|
51
|
+
post login_path, params: { email: @user.email, password: 'password' }
|
|
52
|
+
|
|
53
|
+
post two_factor_challenge_store_path, params: { code: '000000' }
|
|
54
|
+
|
|
55
|
+
assert_guest
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
test 'submitting nothing does not complete the login' do
|
|
59
|
+
post login_path, params: { email: @user.email, password: 'password' }
|
|
60
|
+
|
|
61
|
+
post two_factor_challenge_store_path, params: {}
|
|
62
|
+
|
|
63
|
+
assert_guest
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
test 'a recovery code completes the login and is then spent' do
|
|
67
|
+
recovery_code = @user.two_factor_recovery_codes_list.first
|
|
68
|
+
|
|
69
|
+
post login_path, params: { email: @user.email, password: 'password' }
|
|
70
|
+
post two_factor_challenge_store_path, params: { recovery_code: recovery_code }
|
|
71
|
+
|
|
72
|
+
assert_authenticated
|
|
73
|
+
assert_not_includes @user.reload.two_factor_recovery_codes_list, recovery_code
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
test 'a spent recovery code does not work twice' do
|
|
77
|
+
recovery_code = @user.two_factor_recovery_codes_list.first
|
|
78
|
+
|
|
79
|
+
post login_path, params: { email: @user.email, password: 'password' }
|
|
80
|
+
post two_factor_challenge_store_path, params: { recovery_code: recovery_code }
|
|
81
|
+
post logout_path
|
|
82
|
+
|
|
83
|
+
post login_path, params: { email: @user.email, password: 'password' }
|
|
84
|
+
post two_factor_challenge_store_path, params: { recovery_code: recovery_code }
|
|
85
|
+
|
|
86
|
+
assert_guest
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
test 'a challenge left too long expires' do
|
|
90
|
+
post login_path, params: { email: @user.email, password: 'password' }
|
|
91
|
+
|
|
92
|
+
travel TwoFactorChallenge::EXPIRES_IN + 1.minute do
|
|
93
|
+
post two_factor_challenge_store_path, params: { code: code_for(@user) }
|
|
94
|
+
|
|
95
|
+
assert_guest
|
|
96
|
+
assert_redirected_to login_path
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
test 'the challenge is cleared once it has been used' do
|
|
101
|
+
post login_path, params: { email: @user.email, password: 'password' }
|
|
102
|
+
post two_factor_challenge_store_path, params: { code: code_for(@user) }
|
|
103
|
+
post logout_path
|
|
104
|
+
|
|
105
|
+
get two_factor_challenge_path
|
|
106
|
+
|
|
107
|
+
assert_redirected_to login_path
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
private
|
|
111
|
+
def code_for(user)
|
|
112
|
+
ROTP::TOTP.new(user.two_factor_secret).now
|
|
113
|
+
end
|
|
114
|
+
end
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class Settings::TwoFactorTest < ActionDispatch::IntegrationTest
|
|
4
|
+
setup do
|
|
5
|
+
@user = FactoryBot.create(:user)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
test 'the page is displayed' do
|
|
9
|
+
acting_as(@user).get two_factor_edit_path
|
|
10
|
+
|
|
11
|
+
assert_response :success
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
test 'the page requires a login' do
|
|
15
|
+
get two_factor_edit_path
|
|
16
|
+
|
|
17
|
+
assert_redirected_to login_path
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
test 'starting setup generates a secret without enabling anything' do
|
|
21
|
+
acting_as(@user).post two_factor_store_path
|
|
22
|
+
|
|
23
|
+
@user.reload
|
|
24
|
+
|
|
25
|
+
assert @user.two_factor_pending?
|
|
26
|
+
assert_not @user.two_factor_enabled?
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
test 'a correct code finishes setup' do
|
|
30
|
+
acting_as(@user).post two_factor_store_path
|
|
31
|
+
@user.reload
|
|
32
|
+
|
|
33
|
+
patch two_factor_update_path, params: { code: code_for(@user) }
|
|
34
|
+
|
|
35
|
+
assert @user.reload.two_factor_enabled?
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
test 'a wrong code does not finish setup' do
|
|
39
|
+
acting_as(@user).post two_factor_store_path
|
|
40
|
+
|
|
41
|
+
patch two_factor_update_path, params: { code: '000000' }
|
|
42
|
+
|
|
43
|
+
assert_not @user.reload.two_factor_enabled?
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
test 'an empty code does not finish setup' do
|
|
47
|
+
acting_as(@user).post two_factor_store_path
|
|
48
|
+
|
|
49
|
+
patch two_factor_update_path, params: {}
|
|
50
|
+
|
|
51
|
+
assert_not @user.reload.two_factor_enabled?
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
test 'recovery codes can be regenerated' do
|
|
55
|
+
enable_two_factor
|
|
56
|
+
|
|
57
|
+
before = @user.reload.two_factor_recovery_codes_list
|
|
58
|
+
|
|
59
|
+
post two_factor_recovery_codes_path
|
|
60
|
+
|
|
61
|
+
assert_not_equal before, @user.reload.two_factor_recovery_codes_list
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Turning the protection off is the dangerous direction, so it asks for the
|
|
65
|
+
# password rather than trusting the session alone.
|
|
66
|
+
test 'the current password is required to turn it off' do
|
|
67
|
+
enable_two_factor
|
|
68
|
+
|
|
69
|
+
delete two_factor_destroy_path, params: { password: 'wrong-password' }
|
|
70
|
+
|
|
71
|
+
assert @user.reload.two_factor_enabled?
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
test 'no password at all does not turn it off' do
|
|
75
|
+
enable_two_factor
|
|
76
|
+
|
|
77
|
+
delete two_factor_destroy_path, params: {}
|
|
78
|
+
|
|
79
|
+
assert @user.reload.two_factor_enabled?
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
test 'the correct password turns it off and clears everything' do
|
|
83
|
+
enable_two_factor
|
|
84
|
+
|
|
85
|
+
delete two_factor_destroy_path, params: { password: 'password' }
|
|
86
|
+
|
|
87
|
+
@user.reload
|
|
88
|
+
|
|
89
|
+
assert_not @user.two_factor_enabled?
|
|
90
|
+
assert_nil @user.two_factor_secret
|
|
91
|
+
assert_empty @user.two_factor_recovery_codes_list
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
private
|
|
95
|
+
def code_for(user)
|
|
96
|
+
ROTP::TOTP.new(user.two_factor_secret).now
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def enable_two_factor
|
|
100
|
+
acting_as(@user).post two_factor_store_path
|
|
101
|
+
@user.reload
|
|
102
|
+
patch two_factor_update_path, params: { code: code_for(@user) }
|
|
103
|
+
end
|
|
104
|
+
end
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class TwoFactorAuthenticatableTest < ActiveSupport::TestCase
|
|
4
|
+
setup do
|
|
5
|
+
@user = FactoryBot.create(:user)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
test 'a new user has two factor disabled' do
|
|
9
|
+
assert_not @user.two_factor_enabled?
|
|
10
|
+
assert_not @user.two_factor_pending?
|
|
11
|
+
assert_empty @user.two_factor_recovery_codes_list
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
test 'preparing generates a secret and recovery codes but does not enable' do
|
|
15
|
+
@user.prepare_two_factor
|
|
16
|
+
|
|
17
|
+
assert @user.two_factor_secret.present?
|
|
18
|
+
assert_equal TwoFactorAuthenticatable::RECOVERY_CODE_COUNT, @user.two_factor_recovery_codes_list.length
|
|
19
|
+
assert @user.two_factor_pending?
|
|
20
|
+
assert_not @user.two_factor_enabled?
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
test 'preparing again replaces an unfinished attempt' do
|
|
24
|
+
@user.prepare_two_factor
|
|
25
|
+
first_secret = @user.two_factor_secret
|
|
26
|
+
|
|
27
|
+
@user.prepare_two_factor
|
|
28
|
+
|
|
29
|
+
assert_not_equal first_secret, @user.two_factor_secret
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
test 'a correct code confirms, and only then is it enabled' do
|
|
33
|
+
@user.prepare_two_factor
|
|
34
|
+
|
|
35
|
+
assert @user.confirm_two_factor(current_code)
|
|
36
|
+
assert @user.two_factor_enabled?
|
|
37
|
+
assert_not @user.two_factor_pending?
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
test 'a wrong code does not confirm' do
|
|
41
|
+
@user.prepare_two_factor
|
|
42
|
+
|
|
43
|
+
assert_not @user.confirm_two_factor('000000')
|
|
44
|
+
assert_not @user.two_factor_enabled?
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
test 'confirming does nothing when nothing is pending' do
|
|
48
|
+
assert_not @user.confirm_two_factor('000000')
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
test 'codes verify only against the right secret' do
|
|
52
|
+
@user.prepare_two_factor
|
|
53
|
+
|
|
54
|
+
assert @user.verify_two_factor_code(current_code)
|
|
55
|
+
assert_not @user.verify_two_factor_code('000000')
|
|
56
|
+
assert_not @user.verify_two_factor_code('')
|
|
57
|
+
assert_not @user.verify_two_factor_code(nil)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
test 'a code from a different secret is rejected' do
|
|
61
|
+
@user.prepare_two_factor
|
|
62
|
+
other = ROTP::TOTP.new(ROTP::Base32.random).now
|
|
63
|
+
|
|
64
|
+
assert_not @user.verify_two_factor_code(other)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
test 'recovery codes are single use' do
|
|
68
|
+
@user.prepare_two_factor
|
|
69
|
+
code = @user.two_factor_recovery_codes_list.first
|
|
70
|
+
|
|
71
|
+
assert @user.consume_two_factor_recovery_code(code)
|
|
72
|
+
assert_not_includes @user.two_factor_recovery_codes_list, code
|
|
73
|
+
assert_not @user.consume_two_factor_recovery_code(code)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
test 'consuming an unknown recovery code leaves the rest alone' do
|
|
77
|
+
@user.prepare_two_factor
|
|
78
|
+
before = @user.two_factor_recovery_codes_list
|
|
79
|
+
|
|
80
|
+
assert_not @user.consume_two_factor_recovery_code('not-a-code')
|
|
81
|
+
assert_equal before, @user.two_factor_recovery_codes_list
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
test 'recovery codes can be regenerated without touching the secret' do
|
|
85
|
+
@user.prepare_two_factor
|
|
86
|
+
secret = @user.two_factor_secret
|
|
87
|
+
before = @user.two_factor_recovery_codes_list
|
|
88
|
+
|
|
89
|
+
@user.regenerate_two_factor_recovery_codes
|
|
90
|
+
|
|
91
|
+
assert_equal secret, @user.two_factor_secret
|
|
92
|
+
assert_not_equal before, @user.two_factor_recovery_codes_list
|
|
93
|
+
assert_equal TwoFactorAuthenticatable::RECOVERY_CODE_COUNT, @user.two_factor_recovery_codes_list.length
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
test 'disabling clears everything' do
|
|
97
|
+
@user.prepare_two_factor
|
|
98
|
+
@user.confirm_two_factor(current_code)
|
|
99
|
+
|
|
100
|
+
@user.disable_two_factor
|
|
101
|
+
|
|
102
|
+
assert_nil @user.two_factor_secret
|
|
103
|
+
assert_nil @user.two_factor_confirmed_at
|
|
104
|
+
assert_empty @user.two_factor_recovery_codes_list
|
|
105
|
+
assert_not @user.two_factor_enabled?
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
test 'the provisioning uri carries the secret and the account' do
|
|
109
|
+
@user.prepare_two_factor
|
|
110
|
+
uri = @user.two_factor_provisioning_uri(issuer: 'Kaze')
|
|
111
|
+
|
|
112
|
+
assert_includes uri, @user.two_factor_secret
|
|
113
|
+
assert_includes uri, 'issuer=Kaze'
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
test 'there is no provisioning uri or qr code without a secret' do
|
|
117
|
+
assert_nil @user.two_factor_provisioning_uri
|
|
118
|
+
assert_nil @user.two_factor_qr_code_svg
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
test 'the qr code is an svg' do
|
|
122
|
+
@user.prepare_two_factor
|
|
123
|
+
|
|
124
|
+
assert_match(/<svg/, @user.two_factor_qr_code_svg)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# The secret is the whole of the second factor, so it must not sit in the
|
|
128
|
+
# database in the clear.
|
|
129
|
+
test 'the secret and recovery codes are encrypted at rest' do
|
|
130
|
+
@user.prepare_two_factor
|
|
131
|
+
|
|
132
|
+
row = User.lease_connection.select_one("SELECT two_factor_secret, two_factor_recovery_codes FROM users WHERE id = #{@user.id}")
|
|
133
|
+
|
|
134
|
+
assert_not_equal @user.two_factor_secret, row['two_factor_secret']
|
|
135
|
+
assert_not_includes row['two_factor_secret'].to_s, @user.two_factor_secret
|
|
136
|
+
assert_not_includes row['two_factor_recovery_codes'].to_s, @user.two_factor_recovery_codes_list.first
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
private
|
|
140
|
+
def current_code
|
|
141
|
+
ROTP::TOTP.new(@user.two_factor_secret).now
|
|
142
|
+
end
|
|
143
|
+
end
|
|
@@ -2,6 +2,17 @@ ENV['RAILS_ENV'] ||= 'test'
|
|
|
2
2
|
require_relative '../config/environment'
|
|
3
3
|
require 'rails/test_help'
|
|
4
4
|
|
|
5
|
+
# Build the frontend once, here in the parent process, before `parallelize`
|
|
6
|
+
# forks its workers. Vite Ruby builds on demand and guards it with a Mutex,
|
|
7
|
+
# which only holds within a process: each worker would otherwise start its own
|
|
8
|
+
# `vite build` against the same manifest, and a worker that read it
|
|
9
|
+
# half-written failed with "Unexpected end of JSON input". A first run is the
|
|
10
|
+
# only one exposed -- afterwards there is nothing left to build -- which is
|
|
11
|
+
# exactly the run a fresh checkout on CI does.
|
|
12
|
+
if defined?(ViteRuby) && !ViteRuby.instance.dev_server_running?
|
|
13
|
+
abort 'Vite build failed. Run `bin/vite build --mode=test` to see why.' unless ViteRuby.commands.build
|
|
14
|
+
end
|
|
15
|
+
|
|
5
16
|
module ActiveSupport
|
|
6
17
|
class TestCase
|
|
7
18
|
# Run tests in parallel with specified workers
|
|
@@ -10,6 +21,11 @@ module ActiveSupport
|
|
|
10
21
|
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
|
|
11
22
|
fixtures :all
|
|
12
23
|
|
|
24
|
+
# Rate limits are counted in the cache, which outlives the transaction each
|
|
25
|
+
# test is rolled back in -- and rolled back ids get reused, so one test's
|
|
26
|
+
# attempts would otherwise count against the next one's.
|
|
27
|
+
setup { Rails.cache.clear }
|
|
28
|
+
|
|
13
29
|
# Add more helper methods to be used by all tests here...
|
|
14
30
|
def acting_as(user)
|
|
15
31
|
post login_path, params: { email: user.email, password: 'password' }
|
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
class Auth::AuthenticatedSessionController < ApplicationController
|
|
2
2
|
include RedirectIfAuthenticated
|
|
3
|
+
include SetTwoFactorChallenge
|
|
3
4
|
|
|
4
5
|
skip_authenticate only: %i[new create]
|
|
5
6
|
skip_redirect_if_authenticated only: :destroy
|
|
6
7
|
skip_ensure_email_is_verified only: :destroy
|
|
7
8
|
|
|
9
|
+
# Passwords are stronger than a six digit code, but not so strong that
|
|
10
|
+
# unlimited guesses are safe. Keyed on the email being tried.
|
|
11
|
+
rate_limit to: 10,
|
|
12
|
+
within: 1.minute,
|
|
13
|
+
by: -> { params[:email].to_s.strip.downcase.presence || request.remote_ip },
|
|
14
|
+
with: -> { too_many_attempts },
|
|
15
|
+
only: :create
|
|
16
|
+
|
|
8
17
|
layout 'guest'
|
|
9
18
|
|
|
10
19
|
def new
|
|
@@ -18,7 +27,11 @@ class Auth::AuthenticatedSessionController < ApplicationController
|
|
|
18
27
|
|
|
19
28
|
@form.authenticate
|
|
20
29
|
|
|
21
|
-
return render 'auth/login', status: :unprocessable_entity if
|
|
30
|
+
return render 'auth/login', status: :unprocessable_entity if @form.user.nil?
|
|
31
|
+
|
|
32
|
+
return redirect_to two_factor_challenge_path if start_two_factor_challenge(@form)
|
|
33
|
+
|
|
34
|
+
Current.auth.login(@form.user, @form.remember)
|
|
22
35
|
|
|
23
36
|
redirect_to dashboard_path
|
|
24
37
|
end
|
|
@@ -30,4 +43,22 @@ class Auth::AuthenticatedSessionController < ApplicationController
|
|
|
30
43
|
|
|
31
44
|
redirect_to '/'
|
|
32
45
|
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def too_many_attempts
|
|
50
|
+
@form ||= Auth::LoginForm.new
|
|
51
|
+
@form.errors.add(:email, message: 'Too many attempts. Try again in a minute.')
|
|
52
|
+
|
|
53
|
+
render 'auth/login', status: :too_many_requests
|
|
54
|
+
end
|
|
55
|
+
# A correct password is not a session when a second factor is owed. Hold the
|
|
56
|
+
# user in the challenge instead and let them prove the rest.
|
|
57
|
+
def start_two_factor_challenge(form)
|
|
58
|
+
return false unless form.user.two_factor_enabled?
|
|
59
|
+
|
|
60
|
+
two_factor_challenge.start(form.user, remember: form.remember.present?)
|
|
61
|
+
|
|
62
|
+
true
|
|
63
|
+
end
|
|
33
64
|
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
class Auth::TwoFactorChallengeController < ApplicationController
|
|
2
|
+
include RedirectIfAuthenticated
|
|
3
|
+
include SetTwoFactorChallenge
|
|
4
|
+
|
|
5
|
+
skip_authenticate
|
|
6
|
+
before_action :ensure_challenge_is_pending
|
|
7
|
+
|
|
8
|
+
# Six digits is small enough to guess given enough tries, so the attempts are
|
|
9
|
+
# capped. Keyed on the account being challenged rather than the address, so
|
|
10
|
+
# moving between addresses does not buy more attempts.
|
|
11
|
+
rate_limit to: 5,
|
|
12
|
+
within: 1.minute,
|
|
13
|
+
by: -> { two_factor_challenge.user&.id || request.remote_ip },
|
|
14
|
+
with: -> { too_many_attempts },
|
|
15
|
+
only: :create
|
|
16
|
+
|
|
17
|
+
layout 'guest'
|
|
18
|
+
|
|
19
|
+
def new
|
|
20
|
+
@form = Auth::TwoFactorChallengeForm.new
|
|
21
|
+
|
|
22
|
+
render 'auth/two_factor_challenge'
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def create
|
|
26
|
+
@form = Auth::TwoFactorChallengeForm.new(params.permit(:code, :recovery_code))
|
|
27
|
+
|
|
28
|
+
@form.authenticate(two_factor_challenge.user)
|
|
29
|
+
|
|
30
|
+
return render 'auth/two_factor_challenge', status: :unprocessable_entity if @form.errors.any?
|
|
31
|
+
|
|
32
|
+
complete_login
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def too_many_attempts
|
|
38
|
+
@form ||= Auth::TwoFactorChallengeForm.new
|
|
39
|
+
@form.errors.add(:code, message: 'Too many attempts. Try again in a minute.')
|
|
40
|
+
|
|
41
|
+
render 'auth/two_factor_challenge', status: :too_many_requests
|
|
42
|
+
end
|
|
43
|
+
def ensure_challenge_is_pending
|
|
44
|
+
redirect_to login_path unless two_factor_challenge.pending?
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# The session id is rotated on the way in, so a session fixed before the
|
|
48
|
+
# password was given cannot survive the second factor.
|
|
49
|
+
def complete_login
|
|
50
|
+
user = two_factor_challenge.user
|
|
51
|
+
remember = two_factor_challenge.remember?
|
|
52
|
+
|
|
53
|
+
reset_session
|
|
54
|
+
|
|
55
|
+
Current.auth.login(user, remember)
|
|
56
|
+
|
|
57
|
+
redirect_to dashboard_path
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
class Settings::TwoFactorController < ApplicationController
|
|
2
|
+
skip_ensure_email_is_verified
|
|
3
|
+
|
|
4
|
+
def edit
|
|
5
|
+
@confirm_form = ConfirmTwoFactorForm.new
|
|
6
|
+
@disable_form = DisableTwoFactorForm.new
|
|
7
|
+
|
|
8
|
+
render 'settings/two_factor/edit'
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Starts setup. The account is not protected until the code is confirmed, so
|
|
12
|
+
# this is safe to call again to start over.
|
|
13
|
+
def create
|
|
14
|
+
Current.auth.user.prepare_two_factor
|
|
15
|
+
|
|
16
|
+
redirect_to two_factor_edit_path
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def update
|
|
20
|
+
@confirm_form = ConfirmTwoFactorForm.new(params.permit(:code))
|
|
21
|
+
@disable_form = DisableTwoFactorForm.new
|
|
22
|
+
|
|
23
|
+
unless @confirm_form.valid? && Current.auth.user.confirm_two_factor(@confirm_form.code)
|
|
24
|
+
@confirm_form.errors.add(:code, message: 'That code is not valid.') if @confirm_form.errors[:code].empty?
|
|
25
|
+
|
|
26
|
+
return render 'settings/two_factor/edit', status: :unprocessable_entity
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
redirect_to two_factor_edit_path, flash: { status: 'two-factor-enabled' }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def destroy
|
|
33
|
+
@confirm_form = ConfirmTwoFactorForm.new
|
|
34
|
+
@disable_form = DisableTwoFactorForm.new(params.permit(:password))
|
|
35
|
+
|
|
36
|
+
return render 'settings/two_factor/edit', status: :unprocessable_entity if @disable_form.invalid?
|
|
37
|
+
|
|
38
|
+
Current.auth.user.disable_two_factor
|
|
39
|
+
|
|
40
|
+
redirect_to two_factor_edit_path
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def recovery_codes
|
|
44
|
+
Current.auth.user.regenerate_two_factor_recovery_codes
|
|
45
|
+
|
|
46
|
+
redirect_to two_factor_edit_path, flash: { status: 'recovery-codes-generated' }
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
<%= render(TextInputComponent.new({ id: "password", class: "block mt-1 w-full", type: "password", name: "password", required: true, autocomplete: "new-password" })) %>
|
|
18
18
|
<%= render(InputErrorComponent.new({ class: "mt-2", message: @form.error_messages[:password] })) %>
|
|
19
19
|
</div>
|
|
20
|
-
<!-- Confirm
|
|
20
|
+
<!-- Confirm password -->
|
|
21
21
|
<div class="mt-4">
|
|
22
|
-
<%= render(InputLabelComponent.new({ for: "password_confirmation", value: "Confirm
|
|
22
|
+
<%= render(InputLabelComponent.new({ for: "password_confirmation", value: "Confirm password" })) %>
|
|
23
23
|
<%= render(TextInputComponent.new({ id: "password_confirmation", class: "block mt-1 w-full", type: "password", name: "password_confirmation", required: true, autocomplete: "new-password" })) %>
|
|
24
24
|
<%= render(InputErrorComponent.new({ class: "mt-2", message: @form.error_messages[:password_confirmation] })) %>
|
|
25
25
|
</div>
|
|
@@ -7,15 +7,15 @@
|
|
|
7
7
|
<%= render(TextInputComponent.new({ id: "password", class: "block mt-1 w-full", type: "password", name: "password", required: true, autofocus: true, autocomplete: "new-password" })) %>
|
|
8
8
|
<%= render(InputErrorComponent.new({ class: "mt-2", message: @form.error_messages[:password] })) %>
|
|
9
9
|
</div>
|
|
10
|
-
<!-- Confirm
|
|
10
|
+
<!-- Confirm password -->
|
|
11
11
|
<div class="mt-4">
|
|
12
|
-
<%= render(InputLabelComponent.new({ for: "password_confirmation", value: "Confirm
|
|
12
|
+
<%= render(InputLabelComponent.new({ for: "password_confirmation", value: "Confirm password" })) %>
|
|
13
13
|
<%= render(TextInputComponent.new({ id: "password_confirmation", class: "block mt-1 w-full", type: "password", name: "password_confirmation", required: true, autocomplete: "new-password" })) %>
|
|
14
14
|
<%= render(InputErrorComponent.new({ class: "mt-2", message: @form.error_messages[:password_confirmation] })) %>
|
|
15
15
|
</div>
|
|
16
16
|
<div class="flex items-center justify-end mt-4">
|
|
17
17
|
<%= render(PrimaryButtonComponent.new) do %>
|
|
18
|
-
Reset
|
|
18
|
+
Reset password
|
|
19
19
|
<% end %>
|
|
20
20
|
</div>
|
|
21
21
|
<% end %>
|