Module: Card::Set::All::Recaptcha

Extended by:
Card::Set
Defined in:
card/tmpsets/set/mod034-card-mod-recaptcha/all/recaptcha.rb,
card/tmpsets/set/mod035-card-mod-recaptcha/all/recaptcha.rb

Overview

Set: All cards (Recaptcha)

Defined Under Namespace

Modules: HtmlFormat

Constant Summary collapse

RECAPTCHA_ERROR_CODES =

LOCALIZE

{  # LOCALIZE
  "missing-input-secret" =>	"secret parameter is missing",
  "invalid-input-secret" =>	"secret parameter is invalid or malformed",
  "missing-input-response" =>	"response parameter is missing",
  "invalid-input-response" =>	"response parameter is invalid or malformed",
  "bad-request" =>	"request is invalid or malformed"
}

Constants included from Helpers

Helpers::SET_PATTERN_TEST_REGEXP

Constants included from Event::Api

Event::Api::OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Card::Set

reset_modules

Methods included from I18nScope

#mod_name, #scope

Methods included from Loader

#clean_empty_module_from_hash, #clean_empty_modules, #extended, #process_base_modules, #register_set

Methods included from Helpers

#method_missing, #num_set_parts, #pattern_code, #respond_to_missing?, #set_name_parts, #shortname, #underscore

Methods included from Card::Set::AdvancedApi

#attachment, #define_set_from_error, #ensure_set, #setting_opts, #stage_method

Methods included from Format

#before, #format, layout_method_name, #view, view_method_name, view_setting_method_name, wrapper_method_name

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Basket

#abstract_basket, #add_to_basket, #basket, #unshift_basket

Methods included from Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Class Method Details

.source_locationObject



7
# File 'card/tmpsets/set/mod034-card-mod-recaptcha/all/recaptcha.rb', line 7

def self.source_location; "/Users/ethan/dev/decko/gem/card-mod-recaptcha/set/all/recaptcha.rb"; end

Instance Method Details

#add_recaptcha_errors(error_codes) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'card/tmpsets/set/mod034-card-mod-recaptcha/all/recaptcha.rb', line 32

def add_recaptcha_errors error_codes
  if error_codes.present?
    error_codes.each do |code|
      errors.add :recaptcha, RECAPTCHA_ERROR_CODES.fetch(code, code)
    end
  else
    errors.add :recaptcha, "Looks like you are not a human" # LOCALIZE
  end
end

#event: recaptchaObject



57
58
59
60
61
62
# File 'card/tmpsets/set/mod034-card-mod-recaptcha/all/recaptcha.rb', line 57

event :recaptcha, :validate, when: :validate_recaptcha? do
  handle_recaptcha_config_errors do
    Env[:recaptcha_used] = true
    human?
  end
end

#handle_recaptcha_config_errorsObject



64
65
66
67
68
69
70
71
72
# File 'card/tmpsets/set/mod034-card-mod-recaptcha/all/recaptcha.rb', line 64

def handle_recaptcha_config_errors
  if Env.params[:recaptcha_token] == "grecaptcha-undefined"
    errors.add "recaptcha", "needs correct v3 configuration" # LOCALILZE
  elsif Env.params[:recaptcha_token] == "recaptcha-token-field-missing"
    raise Card::Error, "recaptcha token field missing" # LOCALILZE
  else
    yield
  end
end

#human?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'card/tmpsets/set/mod034-card-mod-recaptcha/all/recaptcha.rb', line 16

def human?
  result = JSON.parse recaptcha_response
  return if recaptcha_success?(result)

  add_recaptcha_errors result["error-codes"]
end

#recaptcha_keys?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'card/tmpsets/set/mod034-card-mod-recaptcha/all/recaptcha.rb', line 53

def recaptcha_keys?
  Card.config.recaptcha_site_key && Card.config.recaptcha_secret_key
end

#recaptcha_on?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
# File 'card/tmpsets/set/mod034-card-mod-recaptcha/all/recaptcha.rb', line 23

def recaptcha_on?
  recaptcha_keys? &&
    Env[:controller] &&
    !Auth.signed_in? &&
    !Auth.always_ok? &&
    !Auth.needs_setup? &&
    Card::Rule.toggle(rule(:captcha))
end

#recaptcha_responseObject



48
49
50
51
# File 'card/tmpsets/set/mod034-card-mod-recaptcha/all/recaptcha.rb', line 48

def recaptcha_response
  ::Recaptcha.get({ secret: Card.config.recaptcha_secret_key,
                    response: Env.params[:recaptcha_token] }, {})
end

#recaptcha_success?(result) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'card/tmpsets/set/mod034-card-mod-recaptcha/all/recaptcha.rb', line 42

def recaptcha_success? result
  result['success'] &&
    (result['score'].to_f >= Cardio.config.recaptcha_minimum_score) &&
    (result['action'].to_sym == action.to_sym)
end

#validate_recaptcha?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'card/tmpsets/set/mod034-card-mod-recaptcha/all/recaptcha.rb', line 75

def validate_recaptcha?
  !@supercard && !Env[:recaptcha_used] && recaptcha_on?
end