Class: Captcha::CaptchaVerificationService

Inherits:
Object
  • Object
show all
Includes:
Recaptcha::Adapters::ControllerMethods
Defined in:
app/services/captcha/captcha_verification_service.rb

Overview

Encapsulates logic of checking captchas.

Defined Under Namespace

Classes: RequestStruct

Instance Method Summary collapse

Constructor Details

#initialize(spam_params:) ⇒ CaptchaVerificationService

Returns a new instance of CaptchaVerificationService.



17
18
19
# File 'app/services/captcha/captcha_verification_service.rb', line 17

def initialize(spam_params:)
  @spam_params = spam_params
end

Instance Method Details

#executeObject

Performs verification of a captcha response.

NOTE: Currently only supports reCAPTCHA, and is not yet used in all places of the app in which captchas are verified, but these can be addressed in future MRs. See: gitlab.com/gitlab-org/gitlab/-/issues/273480



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/captcha/captcha_verification_service.rb', line 27

def execute
  return false unless spam_params.captcha_response

  @request = RequestStruct.new(spam_params.ip_address)

  Gitlab::Recaptcha.load_configurations!

  # NOTE: We could pass the model and let the recaptcha gem automatically add errors to it,
  # but we do not, for two reasons:
  #
  # 1. We want control over when the errors are added
  # 2. We want control over the wording and i18n of the message
  # 3. We want a consistent interface and behavior when adding support for other captcha
  #    libraries which may not support automatically adding errors to the model.
  verify_recaptcha(response: spam_params.captcha_response)
end