Class: RecaptchaValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/recaptcha_validator.rb

Overview

Example:

validates :recaptcha_token, recaptcha: { score: 0.5 }, on: :create

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, token) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/validators/recaptcha_validator.rb', line 6

def validate_each(record, attribute, token)
  return if Katalyst::GoogleApis.config.recaptcha.test_mode

  action     = options.fetch(:action, record.class.model_name.param_key)
  project_id = options.fetch(:project_id, Katalyst::GoogleApis.config.project_id)
  score      = options.fetch(:score, Katalyst::GoogleApis.config.recaptcha.score)
  site_key   = options.fetch(:site_key, Katalyst::GoogleApis.config.recaptcha.site_key)

  if token.blank?
    record.errors.add(attribute, :recaptcha_blank)
    return
  end

  response = Katalyst::GoogleApis::Recaptcha::AssessmentService.call(
    parent:     "projects/#{project_id}",
    assessment: { event: { site_key:, token: } },
  )

  if !response.valid?
    record.errors.add(attribute, :recaptcha_invalid)
  elsif response.action != action
    record.errors.add(attribute, :recaptcha_action_mismatch)
  elsif response.score < score
    record.errors.add(attribute, :recaptcha_suspicious)
  end
end