Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a variant which sets up 2FA with devise #77

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jobs:
- name: devise
config_path: "ci/configs/devise.yml"
skips: --skip-javascript
- name: devise_2fa
config_path: "ci/configs/devise_2fa.yml"
skips: --skip-javascript
- name: basic_with_skips
config_path: "ci/configs/basic.yml"
skips: --skip-active-storage --skip-spring --skip-javascript
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,8 @@ CONFIG_PATH="ci/configs/react.yml" APP_NAME="enterprise" ./ci/bin/build-and-test
# because the template is run by `rails new` which uses the rails app dir as
# it's working dir, hence the `../` at the start.
#
rm -rf mydemoapp && CONFIG_PATH="../ci/configs/react.yml" rails new mydemoapp -d postgresql --skip-javascript -m ./template.rb
rm -rf demoapp
psql -c "DROP DATABASE IF EXISTS demoapp_development;"
psql -c "DROP DATABASE IF EXISTS demoapp_test;"
CONFIG_PATH="../ci/configs/react.yml" rails new mydemoapp -d postgresql --skip-javascript -m ./template.rb
```





1 change: 1 addition & 0 deletions ackama_rails_template.config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ git_repo_url: ""
# use these flags to enable features in the rails app created by this template.
apply_variant_react: false
apply_variant_devise: false
apply_variant_devise_2fa: false
apply_variant_sidekiq: false
apply_variant_typescript: false
1 change: 1 addition & 0 deletions ci/configs/basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ production_hostname: "www.example.com"
git_repo_url: ""
apply_variant_react: false
apply_variant_devise: false
apply_variant_devise_2fa: false
apply_variant_sidekiq: false
apply_variant_typescript: false
1 change: 1 addition & 0 deletions ci/configs/devise.yml → ci/configs/devise_2fa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ production_hostname: "www.example.com"
git_repo_url: ""
apply_variant_react: false
apply_variant_devise: true
apply_variant_devise_2fa: true
apply_variant_sidekiq: false
apply_variant_typescript: false
1 change: 1 addition & 0 deletions ci/configs/react.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ production_hostname: "www.example.com"
git_repo_url: ""
apply_variant_react: true
apply_variant_devise: false
apply_variant_devise_2fa: false
apply_variant_sidekiq: false
apply_variant_typescript: false
1 change: 1 addition & 0 deletions ci/configs/sidekiq.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ production_hostname: "www.example.com"
git_repo_url: ""
apply_variant_react: false
apply_variant_devise: false
apply_variant_devise_2fa: false
apply_variant_sidekiq: true
apply_variant_typescript: false
1 change: 1 addition & 0 deletions ci/configs/typescript.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ production_hostname: "www.example.com"
git_repo_url: ""
apply_variant_react: false
apply_variant_devise: false
apply_variant_devise_2fa: false
apply_variant_sidekiq: false
apply_variant_typescript: true
3 changes: 3 additions & 0 deletions rubocop.yml.tt
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ RSpec/ExampleLength:

RSpec/FactoryBot/SyntaxMethods:
Enabled: false

Rails/BulkChangeTable:
Enabled: false
9 changes: 8 additions & 1 deletion template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def apply_variant_devise?
@yaml_config.fetch("apply_variant_devise")
end

def apply_variant_devise_2fa?
@yaml_config.fetch("apply_variant_devise")
end

def apply_variant_sidekiq?
@yaml_config.fetch("apply_variant_sidekiq")
end
Expand Down Expand Up @@ -137,7 +141,10 @@ def apply_template!

# we deliberately place this after the initial git commit because it
# contains a lot of changes and adds its own git commit
apply "variants/devise/template.rb" if $config.apply_variant_devise?
if $config.apply_variant_devise?
apply "variants/devise/template.rb"
apply "variants/devise-2fa/template.rb" if $config.apply_variant_devise_2fa?
end
end
end

Expand Down
50 changes: 50 additions & 0 deletions variants/devise-2fa/template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Allow us to copy file with root at the directory this file is in
source_paths.unshift(File.dirname(__FILE__))

def print_header(msg)
puts "=" * 80
puts msg
puts "=" * 80
end

print_header "Adding devise-two-factor, rqrcode-rails3 to Gemfile"
run "bundle add devise-two-factor"
run "bundle add rqrcode-rails3"

print_header "Adding OTP info to user model"
run "bundle exec rails generate devise_two_factor User DEVISE_TWO_FACTOR_SECRET_ENCRYPTION_KEY"

gsub_file("app/models/user.rb", "ENV['DEVISE_TWO_FACTOR_SECRET_ENCRYPTION_KEY']", "Rails.application.secrets.devise_two_factor_secret_encryption_key")

insert_into_file("config/secrets.yml", after: /\A.+secret_key_base.+\z/) do
<<-EO_LINE
devise_two_factor_secret_encryption_key: "<%= ENV['DEVISE_TWO_FACTOR_SECRET_ENCRYPTION_KEY'] %>"
EO_LINE
end

append_to_file ".env" do
eoinkelly marked this conversation as resolved.
Show resolved Hide resolved
<<~EO_LINE

# Use 'bundle exec rails secret' to generate a real value here
DEVISE_TWO_FACTOR_SECRET_ENCRYPTION_KEY=fortheloveofallyouholddeardonotusethissecretinproduction
EO_LINE
end

insert_into_file("app/views/users/sessions/new.html.erb", before: /^.*<div class="actions">/) do
<<~EO_FIELD
<div class="form__field">
<%= f.label :otp_attempt, "2FA (Two Factor Auth) code", class: "form__label" %><br />
<%= f.text_field :otp_attempt, class: "form__input" %>
</div>
EO_FIELD
end

append_to_file("config/initializers/filter_parameter_logging.rb") do
<<~EO_CONTENT
Rails.application.config.filter_parameters += %i[otp_attempt]
EO_CONTENT
end

# TODO: got to clean up stuff to keep rubocop happy
print_header "Running rubocop to clean up generated files"
run "bundle exec rubocop -A"