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);
}
}
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);
}
}
}
}
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 !");
}
}
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);
}
}
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));
}
}
Aggregations