Class: PxModule::PerimeterxCookieValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/perimeterx/internal/validators/perimeter_x_cookie_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(px_config) ⇒ PerimeterxCookieValidator

Returns a new instance of PerimeterxCookieValidator.



11
12
13
14
# File 'lib/perimeterx/internal/validators/perimeter_x_cookie_validator.rb', line 11

def initialize(px_config)
  @px_config = px_config
  @logger = px_config[:logger]
end

Instance Attribute Details

#px_configObject

Returns the value of attribute px_config.



9
10
11
# File 'lib/perimeterx/internal/validators/perimeter_x_cookie_validator.rb', line 9

def px_config
  @px_config
end

Instance Method Details

#verify(px_ctx) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
63
64
65
66
67
# File 'lib/perimeterx/internal/validators/perimeter_x_cookie_validator.rb', line 17

def verify(px_ctx)
  begin
    # Case no cookie
    if px_ctx.context[:px_cookie].empty?
      @logger.warn("PerimeterxCookieValidator:[verify]: cookie not found")
      px_ctx.context[:s2s_call_reason] = PxModule::NO_COOKIE
      return false, px_ctx
    end

    # Deserialize cookie start
    cookie = PerimeterxCookie.px_cookie_factory(px_ctx, @px_config)
    if (!cookie.deserialize())
      @logger.warn("PerimeterxCookieValidator:[verify]: invalid cookie")
      px_ctx.context[:s2s_call_reason] =  PxModule::COOKIE_DECRYPTION_FAILED
      return false, px_ctx
    end
    px_ctx.context[:decoded_cookie] = cookie.decoded_cookie
    px_ctx.context[:score] = cookie.cookie_score()
    px_ctx.context[:uuid] = cookie.decoded_cookie[:u]
    px_ctx.context[:vid] = cookie.decoded_cookie[:v]
    px_ctx.context[:block_action] = px_ctx.set_block_action_type(cookie.cookie_block_action())
    px_ctx.context[:cookie_hmac] = cookie.cookie_hmac()

    if (cookie.expired?)
      @logger.warn("PerimeterxCookieValidator:[verify]: cookie expired")
      px_ctx.context[:s2s_call_reason] = PxModule::EXPIRED_COOKIE
      return false, px_ctx
    end

    if (cookie.high_score?)
      @logger.warn("PerimeterxCookieValidator:[verify]: cookie high score")
      px_ctx.context[:s2s_call_reason] = PxModule::COOKIE_HIGH_SCORE
      return true, px_ctx
    end

    if (!cookie.secured?)
      @logger.warn("PerimeterxCookieValidator:[verify]: cookie invalid hmac")
      px_ctx.context[:s2s_call_reason] = PxModule::COOKIE_VALIDATION_FAILED
      return false, px_ctx
    end

    @logger.debug("PerimeterxCookieValidator:[verify]: cookie validation passed succesfully")

    return true, px_ctx
  rescue Exception => e
    @logger.error("PerimeterxCookieValidator:[verify]: exception while verifying cookie => #{e.message}")
    px_ctx.context[:px_orig_cookie] = cookie.px_cookie
    px_ctx.context[:s2s_call_reason] = PxModule::COOKIE_DECRYPTION_FAILED
    return false, px_ctx
  end
end