Search in sources :

Example 31 with Key

use of com.google.recaptchaenterprise.v1.Key in project java-recaptchaenterprise by googleapis.

the class RecaptchaEnterpriseServiceClientTest method getKeyExceptionTest.

@Test
public void getKeyExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockRecaptchaEnterpriseService.addException(exception);
    try {
        GetKeyRequest request = GetKeyRequest.newBuilder().setName(KeyName.of("[PROJECT]", "[KEY]").toString()).build();
        client.getKey(request);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : GetKeyRequest(com.google.recaptchaenterprise.v1.GetKeyRequest) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 32 with Key

use of com.google.recaptchaenterprise.v1.Key in project java-recaptchaenterprise by googleapis.

the class RecaptchaEnterpriseServiceV1Beta1ClientTest method getKeyTest.

@Test
public void getKeyTest() throws Exception {
    Key expectedResponse = Key.newBuilder().setName(KeyName.of("[PROJECT]", "[KEY]").toString()).setDisplayName("displayName1714148973").build();
    mockRecaptchaEnterpriseServiceV1Beta1.addResponse(expectedResponse);
    GetKeyRequest request = GetKeyRequest.newBuilder().setName(KeyName.of("[PROJECT]", "[KEY]").toString()).build();
    Key actualResponse = client.getKey(request);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseServiceV1Beta1.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    GetKeyRequest actualRequest = ((GetKeyRequest) actualRequests.get(0));
    Assert.assertEquals(request.getName(), actualRequest.getName());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : GetKeyRequest(com.google.recaptchaenterprise.v1beta1.GetKeyRequest) AbstractMessage(com.google.protobuf.AbstractMessage) Key(com.google.recaptchaenterprise.v1beta1.Key) Test(org.junit.Test)

Example 33 with Key

use of com.google.recaptchaenterprise.v1.Key in project java-recaptchaenterprise by googleapis.

the class RecaptchaEnterpriseServiceV1Beta1ClientTest method listKeysTest.

@Test
public void listKeysTest() throws Exception {
    Key responsesElement = Key.newBuilder().build();
    ListKeysResponse expectedResponse = ListKeysResponse.newBuilder().setNextPageToken("").addAllKeys(Arrays.asList(responsesElement)).build();
    mockRecaptchaEnterpriseServiceV1Beta1.addResponse(expectedResponse);
    ListKeysRequest request = ListKeysRequest.newBuilder().setParent(ProjectName.of("[PROJECT]").toString()).setPageSize(883849137).setPageToken("pageToken873572522").build();
    ListKeysPagedResponse pagedListResponse = client.listKeys(request);
    List<Key> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getKeysList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseServiceV1Beta1.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListKeysRequest actualRequest = ((ListKeysRequest) actualRequests.get(0));
    Assert.assertEquals(request.getParent(), actualRequest.getParent());
    Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize());
    Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListKeysPagedResponse(com.google.cloud.recaptchaenterprise.v1beta1.RecaptchaEnterpriseServiceV1Beta1Client.ListKeysPagedResponse) ListKeysRequest(com.google.recaptchaenterprise.v1beta1.ListKeysRequest) ListKeysResponse(com.google.recaptchaenterprise.v1beta1.ListKeysResponse) Key(com.google.recaptchaenterprise.v1beta1.Key) Test(org.junit.Test)

Example 34 with Key

use of com.google.recaptchaenterprise.v1.Key in project java-recaptchaenterprise by googleapis.

the class CreateAssessment method createAssessment.

/**
 * Create an assessment to analyze the risk of an UI action. Assessment approach is the same for
 * both 'score' and 'checkbox' type recaptcha site keys.
 *
 * @param projectID : GCloud 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 recaptchaAction : Action name corresponding to the token.
 */
public static void createAssessment(String projectID, String recaptchaSiteKey, String token, String recaptchaAction) throws IOException {
    // clean up any remaining background resources.
    try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
        // Set the properties of the event to be tracked.
        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).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());
            return;
        }
        // (If the key is checkbox type and 'action' attribute wasn't set, skip this check.)
        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");
            return;
        }
        // see: https://cloud.google.com/recaptcha-enterprise/docs/interpret-assessment
        for (ClassificationReason reason : response.getRiskAnalysis().getReasonsList()) {
            System.out.println(reason);
        }
        float recaptchaScore = response.getRiskAnalysis().getScore();
        System.out.println("The reCAPTCHA score is: " + recaptchaScore);
        // Get the assessment name (id). Use this to annotate the assessment.
        String assessmentName = response.getName();
        System.out.println("Assessment name: " + assessmentName.substring(assessmentName.lastIndexOf("/") + 1));
    }
}
Also used : Assessment(com.google.recaptchaenterprise.v1.Assessment) CreateAssessmentRequest(com.google.recaptchaenterprise.v1.CreateAssessmentRequest) Event(com.google.recaptchaenterprise.v1.Event) ClassificationReason(com.google.recaptchaenterprise.v1.RiskAnalysis.ClassificationReason) RecaptchaEnterpriseServiceClient(com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)

Example 35 with Key

use of com.google.recaptchaenterprise.v1.Key in project java-recaptchaenterprise by googleapis.

the class CreateSiteKey method createSiteKey.

/**
 * Create reCAPTCHA Site key which binds a domain name to a unique key.
 *
 * @param projectID : GCloud Project ID.
 * @param domainName : Specify the domain name in which the reCAPTCHA should be activated.
 */
public static String createSiteKey(String projectID, String domainName) throws IOException {
    // clean up any remaining background resources.
    try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
        // 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();
        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);
        String keyName = response.getName();
        String recaptchaSiteKey = keyName.substring(keyName.lastIndexOf("/") + 1);
        System.out.println("reCAPTCHA Site key created successfully. Site Key: " + recaptchaSiteKey);
        return recaptchaSiteKey;
    }
}
Also used : CreateKeyRequest(com.google.recaptchaenterprise.v1.CreateKeyRequest) Key(com.google.recaptchaenterprise.v1.Key) RecaptchaEnterpriseServiceClient(com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)

Aggregations

Test (org.junit.Test)27 Key (com.google.datastore.v1.Key)17 AbstractMessage (com.google.protobuf.AbstractMessage)12 DatastoreHelper.makeKey (com.google.datastore.v1.client.DatastoreHelper.makeKey)11 Key (com.google.recaptchaenterprise.v1.Key)10 RecaptchaEnterpriseServiceClient (com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)8 Entity (com.google.datastore.v1.Entity)7 DeleteKey (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteKey)7 DatastoreV1.isValidKey (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.isValidKey)7 Mutation (com.google.datastore.v1.Mutation)5 ArrayList (java.util.ArrayList)5 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)4 GetKeyRequest (com.google.recaptchaenterprise.v1.GetKeyRequest)4 Key (com.google.recaptchaenterprise.v1beta1.Key)4 StatusRuntimeException (io.grpc.StatusRuntimeException)4 HashMap (java.util.HashMap)4 Query (com.google.datastore.v1.Query)3 DeleteKeyRequest (com.google.recaptchaenterprise.v1.DeleteKeyRequest)3 GetMetricsRequest (com.google.recaptchaenterprise.v1.GetMetricsRequest)3 Metrics (com.google.recaptchaenterprise.v1.Metrics)3