forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hCaptcha support (mastodon#25019)
- Loading branch information
1 parent
e604147
commit bec6a1c
Showing
12 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,3 +160,5 @@ gem 'cocoon', '~> 1.2' | |
|
||
gem 'net-http', '~> 0.3.2' | ||
gem 'rubyzip', '~> 2.3' | ||
|
||
gem 'hcaptcha', '~> 7.1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
module CaptchaConcern | ||
extend ActiveSupport::Concern | ||
include Hcaptcha::Adapters::ViewMethods | ||
|
||
included do | ||
helper_method :render_captcha | ||
end | ||
|
||
def captcha_available? | ||
ENV['HCAPTCHA_SECRET_KEY'].present? && ENV['HCAPTCHA_SITE_KEY'].present? | ||
end | ||
|
||
def captcha_enabled? | ||
captcha_available? && Setting.captcha_enabled | ||
end | ||
|
||
def captcha_user_bypass? | ||
false | ||
end | ||
|
||
def captcha_required? | ||
captcha_enabled? && !captcha_user_bypass? | ||
end | ||
|
||
def check_captcha! | ||
return true unless captcha_required? | ||
|
||
if verify_hcaptcha | ||
true | ||
else | ||
if block_given? | ||
message = flash[:hcaptcha_error] | ||
flash.delete(:hcaptcha_error) | ||
yield message | ||
end | ||
false | ||
end | ||
end | ||
|
||
def extend_csp_for_captcha! | ||
policy = request.content_security_policy | ||
return unless captcha_required? && policy.present? | ||
|
||
%w(script_src frame_src style_src connect_src).each do |directive| | ||
values = policy.send(directive) | ||
values << 'https://hcaptcha.com' unless values.include?('https://hcaptcha.com') || values.include?('https:') | ||
values << 'https://*.hcaptcha.com' unless values.include?('https://*.hcaptcha.com') || values.include?('https:') | ||
policy.send(directive, *values) | ||
end | ||
end | ||
|
||
def render_captcha | ||
return unless captcha_required? | ||
|
||
hcaptcha_tags | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin::SettingsHelper | ||
def captcha_available? | ||
ENV['HCAPTCHA_SECRET_KEY'].present? && ENV['HCAPTCHA_SITE_KEY'].present? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
- content_for :page_title do | ||
= t('auth.captcha_confirmation.title') | ||
|
||
= form_tag auth_captcha_confirmation_url, method: 'POST', class: 'simple_form' do | ||
= render 'auth/shared/progress', stage: 'confirm' | ||
|
||
= hidden_field_tag :confirmation_token, params[:confirmation_token] | ||
|
||
%p.lead= t('auth.captcha_confirmation.hint_html') | ||
|
||
.field-group | ||
= render_captcha | ||
|
||
.actions | ||
%button.button= t('challenge.confirm') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters