kaze 2.1.1 → 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 +46 -1
- data/README.md +13 -1
- 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/two_factor_challenge.html.erb +21 -0
- data/stubs/hotwire/app/views/layouts/_settings.html.erb +1 -0
- 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/TwoFactorChallenge.tsx +71 -0
- 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/TwoFactorChallenge.vue +60 -0
- data/stubs/inertia-vue-ts/app/javascript/Pages/Settings/TwoFactor.vue +147 -0
- metadata +27 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c7ab421b0a7d20dbb30c5b0aaf56162163b146eda38f71a29be7a828aa1625b9
|
|
4
|
+
data.tar.gz: 992e0ec2554ad5b93feb3e88780e774333af09a047bc633cf59d4371e158c4ef
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 464c2bea1cbd7454d506daae0e0ea93860c03522a3c5d3a33008677452412668590cea894890c2467a0c2f624c1ca4acc50c7fa2e156354a8a4a9971924a7674
|
|
7
|
+
data.tar.gz: 9ab0ae727a1b18b1d5bdfb0c8b346d7e86a94338094b758fef7791a38cd91ed47d5be3c7c9e9807ed3d0f156ad7ef4c0e5c1156923b8303b005a24a2eb2f6ef6
|
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,50 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [2.2.0] - 2026-10-27
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- Rate limiting on the login and two factor forms. Six digits is small enough to guess given enough tries, so the challenge caps attempts at five a minute, keyed on the account being challenged rather than the address — moving between addresses buys no extra attempts. Login is capped at ten a minute, keyed on the email being tried, so one account under attack cannot lock anyone else out.
|
|
14
|
+
|
|
15
|
+
Rails counts these in the cache, and a generated application uses the null store in test, which accepts every write silently and would let a limit pass as working while doing nothing. The installer now switches the test environment to the memory store so the behaviour is real and testable; development and production already have real stores. `test_helper.rb` clears the cache between tests, since rate limits outlive the transaction a test is rolled back in and rolled-back ids get reused.
|
|
16
|
+
|
|
17
|
+
- A two factor settings page, at `/settings/two-factor` and in the settings sub-navigation, where two factor can be turned on and off. Turning it on shows a QR code to scan and the key to type if scanning is not possible, and is not finished until a code from the authenticator app is confirmed. Once on, the page lists the eight recovery codes and can regenerate them.
|
|
18
|
+
|
|
19
|
+
Turning it off asks for the current password. Removing a protection should take more than a session someone else may be holding.
|
|
20
|
+
|
|
21
|
+
- A two factor challenge in the login flow. Where a user has two factor enabled, a correct password no longer starts a session: it holds them in a challenge and asks for a code from their authenticator app, or one of their recovery codes. Users without two factor are unaffected and log straight in as before.
|
|
22
|
+
|
|
23
|
+
`SessionGuard#attempt` has been split, so `validate` checks credentials and returns the user without starting a session, and `attempt` is that plus `login`. `Auth::LoginForm` uses the first and exposes the user it found, leaving the controller to decide whether a second factor is still owed. A `TwoFactorChallenge` object holds the pending user in the session and nothing else, so an abandoned challenge grants nothing, and it expires after five minutes because a challenge left open is a password already given. The session id is rotated when the challenge completes, so a session fixed before the password was given cannot survive the second factor.
|
|
24
|
+
|
|
25
|
+
- The groundwork for two-factor authentication: a `TwoFactorAuthenticatable` concern on `User`, the columns it needs, and `rotp` and `rqrcode` as dependencies. Nothing is wired into the login flow or the settings area yet — that comes next — so this changes nothing a user can see.
|
|
26
|
+
|
|
27
|
+
Setup is two steps on purpose, as it is in Laravel's kits. `prepare_two_factor` generates a secret and eight single-use recovery codes, and `confirm_two_factor` only marks the account protected once the user has read a working code back. Walking away halfway leaves the account exactly as it was rather than locked out. Codes verify with a step of drift either side, so a slightly wrong clock or a code typed as it expired still works.
|
|
28
|
+
|
|
29
|
+
The secret and the recovery codes are encrypted at rest with Active Record Encryption. That normally needs three keys a generated application does not have, so a new `config/initializers/active_record_encryption.rb` derives them from `secret_key_base` — encrypted attributes work from the first boot with no extra setup. The trade-off is that rotating `secret_key_base` makes existing ciphertext unreadable; the initializer says so and points at `bin/rails db:encryption:init` for anyone who would rather manage the keys separately.
|
|
30
|
+
|
|
31
|
+
### Fixed
|
|
32
|
+
|
|
33
|
+
- The first `bin/rails test` run of an Inertia application no longer fails to build its frontend. Vite Ruby builds assets on demand and guards that with a mutex, which only holds within a single process — but `test_helper.rb` runs the suite under `parallelize(workers: :number_of_processors)`, so every forked worker started its own `vite build` against the same manifest, and a worker that read the file while another was writing it died with `Unexpected end of JSON input`. The tests that followed reported the symptom rather than the cause: `Vite Ruby can't find entrypoints/application.tsx in the manifests`.
|
|
34
|
+
|
|
35
|
+
Only a first run is exposed, since afterwards there is nothing left to build — which is why this looked like a local flake for as long as it did, and why re-running always cleared it. A fresh checkout on CI is a first run every time, so CI was exposed on every build and had simply been winning the race; it started losing once the two factor work grew the suite. On a ten-core machine a cleared build failed two runs in three.
|
|
36
|
+
|
|
37
|
+
`test_helper.rb` now builds once in the parent process before the workers fork, which leaves them nothing to race over, and stops with the reason if that build fails rather than letting it surface three screens later as a missing entrypoint. The Hotwire stack has no Vite and is unaffected.
|
|
38
|
+
|
|
39
|
+
### Documentation
|
|
40
|
+
|
|
41
|
+
- A README section on two factor authentication. The feature spans four pull requests and touches the login flow, the settings area and the database, and none of it was written down anywhere a user would look. The section covers the two-step setup, the challenge and the recovery codes, the password gate on turning it off, and the `TwoFactorAuthenticatable` concern for anyone driving it from their own code.
|
|
42
|
+
|
|
43
|
+
It also states the one consequence of deriving the encryption keys from `secret_key_base`: rotating that key makes existing two factor secrets unreadable, and anyone who had it turned on has to set it up again. That belongs in the README rather than only in a comment in the initializer, since the person who rotates a secret is not usually the person reading initializers.
|
|
44
|
+
|
|
45
|
+
||||||| 1466c8e
|
|
46
|
+
|
|
47
|
+
### Internal
|
|
48
|
+
|
|
49
|
+
- `bin/release` now promotes the changelog itself, and refuses to run when the ground is not safe. Cutting 2.1.1 showed the old script had drifted out of step with the project: it bumped `lib/kaze/version.rb` in one commit and left the `[Unreleased]` heading to be promoted in another, which puts a commit on master where the version and the newest changelog heading disagree — exactly what the gem's own test suite asserts against. Both now move in the same commit.
|
|
50
|
+
|
|
51
|
+
It also had no `set -e`, so a failed `git push` went on to tag the commit and publish the gem anyway. It now stops at the first failure, and checks before touching anything that the branch is master, the tree is clean, master and origin agree, the tag does not already exist, and `[Unreleased]` actually has something in it. The test suite runs before the push rather than after.
|
|
52
|
+
|
|
9
53
|
## [2.1.1] - 2026-10-25
|
|
10
54
|
|
|
11
55
|
### Fixed
|
|
@@ -217,7 +261,8 @@ Together these take a freshly scaffolded Inertia application from seven npm advi
|
|
|
217
261
|
|
|
218
262
|
Releases before 1.0.0 are not catalogued here. See the [tags](https://github.com/cuonggt/kaze/tags) for their history.
|
|
219
263
|
|
|
220
|
-
[Unreleased]: https://github.com/cuonggt/kaze/compare/v2.
|
|
264
|
+
[Unreleased]: https://github.com/cuonggt/kaze/compare/v2.2.0...HEAD
|
|
265
|
+
[2.2.0]: https://github.com/cuonggt/kaze/compare/v2.1.1...v2.2.0
|
|
221
266
|
[2.1.1]: https://github.com/cuonggt/kaze/compare/v2.1.0...v2.1.1
|
|
222
267
|
[2.1.0]: https://github.com/cuonggt/kaze/compare/v2.0.1...v2.1.0
|
|
223
268
|
[2.0.1]: https://github.com/cuonggt/kaze/compare/v2.0.0...v2.0.1
|
data/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
Heavily inspired by [Laravel Breeze](https://github.com/laravel/breeze), this gem offers authentication and application starter kits to give you a head start building your new [Rails](https://rubyonrails.org) application. These kits automatically scaffold your application with the routes, controllers, and views you need to register and authenticate your application's users.
|
|
10
10
|
|
|
11
|
-
[Kaze](https://github.com/cuonggt/kaze) is an opinionated, minimal, simple implementation of all authentication features that a modern web application should have, including login, registration, password reset, and
|
|
11
|
+
[Kaze](https://github.com/cuonggt/kaze) is an opinionated, minimal, simple implementation of all authentication features that a modern web application should have, including login, registration, password reset, email verification, and two factor authentication. In addition, Kaze includes a settings area where the user may update their name and email address, change their password, turn two factor authentication on and off, choose an appearance, and delete their account.
|
|
12
12
|
|
|
13
13
|
Kaze provides scaffolding options based on [Hotwire](https://hotwired.dev) or [Inertia](https://inertiajs.com), with the choice of using Vue or React for the Inertia-based scaffolding.
|
|
14
14
|
|
|
@@ -104,6 +104,18 @@ Colour runs through design tokens — `bg-background`, `text-muted-foreground`,
|
|
|
104
104
|
|
|
105
105
|
There is no shadcn for Hotwire. That stack gets the same look from ViewComponents written against the same tokens, with the handful of [lucide](https://lucide.dev) icons the layouts need inlined into an `IconComponent`, since it carries no npm dependencies at all.
|
|
106
106
|
|
|
107
|
+
## Two factor authentication
|
|
108
|
+
|
|
109
|
+
Two factor authentication is time-based one-time passwords, the kind an authenticator app generates. A user turns it on from `/settings/two-factor`, scans the QR code shown there — or types the key, if scanning is not possible — and the account is only protected once they have read a code back. Walking away from a half-finished setup leaves the account exactly as it was rather than locked out.
|
|
110
|
+
|
|
111
|
+
Once it is on, a correct password no longer signs anyone in on its own: it holds them at a challenge that asks for a code, or for one of the eight single-use recovery codes the settings page lists. Turning it off asks for the current password, so a session someone else is holding is not enough to undo it.
|
|
112
|
+
|
|
113
|
+
The secret and the recovery codes are encrypted in the database with Active Record Encryption. That normally needs three keys a generated application does not have, so Kaze ships a `config/initializers/active_record_encryption.rb` that derives them from `secret_key_base`. Nothing to configure, with one consequence worth knowing: **rotating `secret_key_base` makes existing secrets unreadable**, and anyone who has turned two factor on will need to set it up again. To manage the keys separately instead, run `bin/rails db:encryption:init` and move them into your credentials.
|
|
114
|
+
|
|
115
|
+
Both forms are rate limited — five attempts a minute on the challenge, ten on login. Rails counts these in the cache, so Kaze sets the test environment to the memory store, since the null store a generated application uses there would accept every write and let a limit look like it was working while it did nothing.
|
|
116
|
+
|
|
117
|
+
The model side lives in a `TwoFactorAuthenticatable` concern on `User`, next to `CanResetPassword`, if you want to drive it from your own code.
|
|
118
|
+
|
|
107
119
|
## Upgrading
|
|
108
120
|
|
|
109
121
|
Kaze copies files into your application and does not own them afterwards, so re-running `kaze install` cannot know whether a file it no longer ships was left over from an older version or written by you. `kaze upgrade` resolves that by asking:
|
|
@@ -82,12 +82,26 @@ class Kaze::Commands::App::BaseStack
|
|
|
82
82
|
Dir.children("#{Dir.pwd}/db/migrate").each { |file| replace_in_file(/ActiveRecord::Migration$/, "ActiveRecord::Migration[#{railsVersion}]", "#{Dir.pwd}/db/migrate/#{file}") }
|
|
83
83
|
end
|
|
84
84
|
|
|
85
|
+
# Rate limiting counts attempts in the cache, and a generated application uses
|
|
86
|
+
# the null store in test -- which silently accepts every write, so a limit can
|
|
87
|
+
# never trip and could never be tested. Development and production already
|
|
88
|
+
# have real stores.
|
|
89
|
+
def enable_test_cache_store
|
|
90
|
+
path = "#{Dir.pwd}/config/environments/test.rb"
|
|
91
|
+
|
|
92
|
+
return unless File.exist?(path)
|
|
93
|
+
|
|
94
|
+
replace_in_file(/^ config\.cache_store = :null_store$/, ' config.cache_store = :memory_store', path)
|
|
95
|
+
end
|
|
96
|
+
|
|
85
97
|
def install_tests
|
|
86
98
|
ensure_directory_exists("#{Dir.pwd}/test/factories")
|
|
87
99
|
ensure_directory_exists("#{Dir.pwd}/test/integration")
|
|
100
|
+
ensure_directory_exists("#{Dir.pwd}/test/models")
|
|
88
101
|
FileUtils.copy_file("#{stubs_path}/default/test/test_helper.rb", "#{Dir.pwd}/test/test_helper.rb")
|
|
89
102
|
FileUtils.copy_entry("#{stubs_path}/default/test/factories", "#{Dir.pwd}/test/factories")
|
|
90
103
|
FileUtils.copy_entry("#{stubs_path}/default/test/integration", "#{Dir.pwd}/test/integration")
|
|
104
|
+
FileUtils.copy_entry("#{stubs_path}/default/test/models", "#{Dir.pwd}/test/models")
|
|
91
105
|
end
|
|
92
106
|
|
|
93
107
|
def ensure_directory_exists(path)
|
|
@@ -20,7 +20,7 @@ class Kaze::Commands::App::HotwireStack < Kaze::Commands::App::BaseStack
|
|
|
20
20
|
def install
|
|
21
21
|
# Gems...
|
|
22
22
|
return unless remove_gems([ 'sprockets-rails', 'stimulus-rails' ])
|
|
23
|
-
return unless install_gems([ 'propshaft', 'view_component', 'turbo-rails', 'dotenv', 'bcrypt' ])
|
|
23
|
+
return unless install_gems([ 'propshaft', 'view_component', 'turbo-rails', 'dotenv', 'bcrypt', 'rotp', 'rqrcode' ])
|
|
24
24
|
return unless run_bundle_command("#{bundle_command} add tailwindcss-rails --version=\"~> 4.0\"").success?
|
|
25
25
|
return unless install_gems([ 'hotwire-livereload' ], 'development')
|
|
26
26
|
return unless install_gems([ 'factory_bot_rails', 'faker' ], 'development, test')
|
|
@@ -61,9 +61,12 @@ class Kaze::Commands::App::HotwireStack < Kaze::Commands::App::BaseStack
|
|
|
61
61
|
|
|
62
62
|
# Tests...
|
|
63
63
|
install_tests
|
|
64
|
+
enable_test_cache_store
|
|
64
65
|
|
|
65
66
|
# Routes...
|
|
66
67
|
FileUtils.copy_file("#{stubs_path}/default/config/routes.rb", "#{Dir.pwd}/config/routes.rb")
|
|
68
|
+
ensure_directory_exists("#{Dir.pwd}/config/initializers")
|
|
69
|
+
FileUtils.copy_file("#{stubs_path}/default/config/initializers/active_record_encryption.rb", "#{Dir.pwd}/config/initializers/active_record_encryption.rb")
|
|
67
70
|
|
|
68
71
|
# Migrations...
|
|
69
72
|
install_migrations
|
|
@@ -25,7 +25,7 @@ class Kaze::Commands::App::InertiaReactStack < Kaze::Commands::App::BaseStack
|
|
|
25
25
|
def install
|
|
26
26
|
# Gems...
|
|
27
27
|
return unless remove_gems([ 'sprockets-rails', 'turbo-rails', 'stimulus-rails' ])
|
|
28
|
-
return unless install_gems([ 'propshaft', 'inertia_rails', 'vite_rails', 'dotenv', 'bcrypt', 'js-routes' ])
|
|
28
|
+
return unless install_gems([ 'propshaft', 'inertia_rails', 'vite_rails', 'dotenv', 'bcrypt', 'rotp', 'rqrcode', 'js-routes' ])
|
|
29
29
|
return unless run_bundle_command("#{bundle_command} add tailwindcss-rails --version=\"~> 4.0\"").success?
|
|
30
30
|
return unless install_gems([ 'factory_bot_rails', 'faker' ], 'development, test')
|
|
31
31
|
|
|
@@ -64,6 +64,7 @@ class Kaze::Commands::App::InertiaReactStack < Kaze::Commands::App::BaseStack
|
|
|
64
64
|
|
|
65
65
|
# Tests...
|
|
66
66
|
install_tests
|
|
67
|
+
enable_test_cache_store
|
|
67
68
|
FileUtils.copy_entry("#{stubs_path}/inertia-common/test/integration", "#{Dir.pwd}/test/integration")
|
|
68
69
|
|
|
69
70
|
# Routes...
|
|
@@ -79,6 +80,7 @@ class Kaze::Commands::App::InertiaReactStack < Kaze::Commands::App::BaseStack
|
|
|
79
80
|
FileUtils.copy_file("#{stubs_path}/inertia-common/config/vite.json", "#{Dir.pwd}/config/vite.json")
|
|
80
81
|
ensure_directory_exists("#{Dir.pwd}/config/initializers")
|
|
81
82
|
FileUtils.copy_file("#{stubs_path}/inertia-common/config/initializers/inertia_rails.rb", "#{Dir.pwd}/config/initializers/inertia_rails.rb")
|
|
83
|
+
FileUtils.copy_file("#{stubs_path}/default/config/initializers/active_record_encryption.rb", "#{Dir.pwd}/config/initializers/active_record_encryption.rb")
|
|
82
84
|
FileUtils.copy_file("#{stubs_path}/inertia-react-ts/components.json", "#{Dir.pwd}/components.json")
|
|
83
85
|
FileUtils.copy_file("#{stubs_path}/inertia-react-ts/tsconfig.json", "#{Dir.pwd}/tsconfig.json")
|
|
84
86
|
FileUtils.copy_file("#{stubs_path}/inertia-react-ts/vite.config.ts", "#{Dir.pwd}/vite.config.ts")
|
|
@@ -26,7 +26,7 @@ class Kaze::Commands::App::InertiaVueStack < Kaze::Commands::App::BaseStack
|
|
|
26
26
|
def install
|
|
27
27
|
# Gems...
|
|
28
28
|
return unless remove_gems([ 'sprockets-rails', 'turbo-rails', 'stimulus-rails' ])
|
|
29
|
-
return unless install_gems([ 'propshaft', 'inertia_rails', 'vite_rails', 'dotenv', 'bcrypt', 'js-routes' ])
|
|
29
|
+
return unless install_gems([ 'propshaft', 'inertia_rails', 'vite_rails', 'dotenv', 'bcrypt', 'rotp', 'rqrcode', 'js-routes' ])
|
|
30
30
|
return unless run_bundle_command("#{bundle_command} add tailwindcss-rails --version=\"~> 4.0\"").success?
|
|
31
31
|
return unless install_gems([ 'factory_bot_rails', 'faker' ], 'development, test')
|
|
32
32
|
|
|
@@ -65,6 +65,7 @@ class Kaze::Commands::App::InertiaVueStack < Kaze::Commands::App::BaseStack
|
|
|
65
65
|
|
|
66
66
|
# Tests...
|
|
67
67
|
install_tests
|
|
68
|
+
enable_test_cache_store
|
|
68
69
|
FileUtils.copy_entry("#{stubs_path}/inertia-common/test/integration", "#{Dir.pwd}/test/integration")
|
|
69
70
|
|
|
70
71
|
# Routes...
|
|
@@ -80,6 +81,7 @@ class Kaze::Commands::App::InertiaVueStack < Kaze::Commands::App::BaseStack
|
|
|
80
81
|
FileUtils.copy_file("#{stubs_path}/inertia-common/config/vite.json", "#{Dir.pwd}/config/vite.json")
|
|
81
82
|
ensure_directory_exists("#{Dir.pwd}/config/initializers")
|
|
82
83
|
FileUtils.copy_file("#{stubs_path}/inertia-common/config/initializers/inertia_rails.rb", "#{Dir.pwd}/config/initializers/inertia_rails.rb")
|
|
84
|
+
FileUtils.copy_file("#{stubs_path}/default/config/initializers/active_record_encryption.rb", "#{Dir.pwd}/config/initializers/active_record_encryption.rb")
|
|
83
85
|
FileUtils.copy_file("#{stubs_path}/inertia-vue-ts/components.json", "#{Dir.pwd}/components.json")
|
|
84
86
|
FileUtils.copy_file("#{stubs_path}/inertia-vue-ts/tsconfig.json", "#{Dir.pwd}/tsconfig.json")
|
|
85
87
|
FileUtils.copy_file("#{stubs_path}/inertia-vue-ts/vite.config.ts", "#{Dir.pwd}/vite.config.ts")
|
data/lib/kaze/version.rb
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
class Auth::LoginForm < ApplicationForm
|
|
2
2
|
attr_accessor :email, :password, :remember
|
|
3
|
+
attr_reader :user
|
|
3
4
|
|
|
4
5
|
validates :email, presence: true, email: true
|
|
5
6
|
validates :password, presence: true
|
|
6
7
|
|
|
8
|
+
# Checks the credentials without logging anyone in, so the caller can decide
|
|
9
|
+
# whether a second factor is still owed.
|
|
7
10
|
def authenticate
|
|
8
11
|
return if invalid?
|
|
9
12
|
|
|
10
|
-
|
|
13
|
+
@user = Current.auth.validate({ email: @email, password: @password })
|
|
14
|
+
|
|
15
|
+
errors.add(:email, message: 'These credentials do not match our records.') if @user.nil?
|
|
11
16
|
end
|
|
12
17
|
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
class Auth::TwoFactorChallengeForm < ApplicationForm
|
|
2
|
+
attr_accessor :code, :recovery_code
|
|
3
|
+
|
|
4
|
+
validate :code_or_recovery_code_present
|
|
5
|
+
|
|
6
|
+
# A recovery code is offered as the way in when the authenticator is not to
|
|
7
|
+
# hand, so whichever was filled in is the one that gets checked.
|
|
8
|
+
def authenticate(user)
|
|
9
|
+
return if invalid?
|
|
10
|
+
|
|
11
|
+
if recovery_code.present?
|
|
12
|
+
errors.add(:recovery_code, message: 'That recovery code is not valid.') unless user.consume_two_factor_recovery_code(recovery_code)
|
|
13
|
+
else
|
|
14
|
+
errors.add(:code, message: 'That code is not valid.') unless user.verify_two_factor_code(code)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
def code_or_recovery_code_present
|
|
20
|
+
return if code.present? || recovery_code.present?
|
|
21
|
+
|
|
22
|
+
errors.add(:code, message: 'Enter the code from your authenticator app, or a recovery code.')
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# Turning two factor off removes a protection, so it asks for the password. A
|
|
2
|
+
# stolen session should not be enough to undo it.
|
|
3
|
+
class DisableTwoFactorForm < ApplicationForm
|
|
4
|
+
attr_accessor :password
|
|
5
|
+
|
|
6
|
+
validates :password, presence: true, current_password: true
|
|
7
|
+
end
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# Time-based one-time passwords, in the shape Laravel's starter kits use: a
|
|
2
|
+
# secret is generated first and only counts as enabled once the user has proved
|
|
3
|
+
# they can read codes from it, so nobody can lock themselves out by walking away
|
|
4
|
+
# from a half-finished setup.
|
|
5
|
+
module TwoFactorAuthenticatable
|
|
6
|
+
extend ActiveSupport::Concern
|
|
7
|
+
|
|
8
|
+
RECOVERY_CODE_COUNT = 8
|
|
9
|
+
|
|
10
|
+
# A window of one step either side, so a code is still accepted if the user's
|
|
11
|
+
# clock is a little out or they typed it as it was expiring.
|
|
12
|
+
DRIFT = 30
|
|
13
|
+
|
|
14
|
+
included do
|
|
15
|
+
encrypts :two_factor_secret
|
|
16
|
+
encrypts :two_factor_recovery_codes
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def two_factor_enabled?
|
|
20
|
+
two_factor_secret.present? && two_factor_confirmed_at.present?
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# A secret exists but has not been confirmed. Setup was started and not
|
|
24
|
+
# finished; the account is not protected yet.
|
|
25
|
+
def two_factor_pending?
|
|
26
|
+
two_factor_secret.present? && two_factor_confirmed_at.nil?
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Starts setup, replacing anything half-finished from a previous attempt.
|
|
30
|
+
def prepare_two_factor
|
|
31
|
+
update!(
|
|
32
|
+
two_factor_secret: ROTP::Base32.random,
|
|
33
|
+
two_factor_recovery_codes: generate_recovery_codes.to_json,
|
|
34
|
+
two_factor_confirmed_at: nil
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def confirm_two_factor(code)
|
|
39
|
+
return false unless two_factor_pending? && verify_two_factor_code(code)
|
|
40
|
+
|
|
41
|
+
update!(two_factor_confirmed_at: Time.current)
|
|
42
|
+
|
|
43
|
+
true
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def disable_two_factor
|
|
47
|
+
update!(two_factor_secret: nil, two_factor_recovery_codes: nil, two_factor_confirmed_at: nil)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def verify_two_factor_code(code)
|
|
51
|
+
return false if two_factor_secret.blank? || code.blank?
|
|
52
|
+
|
|
53
|
+
totp.verify(code.to_s.strip, drift_behind: DRIFT, drift_ahead: DRIFT).present?
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Recovery codes are single use. Returns false without consuming anything if
|
|
57
|
+
# the code is not one of ours.
|
|
58
|
+
def consume_two_factor_recovery_code(code)
|
|
59
|
+
return false if code.blank?
|
|
60
|
+
|
|
61
|
+
remaining = two_factor_recovery_codes_list
|
|
62
|
+
index = remaining.index(code.to_s.strip)
|
|
63
|
+
|
|
64
|
+
return false if index.nil?
|
|
65
|
+
|
|
66
|
+
remaining.delete_at(index)
|
|
67
|
+
update!(two_factor_recovery_codes: remaining.to_json)
|
|
68
|
+
|
|
69
|
+
true
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def two_factor_recovery_codes_list
|
|
73
|
+
return [] if two_factor_recovery_codes.blank?
|
|
74
|
+
|
|
75
|
+
JSON.parse(two_factor_recovery_codes)
|
|
76
|
+
rescue JSON::ParserError
|
|
77
|
+
[]
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def regenerate_two_factor_recovery_codes
|
|
81
|
+
update!(two_factor_recovery_codes: generate_recovery_codes.to_json)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# What the authenticator app scans. The issuer is what the user will see
|
|
85
|
+
# listed in their app, so it is the application's name rather than a URL.
|
|
86
|
+
def two_factor_provisioning_uri(issuer: nil)
|
|
87
|
+
return nil if two_factor_secret.blank?
|
|
88
|
+
|
|
89
|
+
totp(issuer: issuer).provisioning_uri(email)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def two_factor_qr_code_svg(issuer: nil)
|
|
93
|
+
uri = two_factor_provisioning_uri(issuer: issuer)
|
|
94
|
+
|
|
95
|
+
return nil if uri.nil?
|
|
96
|
+
|
|
97
|
+
RQRCode::QRCode.new(uri).as_svg(viewbox: true, use_path: true, module_size: 4)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
private
|
|
101
|
+
def totp(issuer: nil)
|
|
102
|
+
ROTP::TOTP.new(two_factor_secret, issuer: issuer || default_two_factor_issuer)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def default_two_factor_issuer
|
|
106
|
+
Rails.application.class.module_parent_name
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def generate_recovery_codes
|
|
110
|
+
Array.new(RECOVERY_CODE_COUNT) { "#{SecureRandom.alphanumeric(10)}-#{SecureRandom.alphanumeric(10)}".downcase }
|
|
111
|
+
end
|
|
112
|
+
end
|
|
@@ -38,7 +38,7 @@ class SessionGuard
|
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
def attempt(credentials = {}, remember = false)
|
|
41
|
-
|
|
41
|
+
user = validate(credentials)
|
|
42
42
|
|
|
43
43
|
return false if user.nil?
|
|
44
44
|
|
|
@@ -47,6 +47,13 @@ class SessionGuard
|
|
|
47
47
|
true
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
+
# Checks credentials and returns the user without starting a session. The two
|
|
51
|
+
# factor challenge needs this: a correct password is not enough to be logged
|
|
52
|
+
# in when a second factor is required.
|
|
53
|
+
def validate(credentials = {})
|
|
54
|
+
@last_attempted = User.authenticate_by(credentials)
|
|
55
|
+
end
|
|
56
|
+
|
|
50
57
|
def login(user, remember = false)
|
|
51
58
|
update_session(user.id)
|
|
52
59
|
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# The gap between a correct password and a completed login. The user is not
|
|
2
|
+
# logged in while this is pending -- their id is held in the session and nothing
|
|
3
|
+
# else -- so an abandoned challenge grants nothing.
|
|
4
|
+
class TwoFactorChallenge
|
|
5
|
+
EXPIRES_IN = 5.minutes
|
|
6
|
+
|
|
7
|
+
USER_KEY = :two_factor_challenge_user_id
|
|
8
|
+
REMEMBER_KEY = :two_factor_challenge_remember
|
|
9
|
+
STARTED_KEY = :two_factor_challenge_started_at
|
|
10
|
+
|
|
11
|
+
def initialize(session)
|
|
12
|
+
@session = session
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def start(user, remember: false)
|
|
16
|
+
@session[USER_KEY] = user.id
|
|
17
|
+
@session[REMEMBER_KEY] = remember
|
|
18
|
+
@session[STARTED_KEY] = Time.current.to_i
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def pending?
|
|
22
|
+
!user.nil?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def user
|
|
26
|
+
return nil if @session[USER_KEY].nil?
|
|
27
|
+
|
|
28
|
+
if expired?
|
|
29
|
+
clear
|
|
30
|
+
return nil
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
@user ||= User.find_by(id: @session[USER_KEY])
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def remember?
|
|
37
|
+
@session[REMEMBER_KEY].present?
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def clear
|
|
41
|
+
@session.delete(USER_KEY)
|
|
42
|
+
@session.delete(REMEMBER_KEY)
|
|
43
|
+
@session.delete(STARTED_KEY)
|
|
44
|
+
@user = nil
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
# A challenge left open is a password already given, so it should not wait
|
|
49
|
+
# around indefinitely for the second factor.
|
|
50
|
+
def expired?
|
|
51
|
+
started_at = @session[STARTED_KEY]
|
|
52
|
+
|
|
53
|
+
started_at.nil? || Time.zone.at(started_at.to_i) < EXPIRES_IN.ago
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Active Record Encryption needs three keys, and a generated application has
|
|
2
|
+
# none. Deriving them from secret_key_base means encrypted attributes work the
|
|
3
|
+
# moment the application boots, without a second set of secrets to manage.
|
|
4
|
+
#
|
|
5
|
+
# The consequence is that rotating secret_key_base makes existing ciphertext
|
|
6
|
+
# unreadable. To manage the keys separately instead, run
|
|
7
|
+
# `bin/rails db:encryption:init` and move them into your credentials.
|
|
8
|
+
Rails.application.configure do
|
|
9
|
+
generator = ActiveSupport::KeyGenerator.new(
|
|
10
|
+
Rails.application.secret_key_base,
|
|
11
|
+
hash_digest_class: OpenSSL::Digest::SHA256
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
config.active_record.encryption.primary_key =
|
|
15
|
+
generator.generate_key('active_record_encryption_primary_key', 32)
|
|
16
|
+
config.active_record.encryption.deterministic_key =
|
|
17
|
+
generator.generate_key('active_record_encryption_deterministic_key', 32)
|
|
18
|
+
config.active_record.encryption.key_derivation_salt =
|
|
19
|
+
generator.generate_key('active_record_encryption_key_derivation_salt', 32)
|
|
20
|
+
end
|
|
@@ -19,6 +19,9 @@ Rails.application.routes.draw do
|
|
|
19
19
|
post 'verify-email', to: 'auth/email_verification_notification#create', as: :verification_send
|
|
20
20
|
get 'verify-email/:id/:hash', to: 'auth/verified_email#create', as: :verification_verify
|
|
21
21
|
|
|
22
|
+
get 'two-factor-challenge', to: 'auth/two_factor_challenge#new', as: :two_factor_challenge
|
|
23
|
+
post 'two-factor-challenge', to: 'auth/two_factor_challenge#create', as: :two_factor_challenge_store
|
|
24
|
+
|
|
22
25
|
post 'logout', to: 'auth/authenticated_session#destroy', as: :logout
|
|
23
26
|
|
|
24
27
|
get 'dashboard', to: 'dashboard#index', as: :dashboard
|
|
@@ -33,6 +36,12 @@ Rails.application.routes.draw do
|
|
|
33
36
|
get 'password', to: 'password#edit', as: :password_edit
|
|
34
37
|
put 'password', to: 'password#update', as: :password_update
|
|
35
38
|
|
|
39
|
+
get 'two-factor', to: 'two_factor#edit', as: :two_factor_edit
|
|
40
|
+
post 'two-factor', to: 'two_factor#create', as: :two_factor_store
|
|
41
|
+
patch 'two-factor', to: 'two_factor#update', as: :two_factor_update
|
|
42
|
+
delete 'two-factor', to: 'two_factor#destroy', as: :two_factor_destroy
|
|
43
|
+
post 'two-factor/recovery-codes', to: 'two_factor#recovery_codes', as: :two_factor_recovery_codes
|
|
44
|
+
|
|
36
45
|
get 'appearance', to: 'appearance#edit', as: :appearance_edit
|
|
37
46
|
end
|
|
38
47
|
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
class AddTwoFactorToUsers < ActiveRecord::Migration
|
|
2
|
+
def self.up
|
|
3
|
+
change_table :users, bulk: true do |table|
|
|
4
|
+
table.text :two_factor_secret
|
|
5
|
+
table.text :two_factor_recovery_codes
|
|
6
|
+
table.timestamp :two_factor_confirmed_at
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def self.down
|
|
11
|
+
change_table :users, bulk: true do |table|
|
|
12
|
+
table.remove :two_factor_secret, :two_factor_recovery_codes, :two_factor_confirmed_at
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -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
|