Class: SpreeCmCommissioner::TurnstileTokenValidator

Inherits:
BaseInteractor show all
Defined in:
app/interactors/spree_cm_commissioner/turnstile_token_validator.rb

Constant Summary collapse

SITE_VERIFY_URL =
'https://challenges.cloudflare.com/turnstile/v0/siteverify'.freeze

Instance Method Summary collapse

Instance Method Details

#callObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/interactors/spree_cm_commissioner/turnstile_token_validator.rb', line 7

def call
  context.fail!(message: I18n.t('turnstile.token_missing')) if token.blank?

  response = verify_token
  body = parse_response(response)

  unless body['success']
    error_codes = body['error-codes']&.join(', ') || 'unknown'
    Rails.logger.warn(
      "Turnstile verification failed: #{error_codes} " \
      "from IP #{remote_ip} | hostname=#{body['hostname']}"
    )
    context.fail!(message: I18n.t('turnstile.verification_failed'))
  end

  context.challenge_ts = body['challenge_ts']
  context.hostname = body['hostname']
  context.action = body['action']
end