1212require 'perimeterx/internal/validators/perimeter_x_s2s_validator'
1313require 'perimeterx/internal/validators/perimeter_x_cookie_validator'
1414require 'perimeterx/internal/exceptions/px_config_exception'
15+ require 'perimeterx/internal/first_party/px_first_party'
1516
1617module PxModule
1718 # Module expose API
1819 def px_verify_request ( request_config = { } )
1920 begin
2021 px_instance = PerimeterX . new ( request_config )
21- px_ctx = px_instance . verify ( request . env )
22+ req = ActionDispatch ::Request . new ( request . env )
23+
24+ # handle first party requests
25+ if px_instance . first_party . is_first_party_request ( req )
26+ render_first_party_response ( req , px_instance )
27+ return true
28+ end
29+
30+ # verify request
31+ px_ctx = px_instance . verify ( req )
2232 px_config = px_instance . px_config
2333
2434 msg_title = 'PxModule[px_verify_request]'
@@ -46,12 +56,21 @@ def px_verify_request(request_config={})
4656 end
4757
4858 is_mobile = px_ctx . context [ :cookie_origin ] == 'header' ? '1' : '0'
49- action = px_ctx . context [ :block_action ] [ 0 , 1 ]
59+ action = px_ctx . context [ :block_action ] [ 0 , 1 ]
5060
51- px_template_object = {
61+ if px_config [ :first_party_enabled ]
62+ px_template_object = {
63+ js_client_src : "/#{ px_config [ :app_id ] [ 2 ..-1 ] } /init.js" ,
64+ block_script : "/#{ px_config [ :app_id ] [ 2 ..-1 ] } /captcha/#{ px_config [ :app_id ] } /captcha.js?a=#{ action } &u=#{ px_ctx . context [ :uuid ] } &v=#{ px_ctx . context [ :vid ] } &m=#{ is_mobile } " ,
65+ host_url : "/#{ px_config [ :app_id ] [ 2 ..-1 ] } /xhr"
66+ }
67+ else
68+ px_template_object = {
69+ js_client_src : "//#{ PxModule ::CLIENT_HOST } /#{ px_config [ :app_id ] } /main.min.js" ,
5270 block_script : "//#{ PxModule ::CAPTCHA_HOST } /#{ px_config [ :app_id ] } /captcha.js?a=#{ action } &u=#{ px_ctx . context [ :uuid ] } &v=#{ px_ctx . context [ :vid ] } &m=#{ is_mobile } " ,
53- js_client_src : "//#{ PxModule ::CLIENT_HOST } /#{ px_config [ :app_id ] } /main.min.js"
54- }
71+ host_url : "https://collector-#{ px_config [ :app_id ] } .perimeterx.net"
72+ }
73+ end
5574
5675 html = PxTemplateFactory . get_template ( px_ctx , px_config , px_template_object )
5776
@@ -68,7 +87,7 @@ def px_verify_request(request_config={})
6887 hash_json = {
6988 :appId => px_config [ :app_id ] ,
7089 :jsClientSrc => px_template_object [ :js_client_src ] ,
71- :firstPartyEnabled => false ,
90+ :firstPartyEnabled => px_ctx . context [ :first_party_enabled ] ,
7291 :uuid => px_ctx . context [ :uuid ] ,
7392 :vid => px_ctx . context [ :vid ] ,
7493 :hostUrl => "https://collector-#{ px_config [ :app_id ] } .perimeterx.net" ,
@@ -110,6 +129,29 @@ def px_verify_request(request_config={})
110129 end
111130 end
112131
132+ def render_first_party_response ( req , px_instance )
133+ fp = px_instance . first_party
134+ px_config = px_instance . px_config
135+
136+ if px_config [ :first_party_enabled ]
137+ # first party enabled - proxy response
138+ fp_response = fp . get_first_party_response ( req )
139+ response . status = fp_response . code
140+ fp_response . to_hash . each do |header_name , header_value_arr |
141+ if header_name !="content-length"
142+ response . headers [ header_name ] = header_value_arr [ 0 ]
143+ end
144+ end
145+ res_type = fp . get_response_content_type ( req )
146+ render res_type => fp_response . body
147+ else
148+ # first party disabled - return empty response
149+ response . status = "200"
150+ res_type = fp . get_response_content_type ( req )
151+ render res_type => ""
152+ end
153+ end
154+
113155 def self . configure ( basic_config )
114156 PerimeterX . set_basic_config ( basic_config )
115157 end
@@ -119,6 +161,7 @@ def self.configure(basic_config)
119161 class PerimeterX
120162
121163 attr_reader :px_config
164+ attr_reader :first_party
122165 attr_accessor :px_http_client
123166 attr_accessor :px_activity_client
124167
@@ -128,7 +171,7 @@ def self.set_basic_config(basic_config)
128171 end
129172
130173 #Instance Methods
131- def verify ( env )
174+ def verify ( req )
132175 begin
133176
134177 # check module_enabled
@@ -137,13 +180,11 @@ def verify(env)
137180 @logger . warn ( 'Module is disabled' )
138181 return nil
139182 end
140-
141- req = ActionDispatch ::Request . new ( env )
142183
143184 # filter whitelist routes
144185 url_path = URI . parse ( req . original_url ) . path
145186 if url_path && !url_path . empty?
146- if check_whitelist_routes ( px_config [ :whitelist_routes ] , url_path )
187+ if check_whitelist_routes ( px_config [ :whitelist_routes ] , url_path )
147188 @logger . debug ( "PerimeterX[pxVerify]: whitelist route: #{ url_path } " )
148189 return nil
149190 end
@@ -176,6 +217,7 @@ def initialize(request_config)
176217 @px_http_client = PxHttpClient . new ( @px_config )
177218
178219 @px_activity_client = PerimeterxActivitiesClient . new ( @px_config , @px_http_client )
220+ @first_party = FirstPartyManager . new ( @px_config , @px_http_client , @logger )
179221
180222 @px_cookie_validator = PerimeterxCookieValidator . new ( @px_config )
181223 @px_s2s_validator = PerimeterxS2SValidator . new ( @px_config , @px_http_client )
0 commit comments