Skip to content

Commit ed8b1a9

Browse files
authored
docs(samples): Set the assessment.event.expected_action when sending a CreateAssessment request. (GoogleCloudPlatform#9231)
1 parent 787924f commit ed8b1a9

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

recaptcha_enterprise/demosite/src/main/java/app/MainController.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ public static ModelAndView home() {
119119
try {
120120
// <!-- ATTENTION: reCAPTCHA Example (Server Part 1/2) Starts -->
121121
assessmentResponse = CreateAssessment.createAssessment(
122-
CONTEXT.get("project_id"),
123-
CONTEXT.get("site_key"),
124-
jsonData.get("token"));
122+
CONTEXT.get("project_id"), CONTEXT.get("site_key"),
123+
jsonData.get("token"), recaptchaAction);
125124

126125
// Check if the token is valid, score is above threshold score and the action equals expected.
127126
// Take action based on the result (BAD / NOT_BAD).
@@ -172,9 +171,8 @@ public static ModelAndView signup() {
172171
try {
173172
// <!-- ATTENTION: reCAPTCHA Example (Server Part 1/2) Starts -->
174173
assessmentResponse = CreateAssessment.createAssessment(
175-
CONTEXT.get("project_id"),
176-
CONTEXT.get("site_key"),
177-
jsonData.get("token").toString());
174+
CONTEXT.get("project_id"), CONTEXT.get("site_key"),
175+
jsonData.get("token").toString(), recaptchaAction);
178176

179177
// Check if the token is valid, score is above threshold score and the action equals expected.
180178
// Take action based on the result (BAD / NOT_BAD).
@@ -227,9 +225,8 @@ public static ModelAndView login() {
227225
try {
228226
// <!-- ATTENTION: reCAPTCHA Example (Server Part 1/2) Starts -->
229227
assessmentResponse = CreateAssessment.createAssessment(
230-
CONTEXT.get("project_id"),
231-
CONTEXT.get("site_key"),
232-
jsonData.get("token"));
228+
CONTEXT.get("project_id"), CONTEXT.get("site_key"),
229+
jsonData.get("token"), recaptchaAction);
233230

234231
// Check if the token is valid, score is above threshold score and the action equals expected.
235232
// Take action based on the result (BAD / NOT_BAD).
@@ -282,9 +279,8 @@ public static ModelAndView store() {
282279
try {
283280
// <!-- ATTENTION: reCAPTCHA Example (Server Part 1/2) Starts -->
284281
assessmentResponse = CreateAssessment.createAssessment(
285-
CONTEXT.get("project_id"),
286-
CONTEXT.get("site_key"),
287-
jsonData.get("token").toString());
282+
CONTEXT.get("project_id"), CONTEXT.get("site_key"),
283+
jsonData.get("token").toString(), recaptchaAction);
288284

289285
// Check if the token is valid, score is above threshold score and the action equals expected.
290286
// Take action based on the result (BAD / NOT_BAD).
@@ -336,9 +332,8 @@ public static ModelAndView comment() {
336332
try {
337333
// <!-- ATTENTION: reCAPTCHA Example (Server Part 1/2) Starts -->
338334
assessmentResponse = CreateAssessment.createAssessment(
339-
CONTEXT.get("project_id"),
340-
CONTEXT.get("site_key"),
341-
jsonData.get("token"));
335+
CONTEXT.get("project_id"), CONTEXT.get("site_key"),
336+
jsonData.get("token"), recaptchaAction);
342337

343338
// Check if the token is valid, score is above threshold score and the action equals expected.
344339
// Take action based on the result (BAD / NOT_BAD).

recaptcha_enterprise/demosite/src/main/java/recaptcha/CreateAssessment.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,26 @@ public class CreateAssessment {
2828
* Create an assessment to analyze the risk of a UI action.
2929
*
3030
* @param projectID : Google Cloud Project ID
31-
* @param recaptchaSiteKey : Site key obtained by registering a domain/app to use recaptcha
32-
* services. (score/ checkbox type)
33-
* @param token : The token obtained from the client on passing the recaptchaSiteKey.
31+
* @param recaptchaSiteKey : Site key obtained by registering a domain/app to
32+
* use recaptcha services. (score/ checkbox type)
33+
* @param token : The token obtained from the client on passing the
34+
* recaptchaSiteKey.
35+
* @param expectedAction : The expected action for this type of event.
3436
* @return Assessment response.
3537
*/
36-
public static Assessment createAssessment(
37-
String projectID, String recaptchaSiteKey, String token)
38+
public static Assessment createAssessment(String projectID,
39+
String recaptchaSiteKey,
40+
String token, String expectedAction)
3841
throws Exception {
3942

4043
// <!-- ATTENTION: reCAPTCHA Example (Server Part 2/2) Starts -->
4144
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
4245
// Set the properties of the event to be tracked.
4346
Event event = Event.newBuilder()
44-
.setSiteKey(recaptchaSiteKey)
45-
.setToken(token)
46-
.build();
47+
.setSiteKey(recaptchaSiteKey)
48+
.setToken(token)
49+
.setExpectedAction(expectedAction)
50+
.build();
4751

4852
// Build the assessment request.
4953
CreateAssessmentRequest createAssessmentRequest =

0 commit comments

Comments
 (0)