@Test void testValidateUnderThreshold() throws IOException { mockVerificationSetup(0.5, 0.4); String token = "test-token"; assertFalse(reCaptchaService.validate(token)); }
@Timed( value = "recaptcha.validate", description = "Timing information for the reCaptcha validation service.", histogram = true) public boolean validate(String token) { HttpPost httpPost = null; boolean isValid = false; try { httpPost = reCaptchaHttpPostFactory.create(appSettings.getRecaptchaVerifyURI()); NameValuePair secret = new BasicNameValuePair("secret", appSettings.getRecaptchaPrivateKey()); NameValuePair responseToken = new BasicNameValuePair("response", token); httpPost.setEntity(new UrlEncodedFormEntity(Arrays.asList(secret, responseToken))); } catch (UnsupportedEncodingException err) { LOG.error(LOG_MESSAGE, err); } try (CloseableHttpResponse response = httpClient.execute(httpPost)) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode >= HttpStatus.SC_OK && statusCode < HttpStatus.SC_MULTIPLE_CHOICES) { String result = EntityUtils.toString(response.getEntity()); Gson gson = new Gson(); JsonParser parser = new JsonParser(); JsonObject object = (JsonObject) parser.parse(result); ReCaptchaValidationResponse validationResult = gson.fromJson(object, ReCaptchaValidationResponse.class); isValid = validationResult.isSuccess() && validationResult.getScore() > appSettings.getRecaptchaBotThreshold(); } } catch (NumberFormatException | IOException err) { LOG.error(LOG_MESSAGE, err); } return isValid; }
ReCaptchaService { @Timed( value = "recaptcha.validate", description = "Timing information for the reCaptcha validation service.", histogram = true) public boolean validate(String token) { HttpPost httpPost = null; boolean isValid = false; try { httpPost = reCaptchaHttpPostFactory.create(appSettings.getRecaptchaVerifyURI()); NameValuePair secret = new BasicNameValuePair("secret", appSettings.getRecaptchaPrivateKey()); NameValuePair responseToken = new BasicNameValuePair("response", token); httpPost.setEntity(new UrlEncodedFormEntity(Arrays.asList(secret, responseToken))); } catch (UnsupportedEncodingException err) { LOG.error(LOG_MESSAGE, err); } try (CloseableHttpResponse response = httpClient.execute(httpPost)) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode >= HttpStatus.SC_OK && statusCode < HttpStatus.SC_MULTIPLE_CHOICES) { String result = EntityUtils.toString(response.getEntity()); Gson gson = new Gson(); JsonParser parser = new JsonParser(); JsonObject object = (JsonObject) parser.parse(result); ReCaptchaValidationResponse validationResult = gson.fromJson(object, ReCaptchaValidationResponse.class); isValid = validationResult.isSuccess() && validationResult.getScore() > appSettings.getRecaptchaBotThreshold(); } } catch (NumberFormatException | IOException err) { LOG.error(LOG_MESSAGE, err); } return isValid; } }
ReCaptchaService { @Timed( value = "recaptcha.validate", description = "Timing information for the reCaptcha validation service.", histogram = true) public boolean validate(String token) { HttpPost httpPost = null; boolean isValid = false; try { httpPost = reCaptchaHttpPostFactory.create(appSettings.getRecaptchaVerifyURI()); NameValuePair secret = new BasicNameValuePair("secret", appSettings.getRecaptchaPrivateKey()); NameValuePair responseToken = new BasicNameValuePair("response", token); httpPost.setEntity(new UrlEncodedFormEntity(Arrays.asList(secret, responseToken))); } catch (UnsupportedEncodingException err) { LOG.error(LOG_MESSAGE, err); } try (CloseableHttpResponse response = httpClient.execute(httpPost)) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode >= HttpStatus.SC_OK && statusCode < HttpStatus.SC_MULTIPLE_CHOICES) { String result = EntityUtils.toString(response.getEntity()); Gson gson = new Gson(); JsonParser parser = new JsonParser(); JsonObject object = (JsonObject) parser.parse(result); ReCaptchaValidationResponse validationResult = gson.fromJson(object, ReCaptchaValidationResponse.class); isValid = validationResult.isSuccess() && validationResult.getScore() > appSettings.getRecaptchaBotThreshold(); } } catch (NumberFormatException | IOException err) { LOG.error(LOG_MESSAGE, err); } return isValid; } ReCaptchaService(
      CloseableHttpClient httpClient,
      AppSettings appSettings,
      ReCaptchaHttpPostFactory reCaptchaHttpPostFactory); }
ReCaptchaService { @Timed( value = "recaptcha.validate", description = "Timing information for the reCaptcha validation service.", histogram = true) public boolean validate(String token) { HttpPost httpPost = null; boolean isValid = false; try { httpPost = reCaptchaHttpPostFactory.create(appSettings.getRecaptchaVerifyURI()); NameValuePair secret = new BasicNameValuePair("secret", appSettings.getRecaptchaPrivateKey()); NameValuePair responseToken = new BasicNameValuePair("response", token); httpPost.setEntity(new UrlEncodedFormEntity(Arrays.asList(secret, responseToken))); } catch (UnsupportedEncodingException err) { LOG.error(LOG_MESSAGE, err); } try (CloseableHttpResponse response = httpClient.execute(httpPost)) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode >= HttpStatus.SC_OK && statusCode < HttpStatus.SC_MULTIPLE_CHOICES) { String result = EntityUtils.toString(response.getEntity()); Gson gson = new Gson(); JsonParser parser = new JsonParser(); JsonObject object = (JsonObject) parser.parse(result); ReCaptchaValidationResponse validationResult = gson.fromJson(object, ReCaptchaValidationResponse.class); isValid = validationResult.isSuccess() && validationResult.getScore() > appSettings.getRecaptchaBotThreshold(); } } catch (NumberFormatException | IOException err) { LOG.error(LOG_MESSAGE, err); } return isValid; } ReCaptchaService(
      CloseableHttpClient httpClient,
      AppSettings appSettings,
      ReCaptchaHttpPostFactory reCaptchaHttpPostFactory); @Timed( value = "recaptcha.validate", description = "Timing information for the reCaptcha validation service.", histogram = true) boolean validate(String token); }
ReCaptchaService { @Timed( value = "recaptcha.validate", description = "Timing information for the reCaptcha validation service.", histogram = true) public boolean validate(String token) { HttpPost httpPost = null; boolean isValid = false; try { httpPost = reCaptchaHttpPostFactory.create(appSettings.getRecaptchaVerifyURI()); NameValuePair secret = new BasicNameValuePair("secret", appSettings.getRecaptchaPrivateKey()); NameValuePair responseToken = new BasicNameValuePair("response", token); httpPost.setEntity(new UrlEncodedFormEntity(Arrays.asList(secret, responseToken))); } catch (UnsupportedEncodingException err) { LOG.error(LOG_MESSAGE, err); } try (CloseableHttpResponse response = httpClient.execute(httpPost)) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode >= HttpStatus.SC_OK && statusCode < HttpStatus.SC_MULTIPLE_CHOICES) { String result = EntityUtils.toString(response.getEntity()); Gson gson = new Gson(); JsonParser parser = new JsonParser(); JsonObject object = (JsonObject) parser.parse(result); ReCaptchaValidationResponse validationResult = gson.fromJson(object, ReCaptchaValidationResponse.class); isValid = validationResult.isSuccess() && validationResult.getScore() > appSettings.getRecaptchaBotThreshold(); } } catch (NumberFormatException | IOException err) { LOG.error(LOG_MESSAGE, err); } return isValid; } ReCaptchaService(
      CloseableHttpClient httpClient,
      AppSettings appSettings,
      ReCaptchaHttpPostFactory reCaptchaHttpPostFactory); @Timed( value = "recaptcha.validate", description = "Timing information for the reCaptcha validation service.", histogram = true) boolean validate(String token);  }