kaze 2.1.1 → 2.2.1
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 +63 -1
- data/README.md +21 -1
- data/lib/kaze/commands/app/base_stack.rb +37 -3
- data/lib/kaze/commands/app/hotwire_stack.rb +5 -3
- data/lib/kaze/commands/app/inertia_react_stack.rb +4 -3
- data/lib/kaze/commands/app/inertia_vue_stack.rb +4 -3
- 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: ab4d236e7d08529caaafe2924edaab3a90cb9742fe5dcf916cb6e188bf453d38
|
|
4
|
+
data.tar.gz: 57eb011f2173bf00a346195b91d0620777f173be7acf22d97b97cf94b803e6bf
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: af68391e4cce4dace60eb32e614b55c6ea4fe3fc6cd52f7a1c3ff614a4e9b2b5c295d31dada55c57f001ddb850f5cdfc4251c337de4176425162532f941343c2
|
|
7
|
+
data.tar.gz: 0611eeedb28e33537e0d09f85dfdbb1010caad5ef3409ca9bd84ee7b1fddf69197343ffa416c2ca003ddd496849e606bdb76632cf0f909d0dd023dd86a9fecf1
|
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,66 @@ 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.1] - 2026-10-27
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- The install now says what to do next: `Run bin/rails db:prepare to apply the migrations Kaze installed.` A new application reaches its migrations through `bin/setup`, but an upgrade has no such step, so 2.2.0 handed existing applications the two factor settings page and login challenge while their database still had none of the columns behind them. The `## Upgrading` section of the README says it too.
|
|
14
|
+
|
|
15
|
+
### Fixed
|
|
16
|
+
|
|
17
|
+
- `kaze install` and `kaze upgrade` no longer empty files. `replace_in_file` wrote the result of `String#gsub!` back to disk, and the bang form returns `nil` when it matched nothing — so `File.write` truncated the file to nothing. Every call site passes a pattern that can legitimately be absent, either because an earlier install already applied it or because the application edited it out, and each of those was a file silently destroyed. It is `gsub` now, which returns the string unchanged when it matches nothing.
|
|
18
|
+
|
|
19
|
+
Two files were reachable this way. An application's own migrations were emptied on the **first** upgrade: the rewrite that stamps the Rails version into Kaze's migrations walked the whole of `db/migrate`, and the pattern it uses only fits the ones Kaze ships, since a generated migration already carries its `[8.1]` suffix. `rails db:migrate` did not complain, because the version was already recorded and Rails never read the file again — the loss surfaced later, as `NameError: uninitialized constant` on a rollback, or as a schema that could no longer be rebuilt from its migrations. This dates from 2024-07-01 and has been in every release since; it took an application with a migration of its own to show, which is why neither the test suite nor the 1.4.0 upgrade test caught it. The rewrite is now scoped to the files Kaze copied.
|
|
20
|
+
|
|
21
|
+
`config/environments/test.rb` was emptied on the **second** upgrade, and on any upgrade of an application already on 2.2.0. `enable_test_cache_store` rewrites `:null_store` to `:memory_store`, so a second run found nothing to match and truncated the file; the suite then came apart with 15 failures and 31 errors. That one was introduced in 2.2.0, a few hours before this fix.
|
|
22
|
+
|
|
23
|
+
- `install_migrations` no longer crashes if `bin/rails version` prints something unexpected, for the same reason: it called `gsub!` on the output and then `split` on the result.
|
|
24
|
+
|
|
25
|
+
## [2.2.0] - 2026-10-27
|
|
26
|
+
|
|
27
|
+
### Added
|
|
28
|
+
|
|
29
|
+
- 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.
|
|
30
|
+
|
|
31
|
+
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.
|
|
32
|
+
|
|
33
|
+
- 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.
|
|
34
|
+
|
|
35
|
+
Turning it off asks for the current password. Removing a protection should take more than a session someone else may be holding.
|
|
36
|
+
|
|
37
|
+
- 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.
|
|
38
|
+
|
|
39
|
+
`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.
|
|
40
|
+
|
|
41
|
+
- 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.
|
|
42
|
+
|
|
43
|
+
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.
|
|
44
|
+
|
|
45
|
+
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.
|
|
46
|
+
|
|
47
|
+
### Fixed
|
|
48
|
+
|
|
49
|
+
- 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`.
|
|
50
|
+
|
|
51
|
+
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.
|
|
52
|
+
|
|
53
|
+
`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.
|
|
54
|
+
|
|
55
|
+
### Documentation
|
|
56
|
+
|
|
57
|
+
- 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.
|
|
58
|
+
|
|
59
|
+
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.
|
|
60
|
+
|
|
61
|
+
||||||| 1466c8e
|
|
62
|
+
|
|
63
|
+
### Internal
|
|
64
|
+
|
|
65
|
+
- `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.
|
|
66
|
+
|
|
67
|
+
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.
|
|
68
|
+
|
|
9
69
|
## [2.1.1] - 2026-10-25
|
|
10
70
|
|
|
11
71
|
### Fixed
|
|
@@ -217,7 +277,9 @@ Together these take a freshly scaffolded Inertia application from seven npm advi
|
|
|
217
277
|
|
|
218
278
|
Releases before 1.0.0 are not catalogued here. See the [tags](https://github.com/cuonggt/kaze/tags) for their history.
|
|
219
279
|
|
|
220
|
-
[Unreleased]: https://github.com/cuonggt/kaze/compare/v2.
|
|
280
|
+
[Unreleased]: https://github.com/cuonggt/kaze/compare/v2.2.1...HEAD
|
|
281
|
+
[2.2.1]: https://github.com/cuonggt/kaze/compare/v2.2.0...v2.2.1
|
|
282
|
+
[2.2.0]: https://github.com/cuonggt/kaze/compare/v2.1.1...v2.2.0
|
|
221
283
|
[2.1.1]: https://github.com/cuonggt/kaze/compare/v2.1.0...v2.1.1
|
|
222
284
|
[2.1.0]: https://github.com/cuonggt/kaze/compare/v2.0.1...v2.1.0
|
|
223
285
|
[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:
|
|
@@ -114,4 +126,12 @@ kaze upgrade react
|
|
|
114
126
|
|
|
115
127
|
It lists the files an earlier Kaze installed that this version no longer ships, waits for you to agree, removes them, and then runs the normal install. Only files Kaze itself shipped are ever listed — anything you added alongside them is untouched, and a directory is removed only once it is empty. Pass `--dry-run` to see the list and stop, or `--force` to skip the prompt in a script.
|
|
116
128
|
|
|
129
|
+
An upgrade can bring migrations with it, so run them afterwards:
|
|
130
|
+
|
|
131
|
+
```
|
|
132
|
+
bin/rails db:prepare
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Upgrading to 2.2 in particular adds the columns two factor authentication needs. Until they are applied, the settings page and the login challenge are reading columns the database does not have.
|
|
136
|
+
|
|
117
137
|
Upgrading from 1.x to 2.x matters on the React stack in particular: `tsc` typechecks every file under `app/javascript`, so the old `Pages/Profile/Edit.tsx` fails the build until it is gone.
|
|
@@ -74,28 +74,62 @@ class Kaze::Commands::App::BaseStack
|
|
|
74
74
|
|
|
75
75
|
def install_migrations
|
|
76
76
|
stdin, _ = Open3.capture3("#{Dir.pwd}/bin/rails version")
|
|
77
|
-
versions = stdin.gsub
|
|
77
|
+
versions = stdin.gsub('Rails ', '').split('.')
|
|
78
78
|
railsVersion = [ versions[0], versions[1] ].join('.')
|
|
79
79
|
|
|
80
80
|
ensure_directory_exists("#{Dir.pwd}/db/migrate")
|
|
81
|
+
|
|
82
|
+
# Only the migrations Kaze ships get the version written into them. The
|
|
83
|
+
# application's own already carry one, and are none of our business.
|
|
84
|
+
migrations = Dir.children("#{stubs_path}/default/db/migrate")
|
|
85
|
+
|
|
81
86
|
FileUtils.copy_entry("#{stubs_path}/default/db/migrate", "#{Dir.pwd}/db/migrate")
|
|
82
|
-
|
|
87
|
+
migrations.each { |file| replace_in_file(/ActiveRecord::Migration$/, "ActiveRecord::Migration[#{railsVersion}]", "#{Dir.pwd}/db/migrate/#{file}") }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Rate limiting counts attempts in the cache, and a generated application uses
|
|
91
|
+
# the null store in test -- which silently accepts every write, so a limit can
|
|
92
|
+
# never trip and could never be tested. Development and production already
|
|
93
|
+
# have real stores.
|
|
94
|
+
def enable_test_cache_store
|
|
95
|
+
path = "#{Dir.pwd}/config/environments/test.rb"
|
|
96
|
+
|
|
97
|
+
return unless File.exist?(path)
|
|
98
|
+
|
|
99
|
+
replace_in_file(/^ config\.cache_store = :null_store$/, ' config.cache_store = :memory_store', path)
|
|
83
100
|
end
|
|
84
101
|
|
|
85
102
|
def install_tests
|
|
86
103
|
ensure_directory_exists("#{Dir.pwd}/test/factories")
|
|
87
104
|
ensure_directory_exists("#{Dir.pwd}/test/integration")
|
|
105
|
+
ensure_directory_exists("#{Dir.pwd}/test/models")
|
|
88
106
|
FileUtils.copy_file("#{stubs_path}/default/test/test_helper.rb", "#{Dir.pwd}/test/test_helper.rb")
|
|
89
107
|
FileUtils.copy_entry("#{stubs_path}/default/test/factories", "#{Dir.pwd}/test/factories")
|
|
90
108
|
FileUtils.copy_entry("#{stubs_path}/default/test/integration", "#{Dir.pwd}/test/integration")
|
|
109
|
+
FileUtils.copy_entry("#{stubs_path}/default/test/models", "#{Dir.pwd}/test/models")
|
|
91
110
|
end
|
|
92
111
|
|
|
93
112
|
def ensure_directory_exists(path)
|
|
94
113
|
FileUtils.mkdir_p(path) unless File.directory?(path)
|
|
95
114
|
end
|
|
96
115
|
|
|
116
|
+
# Kaze ships migrations, and an install that adds one leaves the application
|
|
117
|
+
# holding code for columns the database does not have yet. A new application
|
|
118
|
+
# reaches them through `bin/setup`, but an upgrade of an existing one has no
|
|
119
|
+
# such step, so say it here where the person is already looking.
|
|
120
|
+
def say_installed_successfully
|
|
121
|
+
say ''
|
|
122
|
+
say 'Kaze scaffolding installed successfully.', :green
|
|
123
|
+
say ''
|
|
124
|
+
say 'Run `bin/rails db:prepare` to apply the migrations Kaze installed.', :yellow
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# NOTE: `gsub`, not `gsub!`. The bang form returns nil when it matched
|
|
128
|
+
# nothing, and writing that back truncates the file to nothing -- so a
|
|
129
|
+
# pattern that had already been applied, or that an application had edited
|
|
130
|
+
# out, silently emptied the file instead of leaving it alone.
|
|
97
131
|
def replace_in_file(search, replace, path)
|
|
98
|
-
File.write(path, File.read(path).gsub
|
|
132
|
+
File.write(path, File.read(path).gsub(search, replace))
|
|
99
133
|
end
|
|
100
134
|
|
|
101
135
|
def run_command(command)
|
|
@@ -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
|
|
@@ -76,8 +79,7 @@ class Kaze::Commands::App::HotwireStack < Kaze::Commands::App::BaseStack
|
|
|
76
79
|
FileUtils.copy_file("#{stubs_path}/default/Procfile.dev", "#{Dir.pwd}/Procfile.dev")
|
|
77
80
|
return unless run_command("#{Dir.pwd}/bin/rails tailwindcss:build").success?
|
|
78
81
|
|
|
79
|
-
|
|
80
|
-
say 'Kaze scaffolding installed successfully.', :green
|
|
82
|
+
say_installed_successfully
|
|
81
83
|
|
|
82
84
|
true
|
|
83
85
|
end
|
|
@@ -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")
|
|
@@ -106,8 +108,7 @@ class Kaze::Commands::App::InertiaReactStack < Kaze::Commands::App::BaseStack
|
|
|
106
108
|
|
|
107
109
|
return unless node_dependencies_installed
|
|
108
110
|
|
|
109
|
-
|
|
110
|
-
say 'Kaze scaffolding installed successfully.', :green
|
|
111
|
+
say_installed_successfully
|
|
111
112
|
|
|
112
113
|
true
|
|
113
114
|
end
|
|
@@ -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")
|
|
@@ -107,8 +109,7 @@ class Kaze::Commands::App::InertiaVueStack < Kaze::Commands::App::BaseStack
|
|
|
107
109
|
|
|
108
110
|
return unless node_dependencies_installed
|
|
109
111
|
|
|
110
|
-
|
|
111
|
-
say 'Kaze scaffolding installed successfully.', :green
|
|
112
|
+
say_installed_successfully
|
|
112
113
|
|
|
113
114
|
true
|
|
114
115
|
end
|
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
|