From 8e57cf17add0a8d1e5ad1d6fd62aee0302fb01b2 Mon Sep 17 00:00:00 2001 From: Arit Amana <32520970+msarit@users.noreply.github.com> Date: Mon, 19 Oct 2020 16:20:18 -0400 Subject: [PATCH] [Team Email Login] Admin enable/disable recaptcha during email signup (#10846) * Initial Implementation * Complete hookup of recaptcha guard * Write tests for recaptcha during email signup * Addressing PR changes 1 * Create helper method and write specs * fix bug --- app/controllers/admin/configs_controller.rb | 1 + app/controllers/registrations_controller.rb | 33 ++++++--- app/helpers/authentication_helper.rb | 6 ++ .../admin/controllers/config_controller.js | 12 +++- app/lib/constants/site_config.rb | 16 +++-- app/models/site_config.rb | 1 + app/views/admin/configs/show.html.erb | 70 ++++++++++--------- .../_email_registration_form.html.erb | 11 +++ spec/helpers/authentication_helper_spec.rb | 27 +++++++ spec/requests/registrations_spec.rb | 53 ++++++++++++++ 10 files changed, 181 insertions(+), 49 deletions(-) diff --git a/app/controllers/admin/configs_controller.rb b/app/controllers/admin/configs_controller.rb index c3de18776..f02f63f60 100644 --- a/app/controllers/admin/configs_controller.rb +++ b/app/controllers/admin/configs_controller.rb @@ -116,6 +116,7 @@ module Admin invite_only_mode allow_email_password_registration allow_email_password_login + require_captcha_for_email_password_registration primary_brand_color_hex spam_trigger_terms recaptcha_site_key diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index 086211c5a..ffa9b9df5 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -19,16 +19,21 @@ class RegistrationsController < Devise::RegistrationsController not_authorized if SiteConfig.waiting_on_first_user && ENV["FOREM_OWNER_SECRET"].present? && ENV["FOREM_OWNER_SECRET"] != params[:user][:forem_owner_secret] - build_resource(sign_up_params) - resource.saw_onboarding = false - resource.editor_version = "v2" - resource.save if resource.email.present? - yield resource if block_given? - if resource.persisted? - update_first_user_permissions(resource) - redirect_to "/confirm-email?email=#{resource.email}" + if recaptcha_disabled? || recaptcha_verified? + build_resource(sign_up_params) + resource.saw_onboarding = false + resource.editor_version = "v2" + resource.save if resource.email.present? + yield resource if block_given? + if resource.persisted? + update_first_user_permissions(resource) + redirect_to "/confirm-email?email=#{resource.email}" + else + render action: "by_email" + end else - render action: "by_email" + redirect_to new_user_registration_path(state: "email_signup") + flash[:notice] = "You must complete the recaptcha ✅" end end @@ -41,4 +46,14 @@ class RegistrationsController < Devise::RegistrationsController resource.add_role(:single_resource_admin, Config) SiteConfig.waiting_on_first_user = false end + + def recaptcha_disabled? + (SiteConfig.recaptcha_site_key.blank? && SiteConfig.recaptcha_secret_key.blank?) || + !SiteConfig.require_captcha_for_email_password_registration + end + + def recaptcha_verified? + recaptcha_params = { secret_key: SiteConfig.recaptcha_secret_key } + params["g-recaptcha-response"] && verify_recaptcha(recaptcha_params) + end end diff --git a/app/helpers/authentication_helper.rb b/app/helpers/authentication_helper.rb index 74041fa2a..5ae2f5ee9 100644 --- a/app/helpers/authentication_helper.rb +++ b/app/helpers/authentication_helper.rb @@ -18,4 +18,10 @@ module AuthenticationHelper def authentication_enabled_providers_for_user(user = current_user) Authentication::Providers.enabled_for_user(user) end + + def recaptcha_configured_and_enabled? + SiteConfig.recaptcha_secret_key.present? && + SiteConfig.recaptcha_site_key.present? && + SiteConfig.require_captcha_for_email_password_registration + end end diff --git a/app/javascript/admin/controllers/config_controller.js b/app/javascript/admin/controllers/config_controller.js index 709938f38..3242d32f7 100644 --- a/app/javascript/admin/controllers/config_controller.js +++ b/app/javascript/admin/controllers/config_controller.js @@ -1,7 +1,9 @@ import { Controller } from 'stimulus'; +const recaptchaFields = document.querySelector('#recaptchaContainer'); + export default class ConfigController extends Controller { - static targets = ['inviteOnlyMode', 'authenticationProviders']; + static targets = ['inviteOnlyMode', 'authenticationProviders', 'requireCaptchaForEmailPasswordRegistration']; disableAuthenticationOptions() { if (this.inviteOnlyModeTarget.checked) { @@ -16,4 +18,12 @@ export default class ConfigController extends Controller { ).disabled = false; } } + + toggleGoogleRecaptchaFields() { + if (this.requireCaptchaForEmailPasswordRegistrationTarget.checked) { + recaptchaFields.classList.remove('collapse'); + } else { + recaptchaFields.classList.add('collapse'); + } + } } diff --git a/app/lib/constants/site_config.rb b/app/lib/constants/site_config.rb index 43d6614ad..39ba99687 100644 --- a/app/lib/constants/site_config.rb +++ b/app/lib/constants/site_config.rb @@ -2,11 +2,15 @@ module Constants module SiteConfig DETAILS = { allow_email_password_registration: { - description: "Can users sign up with only email and password?", + description: "People can sign up using their email and password", placeholder: "" }, allow_email_password_login: { - description: "Can users login using email and password?", + description: "People can login using their email and password", + placeholder: "" + }, + require_captcha_for_email_password_registration: { + description: "People will be required to fill out a captcha when they're creating a new account in your community", placeholder: "" }, authentication_providers: { @@ -232,12 +236,12 @@ module Constants placeholder: 2 }, recaptcha_site_key: { - description: "Site key for Google reCAPTCHA, used for reporting abuse.", - placeholder: "..." + description: "Add the site key for Google reCAPTCHA, which is used for reporting abuse", + placeholder: "What is the Google reCAPTCHA site key?" }, recaptcha_secret_key: { - description: "Secret key for Google reCAPTCHA, used for reporting abuse.", - placeholder: "..." + description: "Add the secret key for Google reCAPTCHA, which is used for reporting abuse", + placeholder: "What is the Google reCAPTCHA secret key?" }, right_navbar_svg_icon: { description: "The SVG icon used to expand the right navbar navigation menu. Should be a max of 24x24px.", diff --git a/app/models/site_config.rb b/app/models/site_config.rb index bb96d7725..ec64ce7d2 100644 --- a/app/models/site_config.rb +++ b/app/models/site_config.rb @@ -24,6 +24,7 @@ class SiteConfig < RailsSettings::Base # Authentication field :allow_email_password_registration, type: :boolean, default: false field :allow_email_password_login, type: :boolean, default: true + field :require_captcha_for_email_password_registration, type: :boolean, default: false field :authentication_providers, type: :array, default: proc { Authentication::Providers.available } field :invite_only_mode, type: :boolean, default: false field :twitter_key, type: :string, default: ApplicationConfig["TWITTER_KEY"] diff --git a/app/views/admin/configs/show.html.erb b/app/views/admin/configs/show.html.erb index c2900f573..e7bdd6271 100644 --- a/app/views/admin/configs/show.html.erb +++ b/app/views/admin/configs/show.html.erb @@ -176,6 +176,15 @@