use of com.google.recaptchaenterprise.v1beta1.Assessment in project pomocua-ogloszenia by coi-gov-pl.
the class CaptchaValidator method validate.
public boolean validate(String recaptchaResponse) {
if (!properties.isEnabled()) {
log.debug("Skip captcha validation. To enable change 'app.captcha.enabled' property");
return true;
}
if (!responseSanityCheck(recaptchaResponse)) {
log.warn("Response contains invalid characters");
return false;
}
ProjectName projectName = ProjectName.of(properties.getGoogleCloudProjectId());
Event event = Event.newBuilder().setSiteKey(properties.getSiteKey()).setToken(recaptchaResponse).build();
CreateAssessmentRequest createAssessmentRequest = CreateAssessmentRequest.newBuilder().setParent(projectName.toString()).setAssessment(Assessment.newBuilder().setEvent(event).build()).build();
Assessment response = recaptchaClient.createAssessment(createAssessmentRequest);
if (response == null) {
log.warn("Empty response from reCaptcha");
return false;
}
// Check if the token is valid.
if (!response.getTokenProperties().getValid()) {
String invalidTokenReason = response.getTokenProperties().getInvalidReason().name();
log.debug("The CreateAssessment call failed because the token was: " + invalidTokenReason);
return false;
}
float score = response.getScore();
if (score < properties.getAcceptLevel()) {
List<String> reasons = response.getReasonsList().stream().map(classificationReason -> classificationReason.getDescriptorForType().getFullName()).collect(Collectors.toList());
log.debug("Validation failed. Score: " + score + ". Reasons: " + String.join(", ", reasons));
return false;
}
log.debug("Validation OK - score: " + score);
return true;
}
use of com.google.recaptchaenterprise.v1beta1.Assessment in project pomocua-ogloszenia by coi-gov-pl.
the class CaptchaValidatorTest method validate_whenCaptchaStatusIsSuccessAndScoreNotAccepted_expectFalse.
@Test
void validate_whenCaptchaStatusIsSuccessAndScoreNotAccepted_expectFalse() {
// given
String recaptcha = "anytext";
Assessment response = createAssessmentResponse(true, 0.3F);
when(recaptchaClient.createAssessment(any(CreateAssessmentRequest.class))).thenReturn(response);
// when
boolean result = captchaValidator.validate(recaptcha);
// then
assertAll(() -> assertThat(result).isFalse(), () -> verify(recaptchaClient, times(1)).createAssessment(any(CreateAssessmentRequest.class)));
}
use of com.google.recaptchaenterprise.v1beta1.Assessment in project pomocua-ogloszenia by coi-gov-pl.
the class CaptchaValidatorTest method validate_whenCaptchaStatusIsSuccessAndScoreAccepted_expectTrue.
@Test
void validate_whenCaptchaStatusIsSuccessAndScoreAccepted_expectTrue() {
// given
String recaptcha = "anytext";
Assessment response = createAssessmentResponse(true, 111);
when(recaptchaClient.createAssessment(any(CreateAssessmentRequest.class))).thenReturn(response);
// when
boolean result = captchaValidator.validate(recaptcha);
// then
assertAll(() -> assertThat(result).isTrue(), () -> verify(recaptchaClient, times(1)).createAssessment(any(CreateAssessmentRequest.class)));
}
use of com.google.recaptchaenterprise.v1beta1.Assessment in project pomocua-ogloszenia by coi-gov-pl.
the class CaptchaValidatorTest method validate_whenCaptchaStatusNotSuccess_expectFalse.
@Test
void validate_whenCaptchaStatusNotSuccess_expectFalse() {
// given
String recaptcha = "anytext";
Assessment response = createAssessmentResponse(false, 0);
when(recaptchaClient.createAssessment(any(CreateAssessmentRequest.class))).thenReturn(response);
// when
boolean result = captchaValidator.validate(recaptcha);
// then
assertAll(() -> assertThat(result).isFalse(), () -> verify(recaptchaClient, times(1)).createAssessment(any(CreateAssessmentRequest.class)));
}
use of com.google.recaptchaenterprise.v1beta1.Assessment in project java-recaptchaenterprise by googleapis.
the class RecaptchaEnterpriseServiceClientTest method createAssessmentTest.
@Test
public void createAssessmentTest() throws Exception {
Assessment expectedResponse = Assessment.newBuilder().setName(AssessmentName.of("[PROJECT]", "[ASSESSMENT]").toString()).setEvent(Event.newBuilder().build()).setRiskAnalysis(RiskAnalysis.newBuilder().build()).setTokenProperties(TokenProperties.newBuilder().build()).setAccountDefenderAssessment(AccountDefenderAssessment.newBuilder().build()).build();
mockRecaptchaEnterpriseService.addResponse(expectedResponse);
ProjectName parent = ProjectName.of("[PROJECT]");
Assessment assessment = Assessment.newBuilder().build();
Assessment actualResponse = client.createAssessment(parent, assessment);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseService.getRequests();
Assert.assertEquals(1, actualRequests.size());
CreateAssessmentRequest actualRequest = ((CreateAssessmentRequest) actualRequests.get(0));
Assert.assertEquals(parent.toString(), actualRequest.getParent());
Assert.assertEquals(assessment, actualRequest.getAssessment());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Aggregations