2019-09-23 17:46:46 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides methods to verify the client captcha on Google's services
|
|
|
|
class RecaptchaService
|
|
|
|
class << self
|
|
|
|
def verify(client_response)
|
|
|
|
return { 'success' => true } unless recaptcha_enabled?
|
|
|
|
|
|
|
|
require 'uri'
|
|
|
|
require 'net/http'
|
|
|
|
|
|
|
|
data = { secret: secret_key, response: client_response }
|
|
|
|
url = URI.parse('https://www.google.com/recaptcha/api/siteverify')
|
|
|
|
res = Net::HTTP.post_form(url, data)
|
|
|
|
|
|
|
|
JSON.parse(res&.body)
|
|
|
|
end
|
|
|
|
|
|
|
|
def recaptcha_enabled?
|
|
|
|
secret_key.present? && site_key.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def secret_key
|
2020-05-27 16:35:30 +02:00
|
|
|
Setting.get('recaptcha_secret_key')
|
2019-09-23 17:46:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def site_key
|
2020-05-27 16:35:30 +02:00
|
|
|
Setting.get('recaptcha_site_key')
|
2019-09-23 17:46:46 +02:00
|
|
|
end
|
|
|
|
end
|
2020-05-27 16:35:30 +02:00
|
|
|
end
|