Class: Turnstiled::Verifier

Inherits:
Object
  • Object
show all
Defined in:
lib/turnstiled/verifier.rb

Instance Method Summary collapse

Instance Method Details

#clientObject



17
18
19
20
21
22
# File 'lib/turnstiled/verifier.rb', line 17

def client
  @client ||= Faraday.new(client_options) do |f|
    f.request :json
    f.response :json
  end
end

#client_optionsObject



24
25
26
27
28
29
30
31
# File 'lib/turnstiled/verifier.rb', line 24

def client_options
  {
    url: "https://challenges.cloudflare.com/turnstile/v0",
    request: {
      timeout: 3
    }
  }
end

#verify(request) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/turnstiled/verifier.rb', line 5

def verify(request)
  return request.params["cf-turnstile-response"] == "1" if Turnstiled.mock

  response = client.post("siteverify", {
    secret: Turnstiled.site_secret,
    response: request.params["cf-turnstile-response"],
    remoteip: request.remote_ip
  })

  response.body.fetch("success", false)
end