-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDefaultVerificationHandler.java
More file actions
92 lines (75 loc) · 3.89 KB
/
DefaultVerificationHandler.java
File metadata and controls
92 lines (75 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.perimeterx.api.verificationhandler;
import com.perimeterx.api.activities.ActivityHandler;
import com.perimeterx.api.blockhandler.BlockHandler;
import com.perimeterx.models.PXContext;
import com.perimeterx.models.configuration.PXConfiguration;
import com.perimeterx.models.exceptions.PXException;
import com.perimeterx.utils.PXLogger;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import static com.perimeterx.utils.PXCommonUtils.logTime;
import static com.perimeterx.utils.PXLogger.LogReason.DEBUG_S2S_SCORE_IS_HIGHER_THAN_BLOCK;
import static com.perimeterx.utils.PXLogger.LogReason.DEBUG_S2S_SCORE_IS_LOWER_THAN_BLOCK;
/**
* Created by nitzangoldfeder on 28/05/2017.
*/
public class DefaultVerificationHandler implements VerificationHandler {
private static final PXLogger logger = PXLogger.getLogger(DefaultVerificationHandler.class);
private PXConfiguration pxConfiguration;
private ActivityHandler activityHandler;
private BlockHandler blockHandler;
public DefaultVerificationHandler(PXConfiguration pxConfiguration, ActivityHandler activityHandler) {
this.pxConfiguration = pxConfiguration;
this.activityHandler = activityHandler;
this.blockHandler = pxConfiguration.getBlockHandler();
}
@Override
public boolean handleVerification(PXContext context, HttpServletResponseWrapper responseWrapper) throws PXException {
boolean verified = logTime("shouldPassRequest", () -> shouldPassRequest(context));
logTime("setPxhdCookie", () -> setPxhdCookie(context, responseWrapper));
if (!verified && !context.isMonitoredRequest()) {
logTime("blockHandler.handleBlocking",() -> this.blockHandler.handleBlocking(context, this.pxConfiguration, responseWrapper));
}
logTime("anonymousAsyncActivitiesSender" , () -> {
try {
if (verified) {
logger.debug("Passing request {} {}", verified, this.pxConfiguration.getModuleMode());
// Not blocking request and sending page_requested activity to px if configured as true
if (this.pxConfiguration.isSendPageActivities()) {
this.activityHandler.handlePageRequestedActivity(context);
}
} else {
logger.debug("Request invalid");
this.activityHandler.handleBlockActivity(context);
}
} catch (PXException pxException) {
logger.error("Error occurred while handle activities", pxException);
}
});
return verified || context.isMonitoredRequest();
}
private void setPxhdCookie(PXContext context, HttpServletResponseWrapper responseWrapper) {
try {
if (!StringUtils.isEmpty(context.getResponsePxhd()) && !context.getResponsePxhd().equals(context.getPxhd())) {
String pxhdCookieValue = context.getResponsePxhd();
Cookie cookie = new Cookie("_pxhd", URLEncoder.encode(pxhdCookieValue, "UTF-8"));
cookie.setPath("/");
cookie.setMaxAge(3600 * 24 * 365);
responseWrapper.addCookie(cookie);
}
} catch (UnsupportedEncodingException e) {
logger.error(e.getMessage());
}
}
private boolean shouldPassRequest(PXContext context) {
int score = context.getRiskScore();
int blockingScore = this.pxConfiguration.getBlockingScore();
// If should block this request we will apply our block handle and send the block activity to px
boolean verified = score < blockingScore;
logger.debug(verified ? DEBUG_S2S_SCORE_IS_LOWER_THAN_BLOCK : DEBUG_S2S_SCORE_IS_HIGHER_THAN_BLOCK, score, blockingScore);
return verified;
}
}