Skip to content

Commit 784300f

Browse files
authored
Merge branch 'develop' into WS-507-HHS-webform
2 parents 5fcfee3 + 94bde06 commit 784300f

File tree

9 files changed

+133
-6
lines changed

9 files changed

+133
-6
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"drupal/quickedit": "^1.0",
9090
"drupal/rdf": "^2.0",
9191
"drupal/recaptcha": "^3.4",
92+
"drupal/recaptcha_v3": "^2.0",
9293
"drupal/restui": "^1.21.0",
9394
"drupal/roleassign": "^2.0.0",
9495
"drupal/rules": "^3.0@alpha",

composer.lock

Lines changed: 55 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/default/captcha.settings.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ _core:
22
default_config_hash: QDFjOXYIYVwCPQYHY4wAx4DUqOEkNaZokIx6DGApR9I
33
enable_globally: 0
44
enable_globally_on_admin_routes: false
5-
default_challenge: recaptcha/reCAPTCHA
5+
default_challenge: recaptcha_v3/submit
66
description: 'This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.'
77
title: CAPTCHA
88
administration_mode: true

config/default/core.extension.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ module:
139139
quickedit: 0
140140
rdf: 0
141141
recaptcha: 0
142+
recaptcha_v3: 0
142143
rest: 0
143144
restui: 0
144145
roleassign: 0

config/default/entity_clone.cloneable_entities.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,4 @@ cloneable_entities:
7171
- mailer_policy
7272
- config_split
7373
- captcha_point
74+
- recaptcha_v3_action

config/default/recaptcha.settings.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
_core:
22
default_config_hash: ByOVf1cU9r5NkZ4ieBJ7k9sUid6jb03ojMN1gjJ0-OU
3-
site_key: 6Le3tV0qAAAAAJB8fxsSPxs3v46Zo69t2IaRFU5C
4-
secret_key: 6Le3tV0qAAAAAB4omG0eYGfM4AaP7pfEuFGD12gE
3+
site_key: ''
4+
secret_key: ''
55
verify_hostname: false
66
use_globally: false
77
widget:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
uuid: 07cf3e20-e94b-4432-ad26-e748d2c078a7
2+
langcode: en
3+
status: true
4+
dependencies: { }
5+
id: submit
6+
label: submit
7+
threshold: 0.5
8+
challenge: recaptcha/reCAPTCHA
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
_core:
2+
default_config_hash: 6xvuojdlZ8-8Q7yJyRldPaE0UK_YijOlDo6HvURmW_E
3+
site_key: 6Le3tV0qAAAAAJB8fxsSPxs3v46Zo69t2IaRFU5C
4+
secret_key: 6Le3tV0qAAAAAB4omG0eYGfM4AaP7pfEuFGD12gE
5+
hide_badge: false
6+
verify_hostname: true
7+
default_challenge: ''
8+
error_message: 'Anti-bot verification failed.'
9+
cacheable: false
10+
library_use_recaptcha_net: true

docroot/modules/custom/foia_api/src/Plugin/rest/resource/WebformSubmissionResource.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public function post($data) {
198198
// Validate recaptcha.
199199
$captcha_errors = [];
200200
$captcha = $values['data']['captcha'];
201-
$captcha_errors = $this->validateCaptcha($captcha);
201+
$captcha_errors = $this->validateCaptchaV3($captcha);
202202

203203
if (!empty($captcha_errors)) {
204204
// The react front end will know that 401 means the captcha failed and
@@ -270,7 +270,7 @@ public function post($data) {
270270
}
271271

272272
/**
273-
* Validates that the submitted reCAPTCHA ( google ) is correct.
273+
* Validates that the submitted reCAPTCHA_v2 ( google ) is correct.
274274
*
275275
* @param string $captcha
276276
* The submitted captcha value.
@@ -364,6 +364,58 @@ protected function validateCaptcha(string $captcha) {
364364
return $errors;
365365
}
366366

367+
/**
368+
* CAPTCHA Callback; Validates the reCAPTCHA v3 code.
369+
*
370+
* Copied and modified from recaptcha_v3.module.
371+
*/
372+
protected function validateCaptchaV3($captcha_response) {
373+
374+
// This is hardwired on the reactjs side.
375+
$captcha_type_challenge = 'submit';
376+
/** @var \Drupal\recaptcha_v3\ReCaptchaV3ActionInterface $recaptcha_v3 */
377+
$recaptcha_v3 = ReCaptchaV3Action::load($captcha_type_challenge) ?? ReCaptchaV3Action::create([
378+
'id' => '',
379+
'label' => '',
380+
'threshold' => 1,
381+
'challenge' => 'default',
382+
]);
383+
// Verify submitted reCAPTCHA v3 token.
384+
$verification_response = _recaptcha_v3_verify_captcha_response($recaptcha_v3, $captcha_response);
385+
386+
if (!$verification_response['success']) {
387+
// If we here, then token verification failed.
388+
if ($verification_response['error-codes']) {
389+
$errors = [];
390+
391+
$challenge = $recaptcha_v3->getChallenge();
392+
if ($challenge === 'default') {
393+
$challenge = \Drupal::config('recaptcha_v3.settings')->get('default_challenge');
394+
}
395+
396+
foreach ($verification_response['error-codes'] as $code) {
397+
// If we have fallback challenge then do not log the threshold errors.
398+
if ($challenge && $code === 'score-threshold-not-met') {
399+
continue;
400+
}
401+
$errors[] = recaptcha_v3_error_by_code($code);
402+
}
403+
404+
if ($errors) {
405+
$errors_string = implode(' ', $errors);
406+
\Drupal::logger('recaptcha_v3')->error(
407+
'Google reCAPTCHA v3 validation failed: @error',
408+
['@error' => $errors_string]
409+
);
410+
}
411+
}
412+
413+
$error_message = \Drupal::config('recaptcha_v3.settings')->get('error_message');
414+
}
415+
416+
return (bool) $verification_response['success'];
417+
}
418+
367419
/**
368420
* Logs a submission with HTTP status code, message, and optional component.
369421
*

0 commit comments

Comments
 (0)