Skip to content

Commit

Permalink
docs(samples): Set the assessment.event.expected_action when sending …
Browse files Browse the repository at this point in the history
…a CreateAssessment request. (#9231)
  • Loading branch information
Cory-Kramer committed Feb 2, 2024
1 parent 787924f commit ed8b1a9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
25 changes: 10 additions & 15 deletions recaptcha_enterprise/demosite/src/main/java/app/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ public static ModelAndView home() {
try {
// <!-- ATTENTION: reCAPTCHA Example (Server Part 1/2) Starts -->
assessmentResponse = CreateAssessment.createAssessment(
CONTEXT.get("project_id"),
CONTEXT.get("site_key"),
jsonData.get("token"));
CONTEXT.get("project_id"), CONTEXT.get("site_key"),
jsonData.get("token"), recaptchaAction);

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

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

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

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

// Check if the token is valid, score is above threshold score and the action equals expected.
// Take action based on the result (BAD / NOT_BAD).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,26 @@ public class CreateAssessment {
* Create an assessment to analyze the risk of a UI action.
*
* @param projectID : Google Cloud Project ID
* @param recaptchaSiteKey : Site key obtained by registering a domain/app to use recaptcha
* services. (score/ checkbox type)
* @param token : The token obtained from the client on passing the recaptchaSiteKey.
* @param recaptchaSiteKey : Site key obtained by registering a domain/app to
* use recaptcha services. (score/ checkbox type)
* @param token : The token obtained from the client on passing the
* recaptchaSiteKey.
* @param expectedAction : The expected action for this type of event.
* @return Assessment response.
*/
public static Assessment createAssessment(
String projectID, String recaptchaSiteKey, String token)
public static Assessment createAssessment(String projectID,
String recaptchaSiteKey,
String token, String expectedAction)
throws Exception {

// <!-- ATTENTION: reCAPTCHA Example (Server Part 2/2) Starts -->
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
// Set the properties of the event to be tracked.
Event event = Event.newBuilder()
.setSiteKey(recaptchaSiteKey)
.setToken(token)
.build();
.setSiteKey(recaptchaSiteKey)
.setToken(token)
.setExpectedAction(expectedAction)
.build();

// Build the assessment request.
CreateAssessmentRequest createAssessmentRequest =
Expand Down

0 comments on commit ed8b1a9

Please sign in to comment.