Skip to content

Commit 0bb838f

Browse files
committed
add ReCAPTCHA on the reset password form. Related to the issue pkp#6984
1 parent 4175076 commit 0bb838f

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

classes/template/PKPTemplateManager.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ public function initialize(PKPRequest $request)
244244
if (Config::getVar('captcha', 'captcha_on_login')) {
245245
array_push($contexts, 'frontend-login-index', 'frontend-login-signIn');
246246
}
247+
if (Config::getVar('captcha', 'captcha_on_lost_password')) {
248+
array_push($contexts, 'frontend-login-lostPassword', 'frontend-login-requestResetPassword');
249+
}
247250
if (count($contexts)) {
248251
// These are the supported locales: https://developers.google.com/recaptcha/docs/language
249252
// It seems Google has already mapping for locales missing in that list, so that we can provide locale es it is.
@@ -1030,7 +1033,7 @@ public function setupBackendPage()
10301033
];
10311034
$isNewSubmissionLinkPresent = true;
10321035
}
1033-
1036+
10341037
$menu['dashboards'] = [
10351038
'name' => __('navigation.dashboards'),
10361039
'icon' => 'Dashboard',
@@ -1380,15 +1383,15 @@ public function display($template = null, $cache_id = null, $compile_id = null,
13801383
];
13811384

13821385
if($context) {
1383-
$pageContext = array_merge($pageContext, [
1386+
$pageContext = array_merge($pageContext, [
13841387
'dateFormatShort' => PKPString::convertStrftimeFormat($context->getLocalizedDateFormatShort()),
13851388
'dateFormatLong' => PKPString::convertStrftimeFormat($context->getLocalizedDateFormatLong()),
13861389
'datetimeFormatShort' => PKPString::convertStrftimeFormat($context->getLocalizedDateTimeFormatShort()),
13871390
'datetimeFormatLong' => PKPString::convertStrftimeFormat($context->getLocalizedDateTimeFormatLong()),
13881391
'timeFormat' => PKPString::convertStrftimeFormat($context->getLocalizedTimeFormat()),
13891392
]);
13901393
} else {
1391-
$pageContext = array_merge($pageContext, [
1394+
$pageContext = array_merge($pageContext, [
13921395
'dateFormatShort' => PKPString::convertStrftimeFormat(Config::getVar('general', 'date_format_short')),
13931396
'dateFormatLong' => PKPString::convertStrftimeFormat(Config::getVar('general', 'date_format_long')),
13941397
'datetimeFormatShort' => PKPString::convertStrftimeFormat(Config::getVar('general', 'datetime_format_short')),

pages/login/LoginHandler.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function signIn(array $args, PKPRequest $request): void
152152
Validation::logout();
153153
$request->redirect(null, null, 'changePassword', [$user->getUsername()]);
154154
}
155-
$source = str_replace('@', '', $request->getUserVar('source'));
155+
$source = $request->getUserVar('source');
156156
if (preg_match('#^/\w#', (string) $source) === 1) {
157157
$request->redirectUrl($source);
158158
}
@@ -194,7 +194,7 @@ public function signOut($args, $request)
194194
Validation::logout();
195195
}
196196

197-
$source = str_replace('@', '', $request->getUserVar('source'));
197+
$source = $request->getUserVar('source');
198198
if (isset($source) && !empty($source)) {
199199
$request->redirectUrl($request->getProtocol() . '://' . $request->getServerHost() . $source, false);
200200
} else {
@@ -214,6 +214,12 @@ public function lostPassword($args, $request)
214214
$this->setupTemplate($request);
215215
$templateMgr = TemplateManager::getManager($request);
216216

217+
// Check if reCAPTCHA is enabled for lost password
218+
$isCaptchaEnabled = Config::getVar('captcha', 'captcha_on_lost_password') && Config::getVar('captcha', 'recaptcha');
219+
if ($isCaptchaEnabled) {
220+
$templateMgr->assign('recaptchaPublicKey', Config::getVar('captcha', 'recaptcha_public_key'));
221+
}
222+
217223
$this->_generateAltchaComponent('altcha_on_lost_password', $templateMgr);
218224
$templateMgr->display('frontend/pages/userLostPassword.tpl');
219225
}
@@ -226,8 +232,25 @@ public function requestResetPassword($args, $request)
226232
$this->setupTemplate($request);
227233
$templateMgr = TemplateManager::getManager($request);
228234

229-
$altchaHasError = $this->_validateAltchasResponse($request, 'altcha_on_lost_password');
235+
// Validate reCAPTCHA if enabled
236+
$isCaptchaEnabled = Config::getVar('captcha', 'captcha_on_lost_password') && Config::getVar('captcha', 'recaptcha');
237+
if ($isCaptchaEnabled) {
238+
$templateMgr->assign('recaptchaPublicKey', Config::getVar('captcha', 'recaptcha_public_key'));
239+
try {
240+
FormValidatorReCaptcha::validateResponse($request->getUserVar('g-recaptcha-response'), $request->getRemoteAddr(), $request->getServerHost());
241+
} catch (Exception $exception) {
242+
// Keep the reCAPTCHA public key in the template
243+
$templateMgr->assign([
244+
'recaptchaPublicKey' => Config::getVar('captcha', 'recaptcha_public_key'),
245+
'error' => 'user.login.lostPassword.confirmationSentFailedWithReason',
246+
'reason' => __('common.captcha.error.invalid-input-response')
247+
]);
248+
$templateMgr->display('frontend/pages/userLostPassword.tpl');
249+
return;
250+
}
251+
}
230252

253+
$altchaHasError = $this->_validateAltchasResponse($request, 'altcha_on_lost_password');
231254
if ($altchaHasError) {
232255
$this->_generateAltchaComponent('altcha_on_lost_password', $templateMgr);
233256

@@ -491,8 +514,11 @@ public function _redirectByURL($request)
491514
*/
492515
protected function sendHome($request)
493516
{
494-
$pkpPageRouter = $request->getRouter(); /** @var \PKP\core\PKPPageRouter $pkpPageRouter */
495-
$pkpPageRouter->redirectHome($request);
517+
if ($request->getContext()) {
518+
$request->redirect(null, 'submissions');
519+
} else {
520+
$request->redirect(null, 'user');
521+
}
496522
}
497523

498524
/**
@@ -510,7 +536,6 @@ private function _validateAltchasResponse($request, $altchaConfigKey): ?string
510536
return 'common.captcha.error.missing-input-response';
511537
}
512538
}
513-
return null;
514539
}
515540

516541
/**

templates/frontend/pages/userLostPassword.tpl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@
4040
</label>
4141
</div>
4242

43+
{* recaptcha spam blocker *}
44+
{if $recaptchaPublicKey}
45+
<fieldset class="recaptcha_wrapper">
46+
<div class="fields">
47+
<div class="recaptcha">
48+
<div class="g-recaptcha" data-sitekey="{$recaptchaPublicKey|escape}">
49+
</div><label for="g-recaptcha-response" style="display:none;" hidden>Recaptcha response</label>
50+
</div>
51+
</div>
52+
</fieldset>
53+
{/if}
54+
4355
{* altcha spam blocker *}
4456
{if $altchaEnabled}
4557
<fieldset class="altcha_wrapper">
@@ -48,7 +60,6 @@
4860
</div>
4961
</fieldset>
5062
{/if}
51-
5263
<div class="buttons">
5364
<button class="submit" type="submit">
5465
{translate key="user.login.resetPassword"}

0 commit comments

Comments
 (0)