Skip to content

Commit 25a31a8

Browse files
committed
AC-1323: Rest Token Improvement.
1 parent e0f1e46 commit 25a31a8

File tree

8 files changed

+66
-14
lines changed

8 files changed

+66
-14
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public function execute()
102102

103103
try {
104104
$this->accountManagement->validateResetPasswordLinkToken($customerId, $resetPasswordToken);
105+
$this->confirmByToken->resetCustomerConfirmation($customerId);
105106

106107
// Extend token validity to avoid expiration while this form is
107108
// being completed by the user.

app/code/Magento/Customer/Model/AccountManagement.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,7 @@ public function isResetPasswordLinkTokenExpired($rpToken, $rpTokenCreatedAt)
14671467
* @throws LocalizedException
14681468
* @throws NoSuchEntityException
14691469
*/
1470-
public function changeResetPasswordLinkToken($customer, $passwordLinkToken)
1470+
public function changeResetPasswordLinkToken(CustomerInterface $customer, string $passwordLinkToken): bool
14711471
{
14721472
if (!is_string($passwordLinkToken) || empty($passwordLinkToken)) {
14731473
throw new InputException(
@@ -1476,8 +1476,7 @@ public function changeResetPasswordLinkToken($customer, $passwordLinkToken)
14761476
['value' => $passwordLinkToken, 'fieldName' => 'password reset token']
14771477
)
14781478
);
1479-
}
1480-
if (is_string($passwordLinkToken) && !empty($passwordLinkToken)) {
1479+
} else {
14811480
$customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId());
14821481
$customerSecure->setRpToken($passwordLinkToken);
14831482
$customerSecure->setRpTokenCreatedAt(

app/code/Magento/Customer/Model/ForgotPasswordToken/ConfirmCustomerByToken.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,19 @@ private function resetConfirmation(CustomerInterface $customer): void
6565

6666
$this->customerRepository->save($customer);
6767
}
68+
69+
/**
70+
* Check if customer confirmation needs to be reset
71+
*
72+
* @param int $customerId
73+
* @return void
74+
*/
75+
public function resetCustomerConfirmation(int $customerId): void
76+
{
77+
$customer = $this->customerRepository->getById($customerId);
78+
79+
if ($customer) {
80+
$this->resetConfirmation($customer);
81+
}
82+
}
6883
}

app/code/Magento/Customer/Model/ResourceModel/Customer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class Customer extends \Magento\Eav\Model\Entity\VersionControl\AbstractEntity
7373
* @param array $data
7474
* @param AccountConfirmation $accountConfirmation
7575
* @param EncryptorInterface|null $encryptor
76+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
7677
*/
7778
public function __construct(
7879
\Magento\Eav\Model\Entity\Context $context,

app/code/Magento/User/Model/ResourceModel/User.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Framework\Model\AbstractModel;
1818
use Magento\User\Model\Backend\Config\ObserverConfig;
1919
use Magento\User\Model\User as ModelUser;
20+
use Magento\Framework\Encryption\EncryptorInterface;
2021

2122
/**
2223
* ACL user resource
@@ -49,6 +50,11 @@ class User extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
4950
*/
5051
private $observerConfig;
5152

53+
/**
54+
* @var EncryptorInterface|null
55+
*/
56+
private $encryptor;
57+
5258
/**
5359
* Construct
5460
*
@@ -58,20 +64,23 @@ class User extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
5864
* @param string $connectionName
5965
* @param CacheInterface $aclDataCache
6066
* @param ObserverConfig|null $observerConfig
67+
* @param EncryptorInterface|null $encryptor
6168
*/
6269
public function __construct(
6370
\Magento\Framework\Model\ResourceModel\Db\Context $context,
6471
\Magento\Authorization\Model\RoleFactory $roleFactory,
6572
\Magento\Framework\Stdlib\DateTime $dateTime,
6673
$connectionName = null,
6774
CacheInterface $aclDataCache = null,
68-
ObserverConfig $observerConfig = null
75+
ObserverConfig $observerConfig = null,
76+
EncryptorInterface $encryptor = null
6977
) {
7078
parent::__construct($context, $connectionName);
7179
$this->_roleFactory = $roleFactory;
7280
$this->dateTime = $dateTime;
7381
$this->aclDataCache = $aclDataCache ?: ObjectManager::getInstance()->get(CacheInterface::class);
7482
$this->observerConfig = $observerConfig ?: ObjectManager::getInstance()->get(ObserverConfig::class);
83+
$this->encryptor = $encryptor ?? ObjectManager::getInstance()->get(EncryptorInterface::class);
7584
}
7685

7786
/**
@@ -180,6 +189,10 @@ protected function _beforeSave(AbstractModel $user)
180189
if ($user->hasRoleId()) {
181190
$user->setReloadAclFlag(1);
182191
}
192+
if ($user->getData('rp_token')) {
193+
$rpToken = $user->getData('rp_token');
194+
$user->setRpToken($this->encryptor->encrypt($rpToken));
195+
}
183196

184197
return parent::_beforeSave($user);
185198
}
@@ -197,6 +210,10 @@ protected function _afterSave(AbstractModel $user)
197210
$this->_clearUserRoles($user);
198211
$this->_createUserRole($user->getRoleId(), $user);
199212
}
213+
if ($user->getData('rp_token')) {
214+
$rpToken = $user->getData('rp_token');
215+
$user->setRpToken($this->encryptor->decrypt($rpToken));
216+
}
200217
return $this;
201218
}
202219

@@ -255,6 +272,10 @@ protected function _afterLoad(AbstractModel $user)
255272
if (is_string($user->getExtra())) {
256273
$user->setExtra($this->getSerializer()->unserialize($user->getExtra()));
257274
}
275+
if ($user->getData('rp_token')) {
276+
$rpToken = $user->getData('rp_token');
277+
$user->setRpToken($this->encryptor->decrypt($rpToken));
278+
}
258279
return parent::_afterLoad($user);
259280
}
260281

dev/tests/integration/testsuite/Magento/Customer/Controller/AccountTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public function testCreatepasswordActionWithDirectLink()
113113
$customer->save();
114114

115115
$this->getRequest()->setParam('token', $token);
116+
$this->getRequest()->setParam('id', 1);
116117

117118
$this->dispatch('customer/account/createPassword');
118119

@@ -266,7 +267,7 @@ public function testActiveUserConfirmationAction()
266267
/**
267268
* @magentoDataFixture Magento/Customer/_files/customer.php
268269
*/
269-
public function testResetPasswordPostNoTokenAction()
270+
public function testResetPasswordPostNoEmail()
270271
{
271272
$this->getRequest()
272273
->setParam('id', 1)
@@ -282,7 +283,7 @@ public function testResetPasswordPostNoTokenAction()
282283
$this->dispatch('customer/account/resetPasswordPost');
283284
$this->assertRedirect($this->stringContains('customer/account/'));
284285
$this->assertSessionMessages(
285-
$this->equalTo(['Something went wrong while saving the new password.']),
286+
$this->equalTo(['"email" is required. Enter and try again.']),
286287
MessageInterface::TYPE_ERROR
287288
);
288289
}
@@ -622,7 +623,8 @@ public function testResetPasswordWhenEmailChanged(): void
622623
$customerRegistry = $this->_objectManager->get(CustomerRegistry::class);
623624
$customerData = $customerRegistry->retrieveByEmail($email);
624625
$token = $customerData->getRpToken();
625-
$this->assertForgotPasswordEmailContent($token);
626+
$customerId = $customerData->getId();
627+
$this->assertForgotPasswordEmailContent($token, $customerId);
626628

627629
/* Set new email */
628630
/** @var CustomerRepositoryInterface $customerRepository */
@@ -701,10 +703,11 @@ private function dispatchLoginPostAction(string $email, string $password): void
701703
* @param string $token
702704
* @return void
703705
*/
704-
private function assertForgotPasswordEmailContent(string $token): void
706+
private function assertForgotPasswordEmailContent(string $token, int $customerId): void
705707
{
706708
$message = $this->transportBuilderMock->getSentMessage();
707-
$pattern = "/<a.+customer\/account\/createPassword\/\?token={$token}.+Set\s+a\s+New\s+Password<\/a\>/";
709+
//phpcs:ignore
710+
$pattern = "/<a.+customer\/account\/createPassword\/\?id={$customerId}&amp;token={$token}.+Set\s+a\s+New\s+Password<\/a\>/";
708711
$rawMessage = $message->getBody()->getParts()[0]->getRawContent();
709712
$messageConstraint = $this->logicalAnd(
710713
new StringContains('There was recently a request to change the password for your account.'),

dev/tests/integration/testsuite/Magento/Customer/Model/AccountManagementTest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,14 @@ public function testChangePassword()
192192
$this->startNewSession($activeSessionId);
193193
$this->assertNotNull($this->getCustomerCutoff($customerId), 'Customer cutoff session should be set.');
194194
// Make sure current visitor session is updated.
195-
$this->assertLessThanOrEqual($this->getCustomerCutoff($customerId), $this->getVisitorCreatedAt($activeVisitor->getId()));
196-
$this->assertGreaterThan($this->getCustomerCutoff($customerId), $this->getVisitorCreatedAt($currentVisitor->getId()));
195+
$this->assertLessThanOrEqual(
196+
$this->getCustomerCutoff($customerId),
197+
$this->getVisitorCreatedAt($activeVisitor->getId())
198+
);
199+
$this->assertGreaterThan(
200+
$this->getCustomerCutoff($customerId),
201+
$this->getVisitorCreatedAt($currentVisitor->getId())
202+
);
197203

198204
$this->accountManagement->authenticate('customer@example.com', 'new_Password123');
199205
}
@@ -441,8 +447,14 @@ public function testResetPassword()
441447
$this->startNewSession($activeSessionId);
442448
$this->assertNotNull($this->getCustomerCutoff($customerId), 'Customer cutoff session should be set.');
443449
// Make sure current visitor session is updated.
444-
$this->assertLessThanOrEqual($this->getCustomerCutoff($customerId), $this->getVisitorCreatedAt($activeVisitor->getId()));
445-
$this->assertGreaterThan($this->getCustomerCutoff($customerId), $this->getVisitorCreatedAt($currentVisitor->getId()));
450+
$this->assertLessThanOrEqual(
451+
$this->getCustomerCutoff($customerId),
452+
$this->getVisitorCreatedAt($activeVisitor->getId())
453+
);
454+
$this->assertGreaterThan(
455+
$this->getCustomerCutoff($customerId),
456+
$this->getVisitorCreatedAt($currentVisitor->getId())
457+
);
446458
}
447459

448460
/**

dev/tests/integration/testsuite/Magento/Customer/Model/ForgotPasswordToken/ConfirmCustomerByTokenTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function testExecuteWithInvalidAddress(): void
7171
//make city address invalid
7272
$this->makeCityInvalid($id);
7373

74-
$this->confirmCustomerByToken->execute(self::STUB_CUSTOMER_RESET_TOKEN);
74+
$this->confirmCustomerByToken->resetCustomerConfirmation($id);
7575
$this->assertNull($customerModel->load($id)->getConfirmation());
7676
}
7777

0 commit comments

Comments
 (0)