ReCaptcha v3 - almost working

This commit is contained in:
2023-07-25 04:31:44 +00:00
parent 4d39814468
commit 32b3904c39
5 changed files with 47 additions and 2 deletions
+13
View File
@@ -1,3 +1,5 @@
require 'net/https'
class ApplicationController < BaseController
protect_from_forgery with: :exception, except: [:do_confirm, :js_error, :admin_update]
@@ -6,6 +8,17 @@ class ApplicationController < BaseController
helper_method :protect, :policies
RECAPTCHA_MINIMUM_SCORE = 0.5
def verify_recaptcha?(token, recaptcha_action)
recaptcha_secret_key = config.app_config['recaptcha_secret_key']
uri = URI.parse("https://www.google.com/recaptcha/api/siteverify?secret=#{recaptcha_secret_key}&response=#{token}")
response = Net::HTTP.get_response(uri)
json = JSON.parse(response.body)
json['success'] && json['score'] > RECAPTCHA_MINIMUM_SCORE && json['action'] == recaptcha_action
end
def default_url_options
{ host: "#{request.protocol}#{request.host_with_port}", trailing_slash: true }
end