Skip to content

Commit

Permalink
Automatically add argon2 and JWT gems to Gemfile
Browse files Browse the repository at this point in the history
  • Loading branch information
janko committed Dec 3, 2024
1 parent 63fb437 commit 0c06558
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,15 @@ $ rails generate rodauth:install users
If you want Rodauth endpoints to be exposed via [JSON API]:

```sh
$ rails generate rodauth:install --json # regular authentication using the Rails session
$ rails generate rodauth:install --json # cookied-based authentication
# or
$ rails generate rodauth:install --jwt # token authentication via the "Authorization" header
$ bundle add jwt
$ rails generate rodauth:install --jwt # token-based authentication
```

To use Argon2 instead of bcrypt for password hashing:

```sh
$ rails generate rodauth:install --argon2
$ bundle add argon2
```

## Usage
Expand Down
7 changes: 6 additions & 1 deletion lib/generators/rodauth/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ def add_gems
gem "sequel-activerecord_connection", "~> 2.0", comment: "Enables Sequel to use Active Record's database connection"
gem "after_commit_everywhere", "~> 1.1", comment: "Required for Sequel's transaction hooks to work in all cases (on Active Record < 7.2)" if ActiveRecord.version < Gem::Version.new("7.2")
end
unless argon2?
if argon2?
gem "argon2", "~> 2.3", comment: "Used by Rodauth for password hashing"
else
gem "bcrypt", "~> 3.1", comment: "Used by Rodauth for password hashing"
end
if jwt?
gem "jwt", "~> 2.9", comment: "Used by Rodauth for JWT support"
end
gem "tilt", "~> 2.4", comment: "Used by Rodauth for rendering built-in view and email templates"
end

Expand Down
11 changes: 7 additions & 4 deletions test/generators/install_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class InstallGeneratorTest < Rails::Generators::TestCase
end
assert_file "app/misc/rodauth_main.rb", /convert_token_id_to_integer\? { Account.columns_hash\["id"\]\.type == :integer }/

assert_file "Gemfile", /gem "sequel-activerecord_connection", "~> 2.0"/
assert_file "Gemfile", /gem "after_commit_everywhere", "~> 1.1"/ if ActiveRecord.version < Gem::Version.new("7.2")
assert_file "Gemfile", /gem "sequel-activerecord_connection"/
assert_file "Gemfile", /gem "after_commit_everywhere"/ if ActiveRecord.version < Gem::Version.new("7.2")
end

test "app" do
Expand All @@ -62,8 +62,8 @@ class InstallGeneratorTest < Rails::Generators::TestCase
assert_file "app/misc/rodauth_main.rb", /logout_redirect/
assert_file "app/misc/rodauth_main.rb", /# accounts_table :users/

assert_file "Gemfile", /gem "bcrypt", "~> 3.1"/
assert_file "Gemfile", /gem "tilt", "~> 2.4"/
assert_file "Gemfile", /gem "bcrypt"/
assert_file "Gemfile", /gem "tilt"/
end

test "app with --json option" do
Expand All @@ -78,6 +78,8 @@ class InstallGeneratorTest < Rails::Generators::TestCase

assert_file "app/misc/rodauth_main.rb", /:login, :logout, :jwt,$/
assert_file "app/misc/rodauth_main.rb", /jwt_secret /

assert_file "Gemfile", /gem "jwt"/
end

test "app with --argon2 option" do
Expand All @@ -87,6 +89,7 @@ class InstallGeneratorTest < Rails::Generators::TestCase
assert_file "app/misc/rodauth_main.rb", /argon2_secret/

assert_file "Gemfile" do |content|
assert_includes content, "argon2"
refute_includes content, "bcrypt"
end
end
Expand Down

0 comments on commit 0c06558

Please sign in to comment.