Skip to content

Commit c7fc6d5

Browse files
committed
Extend token validity on password reset page load
This avoids a race condition where the password reset token is valid at page load time, but no longer valid at page submission time.
1 parent f2eaa1d commit c7fc6d5

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

app/code/Magento/Customer/Controller/Account/CreatePassword.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Customer\Api\AccountManagementInterface;
1111
use Magento\Customer\Model\ForgotPasswordToken\ConfirmCustomerByToken;
12+
use Magento\Customer\Model\ForgotPasswordToken\GetCustomerByToken;
1213
use Magento\Customer\Model\Session;
1314
use Magento\Framework\App\Action\HttpGetActionInterface;
1415
use Magento\Framework\View\Result\PageFactory;
@@ -42,6 +43,11 @@ class CreatePassword extends \Magento\Customer\Controller\AbstractAccount implem
4243
*/
4344
private $confirmByToken;
4445

46+
/**
47+
* @var \Magento\Customer\Model\ForgotPasswordToken\GetCustomerByToken
48+
*/
49+
private $getByToken;
50+
4551
/**
4652
* @param \Magento\Framework\App\Action\Context $context
4753
* @param \Magento\Customer\Model\Session $customerSession
@@ -54,13 +60,16 @@ public function __construct(
5460
Session $customerSession,
5561
PageFactory $resultPageFactory,
5662
AccountManagementInterface $accountManagement,
57-
ConfirmCustomerByToken $confirmByToken = null
63+
ConfirmCustomerByToken $confirmByToken = null,
64+
GetCustomerByToken $getByToken = null
5865
) {
5966
$this->session = $customerSession;
6067
$this->resultPageFactory = $resultPageFactory;
6168
$this->accountManagement = $accountManagement;
6269
$this->confirmByToken = $confirmByToken
6370
?? ObjectManager::getInstance()->get(ConfirmCustomerByToken::class);
71+
$this->getByToken = $getByToken
72+
?? ObjectManager::getInstance()->get(GetCustomerByToken::class);
6473

6574
parent::__construct($context);
6675
}
@@ -83,6 +92,15 @@ public function execute()
8392

8493
$this->confirmByToken->execute($resetPasswordToken);
8594

95+
try {
96+
// Extend token validity to avoid expiration while this form is
97+
// being completed by the user.
98+
$customer = $this->getByToken->execute($resetPasswordToken);
99+
$this->accountManagement->changeResetPasswordLinkToken($customer, $resetPasswordToken);
100+
} catch (\Exception $exception) {
101+
// Intentionally ignoring failures here
102+
}
103+
86104
if ($isDirectLink) {
87105
$this->session->setRpToken($resetPasswordToken);
88106
$resultRedirect = $this->resultRedirectFactory->create();

app/code/Magento/User/Controller/Adminhtml/Auth/ResetPassword.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ public function execute()
2222
try {
2323
$this->_validateResetPasswordLinkToken($userId, $passwordResetToken);
2424

25+
try {
26+
// Extend token validity to avoid expiration while this form is
27+
// being completed by the user.
28+
$user = $this->_userFactory->create()->load($userId);
29+
$user->changeResetPasswordLinkToken($passwordResetToken);
30+
$user->save();
31+
} catch (\Exception $exception) {
32+
// Intentionally ignoring failures here
33+
}
34+
2535
$this->_view->loadLayout();
2636

2737
$content = $this->_view->getLayout()->getBlock('content');

0 commit comments

Comments
 (0)