2023-05-17 00:27:35 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module CaptchaConcern
|
|
|
|
extend ActiveSupport::Concern
|
2023-06-13 23:30:40 +03:00
|
|
|
|
2023-05-17 00:27:35 +03:00
|
|
|
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
|
2023-06-13 23:30:40 +03:00
|
|
|
|
2023-05-17 00:27:35 +03:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def extend_csp_for_captcha!
|
2023-08-08 16:41:38 +03:00
|
|
|
policy = request.content_security_policy&.clone
|
2023-06-13 23:30:40 +03:00
|
|
|
|
2023-05-17 00:27:35 +03:00
|
|
|
return unless captcha_required? && policy.present?
|
|
|
|
|
|
|
|
%w(script_src frame_src style_src connect_src).each do |directive|
|
|
|
|
values = policy.send(directive)
|
2023-06-13 23:30:40 +03:00
|
|
|
|
2023-05-17 00:27:35 +03:00
|
|
|
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:')
|
2023-06-13 23:30:40 +03:00
|
|
|
|
2023-05-17 00:27:35 +03:00
|
|
|
policy.send(directive, *values)
|
|
|
|
end
|
2023-08-08 16:41:38 +03:00
|
|
|
|
|
|
|
request.content_security_policy = policy
|
2023-05-17 00:27:35 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_captcha
|
|
|
|
return unless captcha_required?
|
|
|
|
|
|
|
|
hcaptcha_tags
|
|
|
|
end
|
|
|
|
end
|