mirror of
https://github.com/discourse/discourse.git
synced 2026-08-11 02:59:07 +08:00
This is a revival of this PR: https://github.com/discourse/discourse/pull/33636 ### Description This PR adds ReCaptcha as a Captcha provider and provides a refactor to allow other Captcha providers to be added more easily. To achieve this, the following changes were introduced: - Provider Pattern Introduction in lib/discourse_hcaptcha/captcha_provider.rb - Controller refactoring, created a new Parent controller, extracted common logic - Serializer for each captcha provider - Implemented base abstract component for FE captcha logic - h-captcha-service is now provider agnostic (captcha-service) and manages both captcha providers - Added connectors to the UI to allow the chosen captcha to be rendered - Added problem check and site settings for the newly introduced ReCaptcha - `before-create-account` valueTransformer is introduced to allow captcha validation (or any other data) without modifying the class. --------- Co-authored-by: Juan Martinez <juan@discourse.org> Co-authored-by: David Taylor <david@taylorhq.com>
35 lines
830 B
Ruby
Vendored
35 lines
830 B
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
module DiscourseHcaptcha
|
|
class CaptchaProvider
|
|
HCAPTCHA = "hcaptcha"
|
|
RECAPTCHA = "recaptcha"
|
|
NONE = "none"
|
|
|
|
def fetch_captcha_token(server_session)
|
|
raise NotImplementedError
|
|
end
|
|
|
|
def captcha_verification_url
|
|
raise NotImplementedError
|
|
end
|
|
|
|
def send_captcha_verification(captcha_token)
|
|
raise NotImplementedError
|
|
end
|
|
|
|
protected
|
|
|
|
def send_verification(captcha_token, captcha_verification_url, secret_key)
|
|
uri = URI.parse(captcha_verification_url)
|
|
|
|
http = FinalDestination::HTTP.new(uri.host, uri.port)
|
|
http.use_ssl = true
|
|
|
|
request = FinalDestination::HTTP::Post.new(uri.request_uri)
|
|
request.set_form_data({ "secret" => secret_key, "response" => captcha_token })
|
|
|
|
http.request(request)
|
|
end
|
|
end
|
|
end
|