Search in sources :

Example 1 with RecaptchaEnterpriseServiceClient

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

the class GetMetrics method getMetrics.

/**
 * Get metrics specific to a recaptcha site key. E.g: score bucket count for a key or number of
 * times the checkbox key failed/ passed etc.,
 *
 * @param projectId: Google Cloud Project Id.
 * @param recaptchaSiteKey: Specify the site key to get metrics.
 */
public static void getMetrics(String projectId, String recaptchaSiteKey) throws IOException {
    // clean up any remaining background resources.
    try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
        GetMetricsRequest getMetricsRequest = GetMetricsRequest.newBuilder().setName(MetricsName.of(projectId, recaptchaSiteKey).toString()).build();
        Metrics response = client.getMetrics(getMetricsRequest);
        // response.getScoreMetricsList()
        for (ScoreMetrics scoreMetrics : response.getScoreMetricsList()) {
            // Each ScoreMetrics is in the granularity of one day.
            int scoreBucketCount = scoreMetrics.getOverallMetrics().getScoreBucketsCount();
            System.out.println(scoreBucketCount);
        }
        System.out.printf("Retrieved the bucket count for score based key: %s", recaptchaSiteKey);
    }
}
Also used : Metrics(com.google.recaptchaenterprise.v1.Metrics) ScoreMetrics(com.google.recaptchaenterprise.v1.ScoreMetrics) ScoreMetrics(com.google.recaptchaenterprise.v1.ScoreMetrics) GetMetricsRequest(com.google.recaptchaenterprise.v1.GetMetricsRequest) RecaptchaEnterpriseServiceClient(com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)

Example 2 with RecaptchaEnterpriseServiceClient

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

the class MigrateKey method migrateKey.

/**
 * Migrate a key from reCAPTCHA (non-Enterprise) to reCAPTCHA Enterprise. If you created the key
 * using Admin console: https://www.google.com/recaptcha/admin/site, then use this API to migrate
 * to reCAPTCHA Enterprise. For more info, see:
 * https://cloud.google.com/recaptcha-enterprise/docs/migrate-recaptcha
 *
 * @param projectId: Google Cloud Project Id.
 * @param recaptchaSiteKey: Specify the site key to migrate.
 */
public static void migrateKey(String projectId, String recaptchaSiteKey) throws IOException {
    // clean up any remaining background resources.
    try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
        // Specify the key name to migrate.
        MigrateKeyRequest migrateKeyRequest = MigrateKeyRequest.newBuilder().setName(KeyName.of(projectId, recaptchaSiteKey).toString()).build();
        Key response = client.migrateKey(migrateKeyRequest);
        // key is present.
        for (Key key : recaptcha.ListSiteKeys.listSiteKeys(projectId).iterateAll()) {
            if (key.equals(response)) {
                System.out.printf("Key migrated successfully: %s", recaptchaSiteKey);
            }
        }
    }
}
Also used : MigrateKeyRequest(com.google.recaptchaenterprise.v1.MigrateKeyRequest) Key(com.google.recaptchaenterprise.v1.Key) RecaptchaEnterpriseServiceClient(com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)

Example 3 with RecaptchaEnterpriseServiceClient

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

the class UpdateSiteKey method updateSiteKey.

/**
 * Update the properties of the given site key present under the project id.
 *
 * @param projectID: GCloud Project ID.
 * @param recaptchaSiteKeyID: Specify the site key.
 * @param domainName: Specify the domain name for which the settings should be updated.
 */
public static void updateSiteKey(String projectID, String recaptchaSiteKeyID, String domainName) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // clean up any remaining background resources.
    try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
        // Set the name and the new settings for the key.
        UpdateKeyRequest updateKeyRequest = UpdateKeyRequest.newBuilder().setKey(Key.newBuilder().setDisplayName("any descriptive name for the key").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();
        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 !");
            return;
        }
        System.out.println("reCAPTCHA Site key successfully updated !");
    }
}
Also used : UpdateKeyRequest(com.google.recaptchaenterprise.v1.UpdateKeyRequest) GetKeyRequest(com.google.recaptchaenterprise.v1.GetKeyRequest) Key(com.google.recaptchaenterprise.v1.Key) RecaptchaEnterpriseServiceClient(com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)

Example 4 with RecaptchaEnterpriseServiceClient

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

the class AnnotateAssessment method annotateAssessment.

/**
 * Pre-requisite: Create an assessment before annotating.
 *
 * <p>Annotate an assessment to provide feedback on the correctness of recaptcha prediction.
 *
 * @param projectID: GCloud Project id
 * @param assessmentId: Value of the 'name' field returned from the CreateAssessment call.
 */
public static void annotateAssessment(String projectID, String assessmentId) throws IOException {
    // clean up any remaining background resources.
    try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
        // Build the annotation request.
        // For more info on when/how to annotate, see:
        // https://cloud.google.com/recaptcha-enterprise/docs/annotate-assessment#when_to_annotate
        AnnotateAssessmentRequest annotateAssessmentRequest = AnnotateAssessmentRequest.newBuilder().setName(AssessmentName.of(projectID, assessmentId).toString()).setAnnotation(Annotation.FRAUDULENT).addReasons(Reason.FAILED_TWO_FACTOR).build();
        // Empty response is sent back.
        AnnotateAssessmentResponse response = client.annotateAssessment(annotateAssessmentRequest);
        System.out.println("Annotated response sent successfully ! " + response);
    }
}
Also used : AnnotateAssessmentRequest(com.google.recaptchaenterprise.v1.AnnotateAssessmentRequest) AnnotateAssessmentResponse(com.google.recaptchaenterprise.v1.AnnotateAssessmentResponse) RecaptchaEnterpriseServiceClient(com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)

Example 5 with RecaptchaEnterpriseServiceClient

use of com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient 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)

Aggregations

RecaptchaEnterpriseServiceClient (com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)9 Key (com.google.recaptchaenterprise.v1.Key)5 GetKeyRequest (com.google.recaptchaenterprise.v1.GetKeyRequest)2 ListKeysPagedResponse (com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient.ListKeysPagedResponse)1 AnnotateAssessmentRequest (com.google.recaptchaenterprise.v1.AnnotateAssessmentRequest)1 AnnotateAssessmentResponse (com.google.recaptchaenterprise.v1.AnnotateAssessmentResponse)1 Assessment (com.google.recaptchaenterprise.v1.Assessment)1 CreateAssessmentRequest (com.google.recaptchaenterprise.v1.CreateAssessmentRequest)1 CreateKeyRequest (com.google.recaptchaenterprise.v1.CreateKeyRequest)1 DeleteKeyRequest (com.google.recaptchaenterprise.v1.DeleteKeyRequest)1 Event (com.google.recaptchaenterprise.v1.Event)1 GetMetricsRequest (com.google.recaptchaenterprise.v1.GetMetricsRequest)1 ListKeysRequest (com.google.recaptchaenterprise.v1.ListKeysRequest)1 Metrics (com.google.recaptchaenterprise.v1.Metrics)1 MigrateKeyRequest (com.google.recaptchaenterprise.v1.MigrateKeyRequest)1 ClassificationReason (com.google.recaptchaenterprise.v1.RiskAnalysis.ClassificationReason)1 ScoreMetrics (com.google.recaptchaenterprise.v1.ScoreMetrics)1 UpdateKeyRequest (com.google.recaptchaenterprise.v1.UpdateKeyRequest)1