trek 2.5.0 → 2.7.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/.node-version +1 -1
- data/AGENTS.md +18 -14
- data/CHANGELOG.md +88 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +135 -124
- data/Makefile +9 -1
- data/README.md +17 -21
- data/app/components/trek/dashboard/tile_component/tile_component.css +9 -0
- data/app/components/trek/dashboard/tile_component/tile_component.html.slim +4 -2
- data/app/components/trek/dashboard/tile_component.rb +12 -2
- data/app/components/trek/form/content_editor_component/content_editor_component.css +9 -8
- data/app/components/trek/form/content_editor_component/content_editor_component.html.slim +19 -93
- data/app/components/trek/form/content_editor_component/content_editor_component.js +94 -251
- data/app/components/trek/form/content_editor_component/extensions/block_view.js +40 -0
- data/app/components/trek/form/content_editor_component/extensions/image_block.js +28 -26
- data/app/components/trek/form/content_editor_component/extensions/prompts_block.js +15 -26
- data/app/components/trek/form/content_editor_component/extensions/table_of_contents.js +1 -3
- data/app/components/trek/form/content_editor_component.rb +15 -0
- data/app/components/trek/form/image_field_component/image_field_component.css +40 -1
- data/app/components/trek/form/image_field_component/image_field_component.html.slim +0 -9
- data/app/components/trek/form/image_field_component/image_field_component.js +1 -2
- data/app/components/trek/form/image_field_component.rb +36 -9
- data/app/components/trek/form/password_strength_component/password_strength_component.css +2 -0
- data/app/components/trek/form/sound_field_component/sound_field_component.js +1 -2
- data/app/components/trek/security_check_component/security_check_component.css +69 -0
- data/app/components/trek/security_check_component/security_check_component.html.slim +19 -0
- data/app/components/trek/security_check_component/security_check_component.js +15 -0
- data/app/components/trek/security_check_component.rb +19 -0
- data/app/content/trek/content/cleaner.rb +53 -0
- data/app/content/trek/content/document.rb +2 -2
- data/app/content/trek/content/list_item.rb +1 -1
- data/app/content/trek/content/paragraph.rb +7 -2
- data/app/content/trek/content/prompts_block.rb +1 -2
- data/app/content/trek/content.rb +6 -0
- data/app/controllers/concerns/trek/auditable.rb +5 -2
- data/app/controllers/concerns/trek/localization.rb +6 -2
- data/app/controllers/concerns/trek/reorderable.rb +24 -6
- data/app/controllers/trek/csp_reports_controller.rb +14 -1
- data/app/controllers/trek/panels/prompts_controller.rb +1 -1
- data/app/controllers/trek/resource_controller.rb +5 -5
- data/app/jobs/trek/purge_expired_sessions_job.rb +4 -3
- data/app/jobs/trek/purge_expired_sign_in_codes_job.rb +15 -0
- data/app/models/concerns/trek/contentable.rb +15 -1
- data/app/models/concerns/trek/users/sign_in_code.rb +70 -0
- data/app/models/concerns/trek/users/sign_in_lockout.rb +4 -2
- data/app/policies/trek/security_check_policy.rb +8 -0
- data/app/uploaders/image_uploader.rb +77 -15
- data/app/views/trek/panels/links/_form.html.slim +2 -4
- data/app/views/trek/panels/prompts/create.turbo_stream.slim +0 -1
- data/config/locales/trek.en.yml +97 -11
- data/config/locales/trek.fr.yml +97 -11
- data/docs/.vitepress/config.mjs +5 -1
- data/docs/background-jobs.md +74 -0
- data/docs/content-editor.md +219 -0
- data/docs/customize.md +2 -0
- data/docs/deploy.md +69 -6
- data/docs/getting-started.md +16 -18
- data/docs/index.md +2 -2
- data/docs/personal-data.md +61 -0
- data/docs/proxies.md +2 -2
- data/docs/reference/components.md +4 -4
- data/docs/reference/concerns/contentable.md +10 -4
- data/docs/reference/concerns/formattable.md +1 -1
- data/docs/reference/concerns/fragmentable.md +12 -6
- data/docs/reference/concerns/orderable.md +2 -2
- data/docs/reference/concerns/search-engine-optimizable.md +1 -1
- data/docs/reference/concerns/searchable.md +14 -1
- data/docs/reference/concerns/sectionable.md +7 -3
- data/docs/reference/concerns/versionable.md +1 -1
- data/docs/reference/controllers.md +33 -1
- data/docs/reference/environment-variables.md +3 -2
- data/docs/reference/formatters.md +4 -31
- data/docs/reference/forms.md +7 -4
- data/docs/reference/generators/admin-user.md +1 -1
- data/docs/reference/generators/audit-log.md +12 -13
- data/docs/reference/generators/install.md +23 -11
- data/docs/reference/generators/public-accounts.md +159 -0
- data/docs/reference/generators/scaffold.md +28 -9
- data/docs/reference/generators/security-headers.md +5 -4
- data/docs/reference/generators/taxonomies.md +1 -1
- data/docs/reference/icons.md +5 -3
- data/docs/reference/index.md +10 -7
- data/docs/reference/models/current.md +5 -3
- data/docs/reference/models/page-version.md +1 -1
- data/docs/reference/models/user.md +7 -8
- data/docs/reference/uploaders.md +25 -0
- data/docs/reference/validators.md +1 -1
- data/docs/security.md +30 -9
- data/docs/what-is-trek.md +4 -4
- data/lib/generators/trek/USAGE +1 -1
- data/lib/generators/trek/audit_log_generator.rb +62 -0
- data/lib/generators/trek/helpers.rb +17 -0
- data/lib/generators/trek/install/admin/routes_generator.rb +2 -1
- data/lib/generators/trek/install/admin/security_check_generator.rb +36 -0
- data/lib/generators/trek/install/admin/users_generator.rb +6 -1
- data/lib/generators/trek/install/audit_log_generator.rb +9 -45
- data/lib/generators/trek/install/ci_generator.rb +19 -0
- data/lib/generators/trek/install/good_job_generator.rb +161 -0
- data/lib/generators/trek/install/model_specs_generator.rb +3 -3
- data/lib/generators/trek/install/postcss_generator.rb +1 -2
- data/lib/generators/trek/install/ruby_generator.rb +1 -1
- data/lib/generators/trek/install/scalingo_generator.rb +7 -45
- data/lib/generators/trek/install/translations_generator.rb +1 -2
- data/lib/generators/trek/install_generator.rb +18 -2
- data/lib/generators/trek/public_accounts_generator.rb +294 -0
- data/lib/generators/trek/scaffold_generator.rb +22 -21
- data/lib/generators/trek/scalingo_generator.rb +67 -0
- data/lib/generators/trek/templates/.gitlab-ci.yml +10 -1
- data/lib/generators/trek/templates/AGENTS.md +2 -2
- data/lib/generators/trek/templates/Makefile +1 -0
- data/lib/generators/trek/templates/config/i18n-tasks.yml.erb +0 -8
- data/lib/generators/trek/templates/controllers/admin/password_renewals_controller.rb +11 -0
- data/lib/generators/trek/templates/controllers/admin/security_checks_controller.rb +11 -0
- data/lib/generators/trek/templates/controllers/admin/user_sessions_controller.rb +1 -1
- data/lib/generators/trek/templates/controllers/admin/users_controller.rb +0 -8
- data/lib/generators/trek/templates/controllers/concerns/trek/authentication.rb +13 -4
- data/lib/generators/trek/templates/initializers/content_security_policy.rb +3 -2
- data/lib/generators/trek/templates/initializers/good_job.rb.tt +40 -0
- data/lib/generators/trek/templates/jobs/mail_delivery_job.rb +6 -0
- data/lib/generators/trek/templates/layouts/admin.html.slim +10 -0
- data/lib/generators/trek/templates/mailers/admin_mailer.rb +4 -2
- data/lib/generators/trek/templates/package.json.tt +1 -1
- data/lib/generators/trek/templates/policies/security_check.rb +4 -0
- data/lib/generators/trek/templates/public_accounts/api/authentication.rb.tt +76 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/accounts_controller.rb.tt +22 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/active_sessions_controller.rb.tt +21 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/base_controller.rb.tt +71 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/email_changes_controller.rb.tt +55 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/email_confirmations_controller.rb.tt +19 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/password_resets_controller.rb.tt +28 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/passwords_controller.rb.tt +17 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/registrations_controller.rb.tt +17 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/sessions_controller.rb.tt +57 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/sign_in_codes_controller.rb.tt +20 -0
- data/lib/generators/trek/templates/public_accounts/jobs/sign_in_code_job.rb.tt +23 -0
- data/lib/generators/trek/templates/public_accounts/locales/accounts.en.yml.tt +215 -0
- data/lib/generators/trek/templates/public_accounts/locales/accounts.fr.yml.tt +215 -0
- data/lib/generators/trek/templates/public_accounts/mailers/account_mailer.rb.tt +85 -0
- data/lib/generators/trek/templates/public_accounts/migrations/create_accounts.rb.tt +46 -0
- data/lib/generators/trek/templates/public_accounts/models/account/session.rb.tt +47 -0
- data/lib/generators/trek/templates/public_accounts/models/account/sign_in_code.rb.tt +8 -0
- data/lib/generators/trek/templates/public_accounts/models/account.rb.tt +87 -0
- data/lib/generators/trek/templates/public_accounts/spec/api/accounts_spec.rb.tt +90 -0
- data/lib/generators/trek/templates/public_accounts/spec/api/sign_ins_spec.rb.tt +188 -0
- data/lib/generators/trek/templates/public_accounts/spec/models/account/sign_in_code_spec.rb.tt +66 -0
- data/lib/generators/trek/templates/public_accounts/spec/models/account_spec.rb.tt +129 -0
- data/lib/generators/trek/templates/public_accounts/spec/web/accounts_spec.rb.tt +126 -0
- data/lib/generators/trek/templates/public_accounts/spec/web/authentication_spec.rb.tt +43 -0
- data/lib/generators/trek/templates/public_accounts/spec/web/sign_ins_spec.rb.tt +237 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_change.en.html.slim.tt +10 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_change.en.text.erb.tt +8 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_change.fr.html.slim.tt +13 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_change.fr.text.erb.tt +8 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_change_code.en.html.slim.tt +9 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_change_code.en.text.erb.tt +11 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_change_code.fr.html.slim.tt +11 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_change_code.fr.text.erb.tt +11 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_confirmation.en.html.slim.tt +10 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_confirmation.en.text.erb.tt +8 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_confirmation.fr.html.slim.tt +10 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_confirmation.fr.text.erb.tt +8 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/password_reset.en.html.slim.tt +10 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/password_reset.en.text.erb.tt +8 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/password_reset.fr.html.slim.tt +13 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/password_reset.fr.text.erb.tt +8 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_code.en.html.slim.tt +9 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_code.en.text.erb.tt +11 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_code.fr.html.slim.tt +9 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_code.fr.text.erb.tt +11 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_locked.en.html.slim.tt +23 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_locked.en.text.erb.tt +14 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_locked.fr.html.slim.tt +24 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_locked.fr.text.erb.tt +14 -0
- data/lib/generators/trek/templates/public_accounts/web/authentication.rb.tt +97 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/accounts_controller.rb.tt +33 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/active_sessions_controller.rb.tt +15 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/base_controller.rb.tt +52 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/email_changes_controller.rb.tt +85 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/email_confirmations_controller.rb.tt +19 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/password_resets_controller.rb.tt +41 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/passwords_controller.rb.tt +23 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/registrations_controller.rb.tt +24 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/sessions_controller.rb.tt +85 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/sign_in_codes_controller.rb.tt +32 -0
- data/lib/generators/trek/templates/public_accounts/web/views/_flash.html.slim.tt +5 -0
- data/lib/generators/trek/templates/public_accounts/web/views/accounts/show.html.slim.tt +52 -0
- data/lib/generators/trek/templates/public_accounts/web/views/email_changes/edit.html.slim.tt +17 -0
- data/lib/generators/trek/templates/public_accounts/web/views/email_changes/new.html.slim.tt +15 -0
- data/lib/generators/trek/templates/public_accounts/web/views/password_resets/edit.html.slim.tt +29 -0
- data/lib/generators/trek/templates/public_accounts/web/views/password_resets/new.html.slim.tt +15 -0
- data/lib/generators/trek/templates/public_accounts/web/views/passwords/edit.html.slim.tt +28 -0
- data/lib/generators/trek/templates/public_accounts/web/views/registrations/new.html.slim.tt +30 -0
- data/lib/generators/trek/templates/public_accounts/web/views/sessions/new.html.slim.tt +40 -0
- data/lib/generators/trek/templates/public_accounts/web/views/sign_in_codes/new.html.slim.tt +17 -0
- data/lib/generators/trek/templates/spec/jobs/mail_delivery_job_spec.rb +11 -0
- data/lib/generators/trek/templates/spec/mailers/admin_mailer_spec.rb +10 -0
- data/lib/generators/trek/templates/spec/requests/admin/fragments_spec.rb +1 -1
- data/lib/generators/trek/templates/spec/requests/admin/good_job_spec.rb +34 -0
- data/lib/generators/trek/templates/spec/requests/admin/pages_spec.rb +12 -1
- data/lib/generators/trek/templates/spec/requests/admin/panels_spec.rb +16 -0
- data/lib/generators/trek/templates/spec/requests/admin/password_renewal_spec.rb +2 -2
- data/lib/generators/trek/templates/spec/requests/admin/security_checks_spec.rb +24 -0
- data/lib/generators/trek/templates/spec/requests/admin/sign_in_lockout_spec.rb +1 -1
- data/lib/generators/trek/templates/spec/requests/admin/sign_in_notices_spec.rb +33 -0
- data/lib/generators/trek/templates/spec/requests/admin/user_sessions_spec.rb +2 -2
- data/lib/generators/trek/templates/views/admin/pages/_form.html.slim +1 -1
- data/lib/generators/trek/templates/views/admin/security_checks/show.html.slim +9 -0
- data/lib/generators/trek/templates/views/admin/user_password_resets/edit.html.slim +6 -1
- data/lib/generators/trek/templates/views/admin/user_sessions/new.html.slim +4 -1
- data/lib/generators/trek/templates/views/admin/users/index.html.slim +1 -1
- data/lib/generators/trek/templates/views/admin_mailer/new_sign_in.en.html.slim +1 -1
- data/lib/generators/trek/templates/views/admin_mailer/new_sign_in.en.text.erb +1 -1
- data/lib/generators/trek/templates/views/admin_mailer/new_sign_in.fr.html.slim +1 -1
- data/lib/generators/trek/templates/views/admin_mailer/new_sign_in.fr.text.erb +1 -1
- data/lib/tasks/trek/attachments.rake +12 -0
- data/lib/tasks/trek/security.rake +10 -0
- data/lib/trek/csp_reports.rb +22 -4
- data/lib/trek/disposable_email_domains.rb +9 -20
- data/lib/trek/engine.rb +5 -0
- data/lib/trek/json_compatibility.rb +20 -0
- data/lib/trek/security_check.rb +241 -0
- data/lib/trek/stored_image_metadata.rb +88 -0
- data/lib/trek/typography_formatter.rb +35 -4
- data/lib/trek/version.rb +1 -1
- data/lib/trek.rb +2 -1
- data/package.json +16 -13
- data/postcss.config.js +0 -3
- data/trek.gemspec +6 -7
- data/vitest.config.mjs +24 -0
- data/yarn.lock +1667 -1010
- metadata +122 -33
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
Rails.application.configure do
|
|
2
|
+
# One scheduler for every queue. Give a queue its own lane when it must not wait behind the
|
|
3
|
+
# others, e.g. "mailers:2;*:3" — the thread counts have to stay within GOOD_JOB_MAX_THREADS,
|
|
4
|
+
# which config/database.yml turns into database connections.
|
|
5
|
+
config.good_job.queues = "*"
|
|
6
|
+
|
|
7
|
+
# Cron runs wherever a scheduler runs, which in :async mode means every web container. Keep it
|
|
8
|
+
# to production so a development machine never fires a scheduled job.
|
|
9
|
+
config.good_job.enable_cron = Rails.env.production?
|
|
10
|
+
# Each entry runs a job on a schedule ("0 3 * * *": every day at 3am). trek:audit_log and
|
|
11
|
+
# trek:public_accounts add their purges at the top.
|
|
12
|
+
config.good_job.cron = {
|
|
13
|
+
purge_expired_sessions: {
|
|
14
|
+
cron: "0 3 * * *",
|
|
15
|
+
class: "Trek::PurgeExpiredSessionsJob",
|
|
16
|
+
description: "Delete the admin's sessions past their idle timeout or lifetime"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
# An error raised outside a job's own execution (in the scheduler thread) has no retry handler
|
|
21
|
+
# to catch it. Report it through Rails.error, like the rest of the app.
|
|
22
|
+
config.good_job.on_thread_error = ->(error) { Rails.error.report(error, handled: false) }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# The dashboard is mounted at /admin/good_job, outside Admin::BaseController and therefore outside
|
|
26
|
+
# Trek's authentication. Guard it with the same signed session cookie, and answer 404 rather than
|
|
27
|
+
# redirecting: the route stays invisible to anyone who is not an administrator.
|
|
28
|
+
ActiveSupport.on_load(:good_job_application_controller) do
|
|
29
|
+
before_action do
|
|
30
|
+
raise ActionController::RoutingError, "Not Found" unless good_job_dashboard_user&.admin?
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def good_job_dashboard_user
|
|
36
|
+
return unless cookies.signed[:session_id]
|
|
37
|
+
|
|
38
|
+
<%= session_model %>.find_by(id: cookies.signed[:session_id])&.user
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
# Delivers every `deliver_later` email (config.action_mailer.delivery_job).
|
|
2
|
+
# GoodJob discards a failed job rather than retrying it, so a transient error from the delivery
|
|
3
|
+
# service would lose the email: retry with a growing delay instead.
|
|
4
|
+
class MailDeliveryJob < ActionMailer::MailDeliveryJob
|
|
5
|
+
retry_on StandardError, wait: :polynomially_longer, attempts: 5
|
|
6
|
+
end
|
|
@@ -56,6 +56,16 @@ html(lang=I18n.locale class=class_for_theme)
|
|
|
56
56
|
href: %i[admin users],
|
|
57
57
|
allowed_to: allowed_to?(:index?, User),
|
|
58
58
|
)
|
|
59
|
+
s.with_item(
|
|
60
|
+
text: t("admin.jobs"),
|
|
61
|
+
href: "/admin/good_job",
|
|
62
|
+
allowed_to: Current.user.admin?,
|
|
63
|
+
)
|
|
64
|
+
s.with_item(
|
|
65
|
+
text: t("admin.security_checks.show.title"),
|
|
66
|
+
href: %i[admin security_check],
|
|
67
|
+
allowed_to: Current.user.admin?,
|
|
68
|
+
)
|
|
59
69
|
end
|
|
60
70
|
end
|
|
61
71
|
= render Trek::ToasterComponent.new
|
|
@@ -13,11 +13,13 @@ class AdminMailer < ApplicationMailer
|
|
|
13
13
|
end
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
# A sign-in from a browser that never signed in as the user, so they can react if it wasn't them
|
|
16
|
+
# A sign-in from a browser that never signed in as the user, so they can react if it wasn't them.
|
|
17
|
+
# The IP address comes with its host name, as on the active sessions: sent later, the email can
|
|
18
|
+
# wait for the lookup.
|
|
17
19
|
def new_sign_in(user, at:, ip:, user_agent:)
|
|
18
20
|
@user = user
|
|
19
21
|
@at = at
|
|
20
|
-
@
|
|
22
|
+
@address = [ip, Trek::ReverseDns.hostname(ip)&.then { |hostname| "(#{hostname})" }].compact.join(" ")
|
|
21
23
|
@user_agent = user_agent
|
|
22
24
|
@sessions_url = admin_user_url(user, anchor: "sessions")
|
|
23
25
|
@reset_url = new_admin_user_password_reset_url
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Signs <%= human_name.pluralize.downcase %> in to the API through a JWT the mobile app sends with each request
|
|
2
|
+
# (Authorization: Bearer). The token only names a <%= class_name %>::Session: signing out deletes the session and
|
|
3
|
+
# the token with it, and the session's idle timeout and lifetime apply as on the web. Apart from the
|
|
4
|
+
# admin's users and sessions altogether.
|
|
5
|
+
module <%= accounts_module %>::Authentication
|
|
6
|
+
extend ActiveSupport::Concern
|
|
7
|
+
|
|
8
|
+
included do
|
|
9
|
+
before_action :require_<%= file_name %>
|
|
10
|
+
after_action :track_<%= file_name %>_activity
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class_methods do
|
|
14
|
+
def allow_unauthenticated_access(**options)
|
|
15
|
+
skip_before_action :require_<%= file_name %>, **options
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def require_<%= file_name %>
|
|
22
|
+
render_api_error :unauthorized, :sign_in_needed unless resume_<%= file_name %>_session
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Changing the password or deleting the account asks to sign in again when the session isn't recent.
|
|
26
|
+
def require_recent_<%= file_name %>_sign_in
|
|
27
|
+
render_api_error :forbidden, :recent_sign_in_needed unless Current.<%= file_name %>_session.recent?
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def resume_<%= file_name %>_session
|
|
31
|
+
Current.<%= file_name %>_session ||= find_<%= file_name %>_session_by_token
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# A session past its idle timeout or lifetime is dropped: the <%= human_name.downcase %> signs in again.
|
|
35
|
+
def find_<%= file_name %>_session_by_token
|
|
36
|
+
token = request.authorization.to_s[/\ABearer (.+)\z/, 1]
|
|
37
|
+
return unless token
|
|
38
|
+
|
|
39
|
+
<%= file_name %>_session = <%= class_name %>::Session.from_token(token)
|
|
40
|
+
return <%= file_name %>_session unless <%= file_name %>_session&.expired?
|
|
41
|
+
|
|
42
|
+
<%= file_name %>_session.destroy
|
|
43
|
+
nil
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# The session the token of the response will name
|
|
47
|
+
def start_<%= file_name %>_session_for(<%= file_name %>)
|
|
48
|
+
<%= file_name %>.sessions.create!(user_agent: request.user_agent, ip_address: request.remote_ip).tap do |<%= file_name %>_session|
|
|
49
|
+
Current.<%= file_name %>_session = <%= file_name %>_session
|
|
50
|
+
Trek::ResolveSessionHostnameJob.perform_later(<%= file_name %>_session)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def terminate_<%= file_name %>_session
|
|
55
|
+
Current.<%= file_name %>_session.destroy
|
|
56
|
+
Current.<%= file_name %>_session = nil
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# At most once every <%= class_name %>::Session.activity_interval (5 minutes), so using the app doesn't write on
|
|
60
|
+
# every request.
|
|
61
|
+
def track_<%= file_name %>_activity
|
|
62
|
+
<%= file_name %>_session = Current.<%= file_name %>_session
|
|
63
|
+
return unless <%= file_name %>_session&.persisted?
|
|
64
|
+
|
|
65
|
+
<%= file_name %>_session.record_activity
|
|
66
|
+
<%= file_name %> = <%= file_name %>_session.<%= file_name %>
|
|
67
|
+
return if <%= file_name %>.last_activity_at&.after?(<%= class_name %>::Session.activity_interval.ago)
|
|
68
|
+
|
|
69
|
+
<%= file_name %>.update_columns(last_activity_at: Time.current) # rubocop:disable Rails/SkipsModelValidations
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# An error the app can act on (`error`), with a message it may show (`message`).
|
|
73
|
+
def render_api_error(status, error, **details)
|
|
74
|
+
render json: { error:, message: I18n.t("<%= accounts_path %>.api.errors.#{error}"), **details }, status:
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# The signed-in <%= human_name.downcase %>'s account: GET, PATCH { name }, and DELETE after a recent sign-in.
|
|
2
|
+
class <%= controllers_module %>::AccountsController < <%= controllers_module %>::BaseController
|
|
3
|
+
before_action :require_recent_<%= file_name %>_sign_in, only: :destroy
|
|
4
|
+
|
|
5
|
+
def show
|
|
6
|
+
render json: <%= file_name %>_json(Current.<%= file_name %>)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def update
|
|
10
|
+
if Current.<%= file_name %>.update(params.permit(:name))
|
|
11
|
+
render json: <%= file_name %>_json(Current.<%= file_name %>)
|
|
12
|
+
else
|
|
13
|
+
render_api_error :unprocessable_content, :invalid, errors: Current.<%= file_name %>.errors
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def destroy
|
|
18
|
+
Current.<%= file_name %>.destroy!
|
|
19
|
+
Current.<%= file_name %>_session = nil
|
|
20
|
+
head :no_content
|
|
21
|
+
end
|
|
22
|
+
end
|
data/lib/generators/trek/templates/public_accounts/api/controllers/active_sessions_controller.rb.tt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# The devices signed in as the current <%= human_name.downcase %>: GET lists them, DELETE signs one or the others out.
|
|
2
|
+
# Only theirs: every query goes through Current.<%= file_name %>.sessions.
|
|
3
|
+
class <%= controllers_module %>::ActiveSessionsController < <%= controllers_module %>::BaseController
|
|
4
|
+
def index
|
|
5
|
+
sessions = Current.<%= file_name %>.sessions.active.recent_first.map do |<%= file_name %>_session|
|
|
6
|
+
<%= file_name %>_session.as_json(only: %i[id user_agent ip_address ip_hostname created_at last_activity_at])
|
|
7
|
+
.merge("current" => <%= file_name %>_session == Current.<%= file_name %>_session)
|
|
8
|
+
end
|
|
9
|
+
render json: sessions
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def destroy
|
|
13
|
+
Current.<%= file_name %>.sessions.find(params[:id]).destroy
|
|
14
|
+
head :no_content
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def destroy_others
|
|
18
|
+
Current.<%= file_name %>.sessions.where.not(id: Current.<%= file_name %>_session.id).destroy_all
|
|
19
|
+
head :no_content
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# The API's account endpoints: <%= human_name.pluralize.downcase %> only, never the admin's users.
|
|
2
|
+
class <%= controllers_module %>::BaseController < ActionController::API
|
|
3
|
+
include <%= accounts_module %>::Authentication
|
|
4
|
+
# Sign-ins, <% if code? %>code requests<% else %>sign-ups, password resets<% end %> and account changes go to the audit log, when the app has one.
|
|
5
|
+
include Trek::Auditable if defined?(::AuditLog)
|
|
6
|
+
|
|
7
|
+
# Emails sent to one address (<% if code? %>codes<% else %>password resets, confirmations<% end %>), whatever the IP address: rate_limit
|
|
8
|
+
# counts per IP address.
|
|
9
|
+
EMAILS_PER_ADDRESS = 5
|
|
10
|
+
EMAILS_PER_ADDRESS_WINDOW = 1.hour
|
|
11
|
+
|
|
12
|
+
# Failed sign-ins from one IP address, whatever the account, over a longer window than rate_limit's.
|
|
13
|
+
FAILED_SIGN_INS_PER_IP = 20
|
|
14
|
+
FAILED_SIGN_INS_WINDOW = 1.hour
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def normalized_email(email)
|
|
19
|
+
<%= class_name %>.normalize_value_for(:email, email.to_s)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def valid_email?(email)
|
|
23
|
+
email.match?(URI::MailTo::EMAIL_REGEXP)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Counted in Rails.cache, as rate_limit is, under a digest rather than the email itself.
|
|
27
|
+
def too_many_emails_to?(email)
|
|
28
|
+
key = "<%= accounts_path %>/emails/#{Digest::SHA256.hexdigest(email)}"
|
|
29
|
+
Rails.cache.increment(key, 1, expires_in: EMAILS_PER_ADDRESS_WINDOW).to_i > EMAILS_PER_ADDRESS
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def too_many_failed_sign_ins_from_ip?
|
|
33
|
+
Rails.cache.read(failed_sign_ins_from_ip_key).to_i >= FAILED_SIGN_INS_PER_IP
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def count_failed_sign_in_from_ip
|
|
37
|
+
Rails.cache.increment(failed_sign_ins_from_ip_key, 1, expires_in: FAILED_SIGN_INS_WINDOW)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def failed_sign_ins_from_ip_key
|
|
41
|
+
"<%= accounts_path %>/failed_sign_ins/#{request.remote_ip}"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def render_rate_limited
|
|
45
|
+
render_api_error :too_many_requests, :rate_limited
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def <%= file_name %>_json(<%= file_name %>)
|
|
49
|
+
<% if password? -%>
|
|
50
|
+
<%= file_name %>.as_json(only: %i[id email unconfirmed_email name confirmed_at created_at])
|
|
51
|
+
<% else -%>
|
|
52
|
+
<%= file_name %>.as_json(only: %i[id email name created_at])
|
|
53
|
+
<% end -%>
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# A new session, and the token naming it, for the app to send with each request
|
|
57
|
+
def render_new_session(<%= file_name %>, status: :created)
|
|
58
|
+
<%= file_name %>_session = start_<%= file_name %>_session_for(<%= file_name %>)
|
|
59
|
+
render json: { token: <%= file_name %>_session.token, <%= file_name %>: <%= file_name %>_json(<%= file_name %>) }, status:
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# For the audit log (Trek::Auditable): the signed-in <%= human_name.downcase %>, nil before signing in
|
|
63
|
+
def audit_actor
|
|
64
|
+
Current.<%= file_name %>
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The account signed in to, tried, or changed
|
|
68
|
+
def audit_target
|
|
69
|
+
@<%= file_name %> || Current.<%= file_name %>
|
|
70
|
+
end
|
|
71
|
+
end
|
data/lib/generators/trek/templates/public_accounts/api/controllers/email_changes_controller.rb.tt
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
<% if code? -%>
|
|
2
|
+
# Changing the account's email: POST { email } sends a code to the new address, PATCH { email, otp }
|
|
3
|
+
# confirms it's theirs and changes it.
|
|
4
|
+
<% else -%>
|
|
5
|
+
# Changing the account's email: POST { email } sends a link to the new address, PATCH { token } (the
|
|
6
|
+
# link's, which the app opens, possibly signed out) confirms it's theirs and changes it.
|
|
7
|
+
<% end -%>
|
|
8
|
+
class <%= controllers_module %>::EmailChangesController < <%= controllers_module %>::BaseController
|
|
9
|
+
<% if password? -%>
|
|
10
|
+
allow_unauthenticated_access only: :update
|
|
11
|
+
<% end -%>
|
|
12
|
+
rate_limit to: 10, within: 15.minutes, with: -> { render_rate_limited }
|
|
13
|
+
|
|
14
|
+
def create
|
|
15
|
+
email = normalized_email(params[:email])
|
|
16
|
+
return render_api_error :unprocessable_content, :invalid_email unless valid_email?(email) && email != Current.<%= file_name %>.email
|
|
17
|
+
return render_rate_limited if too_many_emails_to?(email)
|
|
18
|
+
|
|
19
|
+
<% if code? -%>
|
|
20
|
+
<%= accounts_module %>::SignInCodeJob.perform_later(email, purpose: "email_change", <%= file_name %>: Current.<%= file_name %>)
|
|
21
|
+
<% else -%>
|
|
22
|
+
Current.<%= file_name %>.update_column(:unconfirmed_email, email) # rubocop:disable Rails/SkipsModelValidations -- checked above
|
|
23
|
+
<%= class_name %>Mailer.email_change(Current.<%= file_name %>).deliver_later
|
|
24
|
+
<% end -%>
|
|
25
|
+
head :accepted
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Taken by another account is only told once the <%= code? ? "code" : "link" %> proved the address is theirs.
|
|
29
|
+
def update
|
|
30
|
+
<% if code? -%>
|
|
31
|
+
return render_rate_limited if too_many_failed_sign_ins_from_ip?
|
|
32
|
+
|
|
33
|
+
email = normalized_email(params[:email])
|
|
34
|
+
unless <%= class_name %>::SignInCode.redeem(email, params[:otp], purpose: "email_change", <%= file_name %>: Current.<%= file_name %>)
|
|
35
|
+
count_failed_sign_in_from_ip
|
|
36
|
+
return render_api_error :unprocessable_content, :invalid_code
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
if Current.<%= file_name %>.update(email:)
|
|
40
|
+
render json: <%= file_name %>_json(Current.<%= file_name %>)
|
|
41
|
+
else
|
|
42
|
+
render_api_error :conflict, :email_taken
|
|
43
|
+
end
|
|
44
|
+
<% else -%>
|
|
45
|
+
@<%= file_name %> = <%= class_name %>.find_by_token_for(:email_change, params[:token].to_s)
|
|
46
|
+
return render_api_error :unprocessable_content, :invalid_token unless @<%= file_name %>
|
|
47
|
+
|
|
48
|
+
if @<%= file_name %>.change_email!
|
|
49
|
+
render json: <%= file_name %>_json(@<%= file_name %>)
|
|
50
|
+
else
|
|
51
|
+
render_api_error :conflict, :email_taken
|
|
52
|
+
end
|
|
53
|
+
<% end -%>
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# POST: sends the link confirming the account's email again. PATCH { token }: the token of the link,
|
|
2
|
+
# which the app opens, possibly signed out: the token is enough.
|
|
3
|
+
class <%= controllers_module %>::EmailConfirmationsController < <%= controllers_module %>::BaseController
|
|
4
|
+
allow_unauthenticated_access only: :update
|
|
5
|
+
|
|
6
|
+
def create
|
|
7
|
+
<%= file_name %> = Current.<%= file_name %>
|
|
8
|
+
<%= class_name %>Mailer.email_confirmation(<%= file_name %>).deliver_later unless <%= file_name %>.confirmed? || too_many_emails_to?(<%= file_name %>.email)
|
|
9
|
+
head :accepted
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def update
|
|
13
|
+
@<%= file_name %> = <%= class_name %>.find_by_token_for(:email_confirmation, params[:token].to_s)
|
|
14
|
+
return render_api_error :unprocessable_content, :invalid_token unless @<%= file_name %>
|
|
15
|
+
|
|
16
|
+
@<%= file_name %>.confirm_email!
|
|
17
|
+
render json: <%= file_name %>_json(@<%= file_name %>)
|
|
18
|
+
end
|
|
19
|
+
end
|
data/lib/generators/trek/templates/public_accounts/api/controllers/password_resets_controller.rb.tt
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# POST { email }: emails a link to choose a new password, if the email has an account, with the same
|
|
2
|
+
# answer (202) either way. PATCH { token, password }: the token of the link, which the app opens, and
|
|
3
|
+
# the new password; signs out every session.
|
|
4
|
+
class <%= controllers_module %>::PasswordResetsController < <%= controllers_module %>::BaseController
|
|
5
|
+
allow_unauthenticated_access
|
|
6
|
+
rate_limit to: 10, within: 15.minutes, with: -> { render_rate_limited }
|
|
7
|
+
|
|
8
|
+
def create
|
|
9
|
+
email = normalized_email(params[:email])
|
|
10
|
+
@<%= file_name %> = <%= class_name %>.find_by(email:) if valid_email?(email)
|
|
11
|
+
<%= class_name %>Mailer.password_reset(@<%= file_name %>).deliver_later if @<%= file_name %> && !too_many_emails_to?(email)
|
|
12
|
+
|
|
13
|
+
head :accepted
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def update
|
|
17
|
+
@<%= file_name %> = <%= class_name %>.find_by_token_for(:password_reset, params[:token].to_s)
|
|
18
|
+
return render_api_error :unprocessable_content, :invalid_token unless @<%= file_name %>
|
|
19
|
+
|
|
20
|
+
@<%= file_name %>.password = params[:password].to_s
|
|
21
|
+
if @<%= file_name %>.save
|
|
22
|
+
@<%= file_name %>.sessions.delete_all
|
|
23
|
+
head :no_content
|
|
24
|
+
else
|
|
25
|
+
render_api_error :unprocessable_content, :invalid, errors: @<%= file_name %>.errors
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# PATCH { password }: changes the account's password, after a recent sign-in; a forgotten one goes
|
|
2
|
+
# through the password reset. Signs out the account's other sessions.
|
|
3
|
+
class <%= controllers_module %>::PasswordsController < <%= controllers_module %>::BaseController
|
|
4
|
+
before_action :require_recent_<%= file_name %>_sign_in
|
|
5
|
+
|
|
6
|
+
def update
|
|
7
|
+
<%= file_name %> = Current.<%= file_name %>
|
|
8
|
+
<%= file_name %>.password = params[:password].to_s
|
|
9
|
+
|
|
10
|
+
if <%= file_name %>.save
|
|
11
|
+
<%= file_name %>.sessions.where.not(id: Current.<%= file_name %>_session.id).delete_all
|
|
12
|
+
head :no_content
|
|
13
|
+
else
|
|
14
|
+
render_api_error :unprocessable_content, :invalid, errors: <%= file_name %>.errors
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
data/lib/generators/trek/templates/public_accounts/api/controllers/registrations_controller.rb.tt
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# POST { email, password, name }: signs up and in, answering with the token; the email is confirmed
|
|
2
|
+
# later by a link (confirmed_at), which the app opens and sends to PATCH /account/confirmation.
|
|
3
|
+
class <%= controllers_module %>::RegistrationsController < <%= controllers_module %>::BaseController
|
|
4
|
+
allow_unauthenticated_access
|
|
5
|
+
rate_limit to: 5, within: 15.minutes, with: -> { render_rate_limited }
|
|
6
|
+
|
|
7
|
+
def create
|
|
8
|
+
@<%= file_name %> = <%= class_name %>.new(params.permit(:email, :name, :password))
|
|
9
|
+
|
|
10
|
+
if @<%= file_name %>.save
|
|
11
|
+
<%= class_name %>Mailer.email_confirmation(@<%= file_name %>).deliver_later
|
|
12
|
+
render_new_session @<%= file_name %>
|
|
13
|
+
else
|
|
14
|
+
render_api_error :unprocessable_content, :invalid, errors: @<%= file_name %>.errors
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
<% if invite_only? -%>
|
|
2
|
+
# POST { email, otp }: signs in with the emailed code, to the email's account (an email without one
|
|
3
|
+
# was sent no code); answers with the token for the next requests. DELETE: signs out, ending the
|
|
4
|
+
# token's session.
|
|
5
|
+
<% elsif code? -%>
|
|
6
|
+
# POST { email, otp }: signs in with the emailed code, to the email's account, created if it had none;
|
|
7
|
+
# answers with the token for the next requests. DELETE: signs out, ending the token's session.
|
|
8
|
+
<% else -%>
|
|
9
|
+
# POST { email, password }: signs in, and answers with the token for the next requests. DELETE: signs
|
|
10
|
+
# out, ending the token's session.
|
|
11
|
+
<% end -%>
|
|
12
|
+
class <%= controllers_module %>::SessionsController < <%= controllers_module %>::BaseController
|
|
13
|
+
allow_unauthenticated_access only: :create
|
|
14
|
+
rate_limit to: <%= code? ? 10 : 5 %>, within: 3.minutes, only: :create, with: -> { render_rate_limited }
|
|
15
|
+
|
|
16
|
+
def create
|
|
17
|
+
return render_rate_limited if too_many_failed_sign_ins_from_ip?
|
|
18
|
+
|
|
19
|
+
<% if code? -%>
|
|
20
|
+
email = normalized_email(params[:email])
|
|
21
|
+
<% if invite_only? -%>
|
|
22
|
+
unless <%= class_name %>::SignInCode.redeem(email, params[:otp]) && (@<%= file_name %> = <%= class_name %>.for_sign_in(email))
|
|
23
|
+
count_failed_sign_in_from_ip
|
|
24
|
+
return render_api_error :unauthorized, :invalid_code
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
<% else -%>
|
|
28
|
+
unless <%= class_name %>::SignInCode.redeem(email, params[:otp])
|
|
29
|
+
count_failed_sign_in_from_ip
|
|
30
|
+
return render_api_error :unauthorized, :invalid_code
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
@<%= file_name %> = <%= class_name %>.for_verified_email!(email)
|
|
34
|
+
<% end -%>
|
|
35
|
+
render_new_session @<%= file_name %>
|
|
36
|
+
<% else -%>
|
|
37
|
+
# Hashes the password sent even when no account matches, so the response takes as long
|
|
38
|
+
<%= file_name %> = <%= class_name %>.authenticate_by(email: params[:email].to_s, password: params[:password].to_s)
|
|
39
|
+
@<%= file_name %> = <%= file_name %> || <%= class_name %>.find_by(email: params[:email].to_s)
|
|
40
|
+
|
|
41
|
+
# A locked account is refused even with the right password, with the same answer as a wrong one
|
|
42
|
+
if <%= file_name %> && !<%= file_name %>.sign_in_locked?
|
|
43
|
+
<%= file_name %>.record_successful_sign_in!
|
|
44
|
+
render_new_session <%= file_name %>
|
|
45
|
+
else
|
|
46
|
+
@<%= file_name %>&.record_failed_sign_in!
|
|
47
|
+
count_failed_sign_in_from_ip
|
|
48
|
+
render_api_error :unauthorized, :invalid_password
|
|
49
|
+
end
|
|
50
|
+
<% end -%>
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def destroy
|
|
54
|
+
terminate_<%= file_name %>_session
|
|
55
|
+
head :no_content
|
|
56
|
+
end
|
|
57
|
+
end
|
data/lib/generators/trek/templates/public_accounts/api/controllers/sign_in_codes_controller.rb.tt
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
<% if invite_only? -%>
|
|
2
|
+
# POST { email }: emails a code to sign in with, if the email has an account. Whether or not it has
|
|
3
|
+
# one, the answer is the same (202).
|
|
4
|
+
<% else -%>
|
|
5
|
+
# POST { email }: emails a code to sign in with. Whether or not the email has an account, the answer
|
|
6
|
+
# is the same (202); the code creates the account on its first use.
|
|
7
|
+
<% end -%>
|
|
8
|
+
class <%= controllers_module %>::SignInCodesController < <%= controllers_module %>::BaseController
|
|
9
|
+
allow_unauthenticated_access
|
|
10
|
+
rate_limit to: 10, within: 15.minutes, with: -> { render_rate_limited }
|
|
11
|
+
|
|
12
|
+
def create
|
|
13
|
+
email = normalized_email(params[:email])
|
|
14
|
+
return render_api_error :unprocessable_content, :invalid_email unless valid_email?(email)
|
|
15
|
+
return render_rate_limited if too_many_emails_to?(email)
|
|
16
|
+
|
|
17
|
+
<%= accounts_module %>::SignInCodeJob.perform_later(email)
|
|
18
|
+
head :accepted
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Issues a code and emails it, in the job rather than the request: the code in clear only exists
|
|
2
|
+
# here, never in the job's arguments, which the queue keeps. Active Job runs it in the request's
|
|
3
|
+
# locale.
|
|
4
|
+
class <%= accounts_module %>::SignInCodeJob < ApplicationJob
|
|
5
|
+
<% if invite_only? -%>
|
|
6
|
+
# To sign in to `email`'s account, if it has one (<%= class_name %>.for_sign_in), or for `<%= file_name %>` to
|
|
7
|
+
# change its email to `email` (purpose: "email_change").
|
|
8
|
+
<% else -%>
|
|
9
|
+
# To sign in to `email`'s account (created on the code's first use), or for `<%= file_name %>` to
|
|
10
|
+
# change its email to `email` (purpose: "email_change").
|
|
11
|
+
<% end -%>
|
|
12
|
+
def perform(email, purpose: "sign_in", <%= file_name %>: nil)
|
|
13
|
+
<% if invite_only? -%>
|
|
14
|
+
# Decided here rather than in the request, so its answer and timing are the same for every email
|
|
15
|
+
return if purpose == "sign_in" && !<%= class_name %>.for_sign_in(email)
|
|
16
|
+
|
|
17
|
+
<% end -%>
|
|
18
|
+
sign_in_code = <%= class_name %>::SignInCode.issue(email, purpose:, <%= file_name %>:)
|
|
19
|
+
mail = purpose == "email_change" ? :email_change_code : :sign_in_code
|
|
20
|
+
|
|
21
|
+
<%= class_name %>Mailer.public_send(mail, email, sign_in_code.code).deliver_now
|
|
22
|
+
end
|
|
23
|
+
end
|