-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor token validation into service class
- Loading branch information
1 parent
4b0a894
commit 1b73f94
Showing
5 changed files
with
297 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
module/VuFind/src/VuFind/Controller/TurnstileControllerFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
/** | ||
* Turnstile controller factory. | ||
* | ||
* PHP version 8 | ||
* | ||
* Copyright (C) Villanova University 2024. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 2, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* @category VuFind | ||
* @package Controller | ||
* @author Maccabee Levine <[email protected]> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org/wiki/development Wiki | ||
*/ | ||
|
||
namespace VuFind\Controller; | ||
|
||
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; | ||
use Laminas\ServiceManager\Exception\ServiceNotFoundException; | ||
use Psr\Container\ContainerExceptionInterface as ContainerException; | ||
use Psr\Container\ContainerInterface; | ||
use VuFind\Service\GetServiceTrait; | ||
|
||
/** | ||
* Turnstile controller factory. | ||
* | ||
* @category VuFind | ||
* @package Controller | ||
* @author Maccabee Levine <[email protected]> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org/wiki/development Wiki | ||
*/ | ||
class TurnstileControllerFactory extends AbstractBaseFactory | ||
{ | ||
use GetServiceTrait; | ||
|
||
/** | ||
* Create an object | ||
* | ||
* @param ContainerInterface $container Service manager | ||
* @param string $requestedName Service being created | ||
* @param null|array $options Extra options (optional) | ||
* | ||
* @return object | ||
* | ||
* @throws ServiceNotFoundException if unable to resolve the service. | ||
* @throws ServiceNotCreatedException if an exception is raised when | ||
* creating a service. | ||
* @throws ContainerException&\Throwable if any other error occurs | ||
*/ | ||
public function __invoke( | ||
ContainerInterface $container, | ||
$requestedName, | ||
array $options = null | ||
) { | ||
if (!empty($options)) { | ||
throw new \Exception('Unexpected options sent to factory.'); | ||
} | ||
$this->serviceLocator = $container; | ||
|
||
$yamlReader = $container->get(\VuFind\Config\YamlReader::class); | ||
$config = $yamlReader->get('RateLimiter.yaml'); | ||
|
||
return new $requestedName( | ||
$container, | ||
$container->get(\VuFind\RateLimiter\Turnstile\Turnstile::class), | ||
$container->get(\VuFind\RateLimiter\RateLimiterManager::class), | ||
$config, | ||
$container->get(\VuFind\Crypt\HMAC::class), | ||
); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
module/VuFind/src/VuFind/RateLimiter/Turnstile/Turnstile.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/** | ||
* Cloudflare Turnstile service. | ||
* | ||
* PHP version 8 | ||
* | ||
* Copyright (C) Villanova University 2024. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 2, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* @category VuFind | ||
* @package RateLimiter | ||
* @author Maccabee Levine <[email protected]> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org Main Page | ||
*/ | ||
|
||
namespace VuFind\RateLimiter\Turnstile; | ||
|
||
use Laminas\Log\LoggerAwareInterface; | ||
use VuFind\Log\LoggerAwareTrait; | ||
use VuFind\RateLimiter\RateLimiterManager; | ||
use VuFindHttp\HttpServiceAwareInterface; | ||
use VuFindHttp\HttpServiceAwareTrait; | ||
|
||
/** | ||
* Rate limiter manager. | ||
* | ||
* @category VuFind | ||
* @package Cache | ||
* @author Maccabee Levine <[email protected]> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org Main Page | ||
*/ | ||
class Turnstile implements HttpServiceAwareInterface, LoggerAwareInterface | ||
{ | ||
use HttpServiceAwareTrait; | ||
use LoggerAwareTrait; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param array $config Rate Limiter config | ||
* @param RateLimiterManager $rateLimiterManager Rate limiter manager | ||
*/ | ||
public function __construct( | ||
protected array $config, | ||
protected RateLimiterManager $rateLimiterManager | ||
) { | ||
} | ||
|
||
/** | ||
* Validate a token against the Turnstile API | ||
* | ||
* @param string $token The token generated by the Turnstile widget | ||
* | ||
* @return bool Whether validation was successful | ||
*/ | ||
public function validateToken($token) | ||
{ | ||
// Call the Turnstile verify API to validate the token | ||
$secretKey = $this->config['Turnstile']['secretKey']; | ||
$url = $this->config['Turnstile']['verifyUrl'] ?? | ||
'https://challenges.cloudflare.com/turnstile/v0/siteverify'; | ||
$body = [ | ||
'secret' => $secretKey, | ||
'response' => $token, | ||
]; | ||
$response = $this->httpService->post( | ||
$url, | ||
json_encode($body), | ||
'application/json' | ||
); | ||
|
||
if ($response->isOk()) { | ||
$responseData = json_decode($response->getBody(), true); | ||
$success = $responseData['success']; | ||
} else { | ||
// Unexpected error. Treat as a positive result, since it's not the user's fault. | ||
$this->logWarning('Verification process failed, allowing traffic: ' | ||
. $response->getStatusCode() . $response->getBody()); | ||
$success = true; | ||
} | ||
|
||
return $success; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
module/VuFind/src/VuFind/RateLimiter/Turnstile/TurnstileFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/** | ||
* Turnstile service factory. | ||
* | ||
* PHP version 8 | ||
* | ||
* Copyright (C) Villanova University 2024. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 2, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* @category VuFind | ||
* @package Service | ||
* @author Maccabee Levine <[email protected]> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org/wiki/development Wiki | ||
*/ | ||
|
||
namespace VuFind\RateLimiter\Turnstile; | ||
|
||
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; | ||
use Laminas\ServiceManager\Exception\ServiceNotFoundException; | ||
use Laminas\ServiceManager\Factory\FactoryInterface; | ||
use Psr\Container\ContainerExceptionInterface as ContainerException; | ||
use Psr\Container\ContainerInterface; | ||
|
||
/** | ||
* Turnstile service factory. | ||
* | ||
* @category VuFind | ||
* @package RateLimiter | ||
* @author Maccabee Levine <[email protected]> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org/wiki/development Wiki | ||
*/ | ||
class TurnstileFactory implements FactoryInterface | ||
{ | ||
/** | ||
* Create an object | ||
* | ||
* @param ContainerInterface $container Service manager | ||
* @param string $requestedName Service being created | ||
* @param null|array $options Extra options (optional) | ||
* | ||
* @return object | ||
* | ||
* @throws ServiceNotFoundException if unable to resolve the service. | ||
* @throws ServiceNotCreatedException if an exception is raised when | ||
* creating a service. | ||
* @throws ContainerException&\Throwable if any other error occurs | ||
*/ | ||
public function __invoke( | ||
ContainerInterface $container, | ||
$requestedName, | ||
array $options = null | ||
) { | ||
if (!empty($options)) { | ||
throw new \Exception('Unexpected options sent to factory.'); | ||
} | ||
|
||
$yamlReader = $container->get(\VuFind\Config\YamlReader::class); | ||
$config = $yamlReader->get('RateLimiter.yaml'); | ||
|
||
return new $requestedName( | ||
$config, | ||
$container->get(\VuFind\RateLimiter\RateLimiterManager::class), | ||
); | ||
} | ||
} |