Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
chore: regenerate README (#541)
Browse files Browse the repository at this point in the history
This PR was generated using Autosynth. 🌈


<details><summary>Log from Synthtool</summary>

```
2021-08-27 16:56:48,130 synthtool [DEBUG] > Executing /root/.cache/synthtool/java-recaptchaenterprise/.github/readme/synth.py.
On branch autosynth-readme
nothing to commit, working tree clean
2021-08-27 16:56:49,179 synthtool [DEBUG] > Wrote metadata to .github/readme/synth.metadata/synth.metadata.

```
</details>

Full log will be available here:
https://source.cloud.google.com/results/invocations/d2977c74-0405-4584-b4bf-ce1faa3ada13/targets

- [ ] To automatically regenerate this PR, check this box. (May take up to 24 hours.)
  • Loading branch information
yoshi-automation committed Aug 27, 2021
1 parent f09d9ee commit 52388a9
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 73 deletions.
4 changes: 2 additions & 2 deletions .github/readme/synth.metadata/synth.metadata
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"git": {
"name": ".",
"remote": "https://github.com/googleapis/java-recaptchaenterprise.git",
"sha": "52e202bb54126817a212767197d160f6642b872a"
"sha": "f09d9ee7ebf21d10953ad624422e6cb57256a7f3"
}
},
{
"git": {
"name": "synthtool",
"remote": "https://github.com/googleapis/synthtool.git",
"sha": "a01e1a0bf70754f51450958b966bd673945d1e6e"
"sha": "31728d872f50e439ded2f67fa8d67955b26a4e35"
}
}
]
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ use this reCAPTCHA Enterprise Client Library.



## Samples

Samples are in the [`samples/`](https://github.com/googleapis/java-recaptchaenterprise/tree/master/samples) directory.

| Sample | Source Code | Try it |
| --------------------------- | --------------------------------- | ------ |
| Main | [source code](https://github.com/googleapis/java-recaptchaenterprise/blob/master/samples/snippets/cloud-client/src/main/java/app/Main.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-recaptchaenterprise&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/app/Main.java) |
| Main Controller | [source code](https://github.com/googleapis/java-recaptchaenterprise/blob/master/samples/snippets/cloud-client/src/main/java/app/MainController.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-recaptchaenterprise&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/app/MainController.java) |
| Create Assessment | [source code](https://github.com/googleapis/java-recaptchaenterprise/blob/master/samples/snippets/cloud-client/src/main/java/recaptcha/CreateAssessment.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-recaptchaenterprise&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/recaptcha/CreateAssessment.java) |
| Create Site Key | [source code](https://github.com/googleapis/java-recaptchaenterprise/blob/master/samples/snippets/cloud-client/src/main/java/recaptcha/CreateSiteKey.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-recaptchaenterprise&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/recaptcha/CreateSiteKey.java) |
| Delete Site Key | [source code](https://github.com/googleapis/java-recaptchaenterprise/blob/master/samples/snippets/cloud-client/src/main/java/recaptcha/DeleteSiteKey.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-recaptchaenterprise&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/recaptcha/DeleteSiteKey.java) |
| Get Site Key | [source code](https://github.com/googleapis/java-recaptchaenterprise/blob/master/samples/snippets/cloud-client/src/main/java/recaptcha/GetSiteKey.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-recaptchaenterprise&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/recaptcha/GetSiteKey.java) |
| List Site Keys | [source code](https://github.com/googleapis/java-recaptchaenterprise/blob/master/samples/snippets/cloud-client/src/main/java/recaptcha/ListSiteKeys.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-recaptchaenterprise&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/recaptcha/ListSiteKeys.java) |
| Update Site Key | [source code](https://github.com/googleapis/java-recaptchaenterprise/blob/master/samples/snippets/cloud-client/src/main/java/recaptcha/UpdateSiteKey.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-recaptchaenterprise&page=editor&open_in_editor=samples/snippets/cloud-client/src/main/java/recaptcha/UpdateSiteKey.java) |



## Troubleshooting

Expand Down
1 change: 0 additions & 1 deletion samples/snippets/cloud-client/src/main/java/app/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping
public class MainController {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,17 @@ public static void main(String[] args) throws IOException {
createAssessment(projectID, recaptchaSiteKey, token, recaptchaAction);
}


/**
* Create an assessment to analyze the risk of an UI action.
*
* @param projectID : GCloud Project ID
* @param recaptchaSiteKey : Site key obtained by registering a domain/app to use recaptcha
* services.
* services.
* @param token : The token obtained from the client on passing the recaptchaSiteKey.
* @param recaptchaAction : Action name corresponding to the token.
*/
public static void createAssessment(String projectID, String recaptchaSiteKey, String token,
String recaptchaAction)
public static void createAssessment(
String projectID, String recaptchaSiteKey, String token, String recaptchaAction)
throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
Expand All @@ -60,33 +59,36 @@ public static void createAssessment(String projectID, String recaptchaSiteKey, S
// Specify a name for this assessment.
String assessmentName = "assessment-name";
// Set the properties of the event to be tracked.
Event event = Event.newBuilder()
.setSiteKey(recaptchaSiteKey)
.setToken(token)
.build();
Event event = Event.newBuilder().setSiteKey(recaptchaSiteKey).setToken(token).build();

// Build the assessment request.
CreateAssessmentRequest createAssessmentRequest = CreateAssessmentRequest.newBuilder()
.setParent(ProjectName.of(projectID).toString())
.setAssessment(Assessment.newBuilder().setEvent(event).setName(assessmentName).build())
.build();
CreateAssessmentRequest createAssessmentRequest =
CreateAssessmentRequest.newBuilder()
.setParent(ProjectName.of(projectID).toString())
.setAssessment(
Assessment.newBuilder().setEvent(event).setName(assessmentName).build())
.build();

Assessment response = client.createAssessment(createAssessmentRequest);

// Check if the token is valid.
if (!response.getTokenProperties().getValid()) {
System.out.println("The CreateAssessment call failed because the token was: " +
response.getTokenProperties().getInvalidReason().name());
System.out.println(
"The CreateAssessment call failed because the token was: "
+ response.getTokenProperties().getInvalidReason().name());
return;
}

// Check if the expected action was executed.
if (!response.getTokenProperties().getAction().equals(recaptchaAction)) {
System.out.println(
"The action attribute in reCAPTCHA tag is: " + response.getTokenProperties()
.getAction());
System.out.println("The action attribute in the reCAPTCHA tag " +
"does not match the action (" + recaptchaAction + ") you are expecting to score");
"The action attribute in reCAPTCHA tag is: "
+ response.getTokenProperties().getAction());
System.out.println(
"The action attribute in the reCAPTCHA tag "
+ "does not match the action ("
+ recaptchaAction
+ ") you are expecting to score");
return;
}

Expand All @@ -102,4 +104,4 @@ public static void createAssessment(String projectID, String recaptchaSiteKey, S
}
}
}
// [END recaptcha_enterprise_create_assessment]
// [END recaptcha_enterprise_create_assessment]
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.google.recaptchaenterprise.v1.WebKeySettings.IntegrationType;
import java.io.IOException;


public class CreateSiteKey {

public static void main(String[] args) throws IOException {
Expand All @@ -52,18 +51,22 @@ public static String createSiteKey(String projectID, String domainName) throws I

// Set the type of reCAPTCHA to be displayed.
// For different types, see: https://cloud.google.com/recaptcha-enterprise/docs/keys
Key scoreKey = Key.newBuilder()
.setDisplayName("any_descriptive_name_for_the_key")
.setWebSettings(WebKeySettings.newBuilder()
.addAllowedDomains(domainName)
.setAllowAmpTraffic(false)
.setIntegrationType(IntegrationType.SCORE).build())
.build();
Key scoreKey =
Key.newBuilder()
.setDisplayName("any_descriptive_name_for_the_key")
.setWebSettings(
WebKeySettings.newBuilder()
.addAllowedDomains(domainName)
.setAllowAmpTraffic(false)
.setIntegrationType(IntegrationType.SCORE)
.build())
.build();

CreateKeyRequest createKeyRequest = CreateKeyRequest.newBuilder()
.setParent(ProjectName.of(projectID).toString())
.setKey(scoreKey)
.build();
CreateKeyRequest createKeyRequest =
CreateKeyRequest.newBuilder()
.setParent(ProjectName.of(projectID).toString())
.setKey(scoreKey)
.build();

// Get the name of the created reCAPTCHA site key.
Key response = client.createKey(createKeyRequest);
Expand All @@ -75,4 +78,3 @@ public static String createSiteKey(String projectID, String domainName) throws I
}
}
// [END recaptcha_enterprise_create_site_key]

Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ public static void deleteSiteKey(String projectID, String recaptchaSiteKey)
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {

// Set the project ID and reCAPTCHA site key.
DeleteKeyRequest deleteKeyRequest = DeleteKeyRequest.newBuilder()
.setName(KeyName.of(projectID, recaptchaSiteKey).toString())
.build();
DeleteKeyRequest deleteKeyRequest =
DeleteKeyRequest.newBuilder()
.setName(KeyName.of(projectID, recaptchaSiteKey).toString())
.build();

client.deleteKeyCallable().futureCall(deleteKeyRequest).get(5, TimeUnit.SECONDS);
System.out.println("reCAPTCHA Site key successfully deleted !");
}
}
}
// [END recaptcha_enterprise_delete_site_key]
// [END recaptcha_enterprise_delete_site_key]
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public static void main(String[] args)
getSiteKey(projectID, recaptchaSiteKey);
}


/**
* Get the reCAPTCHA site key present under the project ID.
*
Expand All @@ -55,9 +54,10 @@ public static void getSiteKey(String projectID, String recaptchaSiteKey)
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {

// Construct the "GetSiteKey" request.
GetKeyRequest getKeyRequest = GetKeyRequest.newBuilder()
.setName(KeyName.of(projectID, recaptchaSiteKey).toString())
.build();
GetKeyRequest getKeyRequest =
GetKeyRequest.newBuilder()
.setName(KeyName.of(projectID, recaptchaSiteKey).toString())
.build();

// Wait for the operation to complete.
ApiFuture<Key> futureCall = client.getKeyCallable().futureCall(getKeyRequest);
Expand All @@ -67,4 +67,4 @@ public static void getSiteKey(String projectID, String recaptchaSiteKey)
}
}
}
// [END recaptcha_enterprise_get_site_key]
// [END recaptcha_enterprise_get_site_key]
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ public static void listSiteKeys(String projectID) throws IOException {
// clean up any remaining background resources.
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
// Set the project ID to list the keys present in it.
ListKeysRequest listKeysRequest = ListKeysRequest.newBuilder()
.setParent(ProjectName.of(projectID).toString())
.build();
ListKeysRequest listKeysRequest =
ListKeysRequest.newBuilder().setParent(ProjectName.of(projectID).toString()).build();

System.out.println("Listing reCAPTCHA site keys: ");
for (Key key : client.listKeys(listKeysRequest).iterateAll()) {
Expand All @@ -56,4 +55,4 @@ public static void listSiteKeys(String projectID) throws IOException {
}
}
}
// [END recaptcha_enterprise_list_site_keys]
// [END recaptcha_enterprise_list_site_keys]
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,37 @@ public static void updateSiteKey(String projectID, String recaptchaSiteKeyID, St
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {

// Set the name and the new settings for the key.
UpdateKeyRequest updateKeyRequest = UpdateKeyRequest.newBuilder()
.setKey(Key.newBuilder()
.setName(KeyName.of(projectID, recaptchaSiteKeyID).toString())
.setWebSettings(WebKeySettings.newBuilder()
.setAllowAmpTraffic(true)
.addAllowedDomains(domainName).build())
.build())
.build();
UpdateKeyRequest updateKeyRequest =
UpdateKeyRequest.newBuilder()
.setKey(
Key.newBuilder()
.setName(KeyName.of(projectID, recaptchaSiteKeyID).toString())
.setWebSettings(
WebKeySettings.newBuilder()
.setAllowAmpTraffic(true)
.addAllowedDomains(domainName)
.build())
.build())
.build();

client.updateKeyCallable().futureCall(updateKeyRequest).get();

// Check if the key has been updated.
GetKeyRequest getKeyRequest = GetKeyRequest.newBuilder()
.setName(KeyName.of(projectID, recaptchaSiteKeyID).toString()).build();
GetKeyRequest getKeyRequest =
GetKeyRequest.newBuilder()
.setName(KeyName.of(projectID, recaptchaSiteKeyID).toString())
.build();
Key response = client.getKey(getKeyRequest);

// Get the changed property.
boolean allowedAmpTraffic = response.getWebSettings().getAllowAmpTraffic();
if (!allowedAmpTraffic) {
System.out
.println("Error! reCAPTCHA Site key property hasn't been updated. Please try again !");
System.out.println(
"Error! reCAPTCHA Site key property hasn't been updated. Please try again !");
return;
}
System.out.println("reCAPTCHA Site key successfully updated !");
}
}
}
// [END recaptcha_enterprise_update_site_key]
// [END recaptcha_enterprise_update_site_key]
24 changes: 13 additions & 11 deletions samples/snippets/cloud-client/src/test/java/app/SnippetsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,14 @@ public class SnippetsIT {
private static String RECAPTCHA_SITE_KEY_1 = "recaptcha-site-key1";
private static String RECAPTCHA_SITE_KEY_2 = "recaptcha-site-key2";
private static WebDriver browser;
@LocalServerPort
private int randomServerPort;
@LocalServerPort private int randomServerPort;
private ByteArrayOutputStream stdOut;


// Check if the required environment variables are set.
public static void requireEnvVar(String envVarName) {
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))
.that(System.getenv(envVarName)).isNotEmpty();
.that(System.getenv(envVarName))
.isNotEmpty();
}

@BeforeClass
Expand Down Expand Up @@ -148,16 +147,19 @@ public void testDeleteSiteKey()
public void testCreateAssessment() throws IOException, JSONException, InterruptedException {
// Construct the URL to call for validating the assessment.
String assessURL = "http://localhost:" + randomServerPort + "/";
URI url = UriComponentsBuilder.fromUriString(assessURL)
.queryParam("recaptchaSiteKey", RECAPTCHA_SITE_KEY_1)
.build().encode().toUri();
URI url =
UriComponentsBuilder.fromUriString(assessURL)
.queryParam("recaptchaSiteKey", RECAPTCHA_SITE_KEY_1)
.build()
.encode()
.toUri();

browser.get(url.toURL().toString());

// Wait until the page is loaded.
JavascriptExecutor js = (JavascriptExecutor) browser;
new WebDriverWait(browser, 10).until(
webDriver -> js.executeScript("return document.readyState").equals("complete"));
new WebDriverWait(browser, 10)
.until(webDriver -> js.executeScript("return document.readyState").equals("complete"));

// Pass the values to be entered.
browser.findElement(By.id("username")).sendKeys("username");
Expand Down Expand Up @@ -186,8 +188,8 @@ public float assessToken(String token, String action) throws IOException {
recaptcha.CreateAssessment.createAssessment(PROJECT_ID, RECAPTCHA_SITE_KEY_1, token, action);
String response = stdOut.toString();
assertThat(response).contains("The reCAPTCHA score is: ");
float recaptchaScore = Float
.parseFloat(response.substring(response.lastIndexOf(":") + 1).trim());
float recaptchaScore =
Float.parseFloat(response.substring(response.lastIndexOf(":") + 1).trim());
return recaptchaScore;
}
}

0 comments on commit 52388a9

Please sign in to comment.