Skip to content

Commit

Permalink
Add reCAPTCHA support (#64)
Browse files Browse the repository at this point in the history
* Add reCAPTCHA support

See #63

* Update app/views/recaptcha/_in_form.haml

Co-authored-by: Brian J. Cardiff <[email protected]>

Co-authored-by: Brian J. Cardiff <[email protected]>
  • Loading branch information
matiasgarciaisaia and Brian J. Cardiff authored Oct 7, 2020
1 parent e9d572d commit fc1dcd4
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ gem 'env_rails'
gem 'instedd_telemetry', git: 'https://github.com/instedd/telemetry_rails.git'
gem 'intercom-rails'
gem 'dalli'
gem 'recaptcha'

group :doc do
gem 'sdoc', require: false
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ GEM
rake (12.3.2)
rdoc (4.1.2)
json (~> 1.4)
recaptcha (5.5.0)
json
responders (2.4.0)
actionpack (>= 4.2.0, < 5.3)
railties (>= 4.2.0, < 5.3)
Expand Down Expand Up @@ -350,6 +352,7 @@ DEPENDENCIES
rack-oauth2!
rails (~> 4.2.0)
rails-dev-tweaks (~> 1.1)
recaptcha
rspec-rails
ruby-openid
sass-rails (~> 4.0.0)
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/confirmations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ConfirmationsController < Devise::ConfirmationsController
prepend_before_action :check_captcha, only: [:create] if Guisso::Settings.recaptcha?

private
def check_captcha
unless verify_recaptcha
self.resource = resource_class.new
respond_with_navigational(resource) { render :new }
end
end
end
8 changes: 8 additions & 0 deletions app/controllers/passwords_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
class PasswordsController < Devise::PasswordsController
prepend_before_action :check_captcha, only: [:create] if Guisso::Settings.recaptcha?

def create
super
Telemetry::Auth.reset_password if successfully_sent?(resource)
end

private
def check_captcha
unless verify_recaptcha
self.resource = resource_class.new
respond_with_navigational(resource) { render :new }
end
end
end
11 changes: 11 additions & 0 deletions app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class RegistrationsController < Devise::RegistrationsController
prepend_before_action :check_captcha, only: [:create] if Guisso::Settings.recaptcha?

def new
if redirect_url = params[:redirect_url]
session[:user_return_to] = redirect_url
Expand All @@ -9,4 +11,13 @@ def new
def after_inactive_sign_up_path_for(resource)
new_user_session_path
end

private
def check_captcha
unless verify_recaptcha
self.resource = resource_class.new sign_up_params
resource.validate # Look for any other validation errors besides reCAPTCHA
respond_with_navigational(resource) { render :new }
end
end
end
2 changes: 2 additions & 0 deletions app/views/devise/confirmations/new.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
= f.input_field :email, placeholder: 'Email', class: 'block email', pattern: false
= f.full_error :email

= render 'recaptcha/in_form'

.control-group
.controls
= f.submit "Resend confirmation instructions", class: 'btn'
Expand Down
2 changes: 2 additions & 0 deletions app/views/devise/passwords/new.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
= f.input_field :email, placeholder: 'Email', class: 'block email', pattern: false
= f.full_error :email

= render 'recaptcha/in_form'

.control-group
.controls
= f.submit "Send me reset password instructions", class: 'btn'
Expand Down
2 changes: 2 additions & 0 deletions app/views/devise/registrations/new.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
= f.input_field :password_confirmation, placeholder: 'Password confirmation', class: 'block password'
= f.full_error :password_confirmation

= render 'recaptcha/in_form'

.control-group
.controls
= f.submit "Sign up", class: 'btn'
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/centred_form.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
= yield :form_title

.row.centred
.span4
.span5
.well-white
= yield

Expand Down
5 changes: 5 additions & 0 deletions app/views/recaptcha/_in_form.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- if Guisso::Settings.recaptcha?
.control-group{ class: flash[:recaptcha_error].present? ? "error" : "" }
= recaptcha_tags
%span.help-inline
= flash[:recaptcha_error]
12 changes: 12 additions & 0 deletions config/initializers/_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,17 @@ def self.cookie_domain
def self.openid_store
URI(ENV["OPENID_STORE"] || Config["openid_store"] || "file:db/openid-store")
end

def self.recaptcha_site_key
ENV['RECAPTCHA_SITE_KEY'] || Config['recaptcha']['site_key']
end

def self.recaptcha_secret_key
ENV['RECAPTCHA_SECRET_KEY'] || Config['recaptcha']['secret_key']
end

def self.recaptcha?
recaptcha_site_key.present? && recaptcha_secret_key.present?
end
end
end
10 changes: 10 additions & 0 deletions config/initializers/recaptcha.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
recaptcha_enabled = Guisso::Settings.recaptcha?
recaptcha_site_key = Guisso::Settings.recaptcha_site_key
recaptcha_secret_key = Guisso::Settings.recaptcha_secret_key

if recaptcha_enabled
Recaptcha.configure do |config|
config.site_key = recaptcha_site_key
config.secret_key = recaptcha_secret_key
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
omniauth_callbacks: 'omniauth_callbacks',
sessions: 'sessions',
registrations: 'registrations',
confirmations: 'confirmations',
passwords: 'passwords'
}

Expand Down
3 changes: 3 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ cookie:
name: guisso
domain: instedd.org
openid_store: file:db/openid-store
recaptcha:
site_key:
secret_key:

0 comments on commit fc1dcd4

Please sign in to comment.