Skip to content

Instantly share code, notes, and snippets.

@esale
Last active December 22, 2019 15:38
Show Gist options
  • Save esale/c4d08377d5df5ce6b2540358946e16a3 to your computer and use it in GitHub Desktop.
Save esale/c4d08377d5df5ce6b2540358946e16a3 to your computer and use it in GitHub Desktop.
Suggested addition to /lib/recaptcha/adapters/controller_methods.rb in order to being able to retrieve recaptcha score and other values from response.
# This is based on verify_recaptcha:
# https://github.com/ambethia/recaptcha/blob/c9ef2815a3c3d7b8e0abd4b5b85207a34db23cc0/lib/recaptcha/adapters/controller_methods.rb#L10
#
# Your private API can be specified in the +options+ hash or preferably
# using the Configuration.
#
# Example usage: recaptcha_response({secret_key: "insert_your_secret_key_from_google", action: "contact"})
#
# Example response:
#
# {"success"=>true, "challenge_ts"=>"2019-12-22T15:10:25Z", "hostname"=>"localhost", "score"=>0.9, "action"=>"contact"}
#
def recaptcha_response(options = {})
secret_key = options.fetch(:secret_key) { configuration.secret_key! }
recaptcha_raw_response = options[:response] || recaptcha_response_token(options[:action])
begin
return Recaptcha.api_verification({secret: secret_key, response: recaptcha_raw_response})
rescue Timeout::Error
if Recaptcha.configuration.handle_timeouts_gracefully
recaptcha_error(
options.fetch(:message) { Recaptcha::Helpers.to_error_message(:recaptcha_unreachable) }
)
false
else
raise RecaptchaError, 'Recaptcha unreachable.'
end
rescue StandardError => e
raise RecaptchaError, e.message, e.backtrace
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment