Class: PxModule::PerimeterxCaptchaValidator

Inherits:
PerimeterxRiskClient show all
Defined in:
lib/perimeterx/internal/validators/perimeter_x_captcha_validator.rb

Instance Attribute Summary

Attributes inherited from PerimeterxRiskClient

#http_client, #px_config

Instance Method Summary collapse

Methods inherited from PerimeterxRiskClient

#format_headers

Constructor Details

#initialize(px_config, http_client) ⇒ PerimeterxCaptchaValidator

Returns a new instance of PerimeterxCaptchaValidator.



6
7
8
# File 'lib/perimeterx/internal/validators/perimeter_x_captcha_validator.rb', line 6

def initialize(px_config, http_client)
  super(px_config, http_client)
end

Instance Method Details

#send_captcha_request(vid, uuid, captcha, px_ctx) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/perimeterx/internal/validators/perimeter_x_captcha_validator.rb', line 10

def send_captcha_request(vid, uuid, captcha, px_ctx)

  request_body = {
    :request => {
      :ip => px_ctx.context[:ip],
      :headers => format_headers(px_ctx),
      :uri => px_ctx.context[:uri]
    },
    :pxCaptcha => captcha,
    :vid => vid,
    :uuid => uuid,
    :hostname => px_ctx.context[:hostname]
  }

  # Prepare request
  headers = {
      "Authorization" => "Bearer #{@px_config[:auth_token]}" ,
      "Content-Type" => "application/json"
  };

  return @http_client.post(PxModule::API_V1_CAPTCHA, request_body, headers, @px_config[:api_timeout])

end

#verify(px_ctx) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/perimeterx/internal/validators/perimeter_x_captcha_validator.rb', line 34

def verify(px_ctx)
  captcha_validated = false
  begin
    if(!px_ctx.context.key?(:px_captcha))
      return captcha_validated, px_ctx
    end
    captcha, vid, uuid = px_ctx.context[:px_captcha].split(':', 3)
    if captcha.nil? || vid.nil? || uuid.nil?
      return captcha_validated, px_ctx
    end

    px_ctx.context[:vid] = vid
    px_ctx.context[:uuid] = uuid
    response = send_captcha_request(vid, uuid, captcha, px_ctx)

    if (response.status_code == 200)
      response_body = eval(response.body)
      if ( response_body[:status] == 0 )
        captcha_validated = true
      end
    end

    return captcha_validated, px_ctx

  rescue Exception => e
    @logger.error("PerimeterxCaptchaValidator[verify]: failed, returning false")
    return captcha_validated, px_ctx
  end
end