Skip to content

Commit

Permalink
update requirements for ALTCHA integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Godoy0722 authored and asmecher committed Jan 11, 2025
1 parent 96434bd commit 9279145
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 56 deletions.
27 changes: 8 additions & 19 deletions classes/form/validation/FormValidatorAltcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use AltchaOrg\Altcha\Altcha;
use AltchaOrg\Altcha\ChallengeOptions;
use APP\core\Application;
use APP\template\TemplateManager;
use Exception;
use InvalidArgumentException;
use PKP\config\Config;
Expand All @@ -28,6 +29,7 @@ class FormValidatorAltcha extends FormValidator
{
/** @var string The response field containing the ALTCHA response */
private const ALTCHA_RESPONSE_FIELD = 'altcha';

/** @var string The initiating IP address of the user */
private $_userIp;

Expand All @@ -43,13 +45,9 @@ public function __construct(Form $form, string $userIp, string $message)
$this->_userIp = $userIp;
}

//
// Public methods
//
/**
* @see FormValidator::isValid()
* Determine whether or not the form meets this ALTCHA constraint.
*
*/
public function isValid(): bool
{
Expand All @@ -66,12 +64,12 @@ public function isValid(): bool
/**
* Validates the ALTCHA response
*
* @param string|null $response The ALTCHA response
* @param string|null $ip The user IP address (defaults to null)
* @param $response The ALTCHA response
* @param $ip The user IP address (defaults to null)
*
* @throws Exception Throws in case the validation fails
*/
public static function validateResponse($response, ?string $ip = null): void
public static function validateResponse(?string $response, ?string $ip = null): void
{
if (!empty($ip) && !filter_var($ip, FILTER_VALIDATE_IP)) {
throw new InvalidArgumentException('Invalid IP address.');
Expand All @@ -93,21 +91,16 @@ public static function validateResponse($response, ?string $ip = null): void
}
}

/**
* Add ALTCHA javascript on the journal header
*
* @param TemplateManager $templateMgr
*/
public static function addAltchaJavascript(&$templateMgr): void
public static function addAltchaJavascript(TemplateManager &$templateMgr): void
{
$request = Application::get()->getRequest();
$altchaPath = $request->getBaseUrl() . '/lib/pkp/js/lib/altcha/altcha.min.js';
$altchaPath = $request->getBaseUrl() . '/node_modules/altcha/dist/altcha.js';

$altchaHeader = '<script async defer src="' . $altchaPath . '" type="module"></script>';
$templateMgr->addHeader('altcha', $altchaHeader);
}

public static function insertFormChallenge(&$templateMgr): void
public static function insertFormChallenge(TemplateManager &$templateMgr): void
{
$options = new ChallengeOptions([
'hmacKey' => Config::getVar('captcha', 'altcha_hmackey'),
Expand All @@ -120,7 +113,3 @@ public static function insertFormChallenge(&$templateMgr): void
$templateMgr->assign('altchaChallenge', $challenge);
}
}

if (!PKP_STRICT_MODE) {
class_alias('\PKP\form\validation\FormValidatorAltcha', '\FormValidatorAltcha');
}
7 changes: 1 addition & 6 deletions classes/user/form/RegistrationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ class RegistrationForm extends Form
/** @var bool whether or not captcha is enabled for this form */
public $altchaEnabled;

/**
* Constructor.
*
* @param Site $site
*/
public function __construct($site)
public function __construct(Site $site)
{
parent::__construct('frontend/pages/userRegister.tpl');

Expand Down
8 changes: 0 additions & 8 deletions js/lib/altcha/altcha.min.js

This file was deleted.

35 changes: 12 additions & 23 deletions pages/login/LoginHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
use PKP\site\Site;
use PKP\user\form\LoginChangePasswordForm;
use PKP\user\form\ResetPasswordForm;
use PKP\user\User;

class LoginHandler extends Handler
{
Expand Down Expand Up @@ -89,7 +88,7 @@ public function index($args, $request)
$templateMgr->assign('recaptchaPublicKey', Config::getVar('captcha', 'recaptcha_public_key'));
}

$this->generateAltchaComponent('altcha_on_login', $templateMgr);
$this->_generateAltchaComponent('altcha_on_login', $templateMgr);
$templateMgr->display('frontend/pages/userLogin.tpl');
}

Expand Down Expand Up @@ -117,7 +116,7 @@ public function _redirectAfterLogin($request)
/**
* Validate a user's credentials and log the user in.
*/
public function signIn($args, $request)
public function signIn(array $args, PKPRequest $request): void
{
$this->setupTemplate($request);
$templateMgr = TemplateManager::getManager($request);
Expand All @@ -141,7 +140,7 @@ public function signIn($args, $request)
}
}

$error = $this->validateAltchaResponse($request, 'altcha_on_login');
$error = $this->_validateAltchasResponse($request, 'altcha_on_login');
$username = $request->getUserVar('username');
$reason = null;
$user = $error || !strlen($username ?? '')
Expand Down Expand Up @@ -180,7 +179,8 @@ public function signIn($args, $request)
'error' => $error,
'reason' => $reason,
]);
$this->generateAltchaComponent('altcha_on_login', $templateMgr);

$this->_generateAltchaComponent('altcha_on_login', $templateMgr);
$templateMgr->display('frontend/pages/userLogin.tpl');
}

Expand Down Expand Up @@ -213,7 +213,8 @@ public function lostPassword($args, $request)

$this->setupTemplate($request);
$templateMgr = TemplateManager::getManager($request);
$this->generateAltchaComponent('altcha_on_lost_password', $templateMgr);

$this->_generateAltchaComponent('altcha_on_lost_password', $templateMgr);
$templateMgr->display('frontend/pages/userLostPassword.tpl');
}

Expand All @@ -225,10 +226,10 @@ public function requestResetPassword($args, $request)
$this->setupTemplate($request);
$templateMgr = TemplateManager::getManager($request);

$altchaHasError = $this->validateAltchaResponse($request, 'altcha_on_lost_password');
$altchaHasError = $this->_validateAltchasResponse($request, 'altcha_on_lost_password');

if ($altchaHasError) {
$this->generateAltchaComponent('altcha_on_lost_password', $templateMgr);
$this->_generateAltchaComponent('altcha_on_lost_password', $templateMgr);

$templateMgr
->assign([
Expand All @@ -244,7 +245,7 @@ public function requestResetPassword($args, $request)
$user = $email ? Repo::user()->getByEmail($email, true) : null;
if ($user !== null) {
if ($user->getDisabled()) {
$this->generateAltchaComponent('altcha_on_lost_password', $templateMgr);
$this->_generateAltchaComponent('altcha_on_lost_password', $templateMgr);

$templateMgr
->assign([
Expand Down Expand Up @@ -418,9 +419,6 @@ public function savePassword($args, $request)

/**
* Sign in as another user.
*
* @param array $args ($userId)
* @param PKPRequest $request
*/
public function signInAsUser($args, $request)
{
Expand Down Expand Up @@ -455,9 +453,6 @@ public function signInAsUser($args, $request)

/**
* Restore original user account after signing in as a user.
*
* @param array $args
* @param PKPRequest $request
*/
public function signOutAsUser($args, $request)
{
Expand All @@ -479,8 +474,6 @@ public function signOutAsUser($args, $request)

/**
* Redirect to redirectURL if exists else send to Home
*
* @param PKPRequest $request
*/
public function _redirectByURL($request)
{
Expand All @@ -495,8 +488,6 @@ public function _redirectByURL($request)
/**
* Send the user "home" (typically to the dashboard, but that may not
* always be available).
*
* @param PKPRequest $request
*/
protected function sendHome($request)
{
Expand All @@ -507,10 +498,9 @@ protected function sendHome($request)
/**
* Validate if ALTCHA user's response is valid
*
* @param PKPRequest $request
* @param string $altchaConfigKey the key to search on config.inc.php
*/
private function validateAltchaResponse($request, $altchaConfigKey): ?string
private function _validateAltchasResponse($request, $altchaConfigKey): ?string
{
if (Config::getVar('captcha', 'altcha') && Config::getVar('captcha', $altchaConfigKey)) {
try {
Expand All @@ -527,9 +517,8 @@ private function validateAltchaResponse($request, $altchaConfigKey): ?string
* is enabled on the specific page
*
* @param string $altchaConfigKey the key to search on config.inc.php
* @param TemplateManager $templateMgr
*/
private function generateAltchaComponent($altchaConfigKey, &$templateMgr): void
private function _generateAltchaComponent($altchaConfigKey, &$templateMgr): void
{
if (Config::getVar('captcha', 'altcha') && Config::getVar('captcha', $altchaConfigKey)) {
FormValidatorAltcha::addAltchaJavascript($templateMgr);
Expand Down

0 comments on commit 9279145

Please sign in to comment.