ltsecurityinc安防产品在线商城代码
This commit is contained in:
+196
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Security\Model\Plugin;
|
||||
|
||||
use Laminas\Stdlib\Parameters;
|
||||
use Magento\Customer\Api\AccountManagementInterface;
|
||||
use Magento\Customer\Model\AccountManagement as CustomerAccountManagement;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\Exception\SecurityViolationException;
|
||||
use Magento\Framework\Module\Manager;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Framework\Phrase;
|
||||
use Magento\Security\Model\ConfigInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Interception\PluginList;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for account manager plugin.
|
||||
*
|
||||
* @magentoAppArea frontend
|
||||
* @magentoDbIsolation enabled
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class AccountManagementTest extends TestCase
|
||||
{
|
||||
/** @var ObjectManagerInterface */
|
||||
private $objectManager;
|
||||
|
||||
/** @var Manager */
|
||||
private $moduleManager;
|
||||
|
||||
/** @var AccountManagementInterface */
|
||||
private $accountManagement;
|
||||
|
||||
/** @var ConfigInterface */
|
||||
private $securityConfig;
|
||||
|
||||
/** @var RequestInterface */
|
||||
private $request;
|
||||
|
||||
/** @var Phrase */
|
||||
private $errorMessage;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->moduleManager = $this->objectManager->get(Manager::class);
|
||||
//This check is needed because Magento_Security independent of Magento_Customer
|
||||
if (!$this->moduleManager->isEnabled('Magento_Customer')) {
|
||||
$this->markTestSkipped('Magento_Customer module disabled.');
|
||||
}
|
||||
$this->accountManagement = $this->objectManager->get(AccountManagementInterface::class);
|
||||
$this->request = $this->objectManager->get(RequestInterface::class);
|
||||
$this->securityConfig = $this->objectManager->get(ConfigInterface::class);
|
||||
$this->errorMessage = __(
|
||||
'We received too many requests for password resets. Please wait and try again later or contact %1.',
|
||||
$this->securityConfig->getCustomerServiceEmail()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testPluginIsRegistered(): void
|
||||
{
|
||||
$pluginInfo = $this->objectManager->get(PluginList::class)->get(CustomerAccountManagement::class);
|
||||
$this->assertSame(
|
||||
AccountManagement::class,
|
||||
$pluginInfo['security_check_customer_password_reset_attempt']['instance']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store customer/password/max_number_password_reset_requests 1
|
||||
* @magentoDataFixture Magento/Security/_files/customer_reset_password.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMaxNumberPasswordResetRequests(): void
|
||||
{
|
||||
$this->prepareServerParameters();
|
||||
$this->expectExceptionObject(new SecurityViolationException($this->errorMessage));
|
||||
$this->accountManagement->initiatePasswordReset(
|
||||
'customer@example.com',
|
||||
CustomerAccountManagement::EMAIL_REMINDER
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store customer/password/min_time_between_password_reset_requests 10
|
||||
* @magentoDataFixture Magento/Security/_files/customer_reset_password.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testTimeBetweenPasswordResetRequests(): void
|
||||
{
|
||||
$this->prepareServerParameters();
|
||||
$this->expectExceptionObject(new SecurityViolationException($this->errorMessage));
|
||||
$this->accountManagement->initiatePasswordReset(
|
||||
'customer@example.com',
|
||||
CustomerAccountManagement::EMAIL_REMINDER
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store customer/password/password_reset_protection_type 0
|
||||
* @magentoConfigFixture current_store customer/password/max_number_password_reset_requests 1
|
||||
* @magentoDataFixture Magento/Security/_files/customer_reset_password.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testPasswordResetProtectionTypeDisabled(): void
|
||||
{
|
||||
$this->prepareServerParameters();
|
||||
$result = $this->accountManagement->initiatePasswordReset(
|
||||
'customer@example.com',
|
||||
CustomerAccountManagement::EMAIL_REMINDER
|
||||
);
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store customer/password/password_reset_protection_type 1
|
||||
* @magentoConfigFixture current_store customer/password/max_number_password_reset_requests 1
|
||||
* @magentoDataFixture Magento/Security/_files/customer_reset_password.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testPasswordResetProtectionTypeByIpAndEmail(): void
|
||||
{
|
||||
$this->prepareServerParameters();
|
||||
$this->expectExceptionObject(new SecurityViolationException($this->errorMessage));
|
||||
$this->accountManagement->initiatePasswordReset(
|
||||
'customer@example.com',
|
||||
CustomerAccountManagement::EMAIL_REMINDER
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store customer/password/password_reset_protection_type 2
|
||||
* @magentoConfigFixture current_store customer/password/max_number_password_reset_requests 1
|
||||
* @magentoDataFixture Magento/Security/_files/customer_reset_password.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testPasswordResetProtectionTypeByIp(): void
|
||||
{
|
||||
$this->markTestSkipped('Test blocked by issue MC-32988.');
|
||||
$this->prepareServerParameters();
|
||||
$this->expectExceptionObject(new SecurityViolationException($this->errorMessage));
|
||||
$this->accountManagement->initiatePasswordReset(
|
||||
'customer@example.com',
|
||||
CustomerAccountManagement::EMAIL_REMINDER
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store customer/password/password_reset_protection_type 3
|
||||
* @magentoConfigFixture current_store customer/password/max_number_password_reset_requests 1
|
||||
* @magentoDataFixture Magento/Security/_files/customer_reset_password.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testPasswordResetProtectionTypeByEmail(): void
|
||||
{
|
||||
$this->prepareServerParameters();
|
||||
$this->expectExceptionObject(new SecurityViolationException($this->errorMessage));
|
||||
$this->accountManagement->initiatePasswordReset(
|
||||
'customer@example.com',
|
||||
CustomerAccountManagement::EMAIL_REMINDER
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare server parameters.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function prepareServerParameters(): void
|
||||
{
|
||||
$parameters = $this->objectManager->create(Parameters::class);
|
||||
$parameters->set('REMOTE_ADDR', '127.0.0.1');
|
||||
$this->request->setServer($parameters);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Security\Model\Plugin;
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoAppIsolation enabled
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class AuthSessionTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Backend\Model\Auth
|
||||
*/
|
||||
protected $auth;
|
||||
|
||||
/**
|
||||
* @var \Magento\Backend\Model\Auth\Session
|
||||
*/
|
||||
protected $authSession;
|
||||
|
||||
/**
|
||||
* @var \Magento\Security\Model\AdminSessionInfo
|
||||
*/
|
||||
protected $adminSessionInfo;
|
||||
|
||||
/**
|
||||
* @var \Magento\Security\Model\AdminSessionsManager
|
||||
*/
|
||||
protected $adminSessionsManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
protected $objectManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Stdlib\DateTime
|
||||
*/
|
||||
protected $dateTime;
|
||||
|
||||
/**
|
||||
* @var \Magento\Security\Model\ConfigInterface
|
||||
*/
|
||||
protected $securityConfig;
|
||||
|
||||
/**
|
||||
* Set up
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->objectManager->get(\Magento\Framework\Config\ScopeInterface::class)
|
||||
->setCurrentScope(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE);
|
||||
$this->auth = $this->objectManager->create(\Magento\Backend\Model\Auth::class);
|
||||
$this->authSession = $this->objectManager->create(\Magento\Backend\Model\Auth\Session::class);
|
||||
$this->adminSessionInfo = $this->objectManager->create(\Magento\Security\Model\AdminSessionInfo::class);
|
||||
$this->auth->setAuthStorage($this->authSession);
|
||||
$this->adminSessionsManager = $this->objectManager->get(\Magento\Security\Model\AdminSessionsManager::class);
|
||||
$this->dateTime = $this->objectManager->create(\Magento\Framework\Stdlib\DateTime::class);
|
||||
$this->securityConfig = $this->objectManager->create(\Magento\Security\Model\ConfigInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tear down
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->auth = null;
|
||||
$this->authSession = null;
|
||||
$this->adminSessionInfo = null;
|
||||
$this->adminSessionsManager = null;
|
||||
$this->objectManager = null;
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of prolong user action
|
||||
* session manager will not trigger new prolong if previous prolong was less than X sec ago
|
||||
* X - is calculated based on current admin session lifetime
|
||||
*
|
||||
* @see \Magento\Security\Model\AdminSessionsManager::lastProlongIsOldEnough
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
public function testConsecutiveProcessProlong()
|
||||
{
|
||||
$this->auth->login(
|
||||
\Magento\TestFramework\Bootstrap::ADMIN_NAME,
|
||||
\Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
|
||||
);
|
||||
$adminSessionInfoId = $this->authSession->getAdminSessionInfoId();
|
||||
$prolongsDiff = log($this->securityConfig->getAdminSessionLifetime()) - 2; // X from comment above
|
||||
$dateInPast = $this->dateTime->formatDate((int) ($this->authSession->getUpdatedAt() - $prolongsDiff));
|
||||
$this->adminSessionsManager->getCurrentSession()
|
||||
->setData(
|
||||
'updated_at',
|
||||
$dateInPast
|
||||
)
|
||||
->save();
|
||||
$this->adminSessionInfo->load($adminSessionInfoId, 'id');
|
||||
$oldUpdatedAt = $this->adminSessionInfo->getUpdatedAt();
|
||||
$this->authSession->prolong();
|
||||
$this->adminSessionInfo->load($adminSessionInfoId, 'id');
|
||||
$updatedAt = $this->adminSessionInfo->getUpdatedAt();
|
||||
|
||||
$this->assertSame(strtotime($oldUpdatedAt), strtotime($updatedAt));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of prolong user action
|
||||
* session manager will trigger new prolong if previous prolong was more than X sec ago
|
||||
* X - is calculated based on current admin session lifetime
|
||||
*
|
||||
* @see \Magento\Security\Model\AdminSessionsManager::lastProlongIsOldEnough
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
public function testProcessProlong()
|
||||
{
|
||||
$this->auth->login(
|
||||
\Magento\TestFramework\Bootstrap::ADMIN_NAME,
|
||||
\Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
|
||||
);
|
||||
$adminSessionInfoId = $this->authSession->getAdminSessionInfoId();
|
||||
$prolongsDiff = 4 * log($this->securityConfig->getAdminSessionLifetime()) + 2; // X from comment above
|
||||
$dateInPast = $this->dateTime->formatDate((int) ($this->authSession->getUpdatedAt() - $prolongsDiff));
|
||||
$this->adminSessionsManager->getCurrentSession()
|
||||
->setData(
|
||||
'updated_at',
|
||||
$dateInPast
|
||||
)
|
||||
->save();
|
||||
$this->adminSessionInfo->load($adminSessionInfoId, 'id');
|
||||
$oldUpdatedAt = $this->adminSessionInfo->getUpdatedAt();
|
||||
$this->authSession->prolong();
|
||||
$this->adminSessionInfo->load($adminSessionInfoId, 'id');
|
||||
$updatedAt = $this->adminSessionInfo->getUpdatedAt();
|
||||
|
||||
$this->assertGreaterThan(strtotime($oldUpdatedAt), strtotime($updatedAt));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test processing prolong with an expired user.
|
||||
*
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
public function testProcessProlongWithExpiredUser()
|
||||
{
|
||||
$this->auth->login(
|
||||
\Magento\TestFramework\Bootstrap::ADMIN_NAME,
|
||||
\Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
|
||||
);
|
||||
|
||||
$expireDate = new \DateTime();
|
||||
$expireDate->modify('-10 days');
|
||||
/** @var \Magento\User\Model\User $user */
|
||||
$user = $this->objectManager->create(\Magento\User\Model\User::class);
|
||||
$user->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
|
||||
$userExpirationFactory =
|
||||
$this->objectManager->create(\Magento\Security\Api\Data\UserExpirationInterfaceFactory::class);
|
||||
/** @var \Magento\Security\Api\Data\UserExpirationInterface $userExpiration */
|
||||
$userExpiration = $userExpirationFactory->create();
|
||||
$userExpiration->setId($user->getId())
|
||||
->setExpiresAt($expireDate->format('Y-m-d H:i:s'))
|
||||
->save();
|
||||
|
||||
// need to trigger a prolong
|
||||
$adminSessionInfoId = $this->authSession->getAdminSessionInfoId();
|
||||
$prolongsDiff = 4 * log($this->securityConfig->getAdminSessionLifetime()) + 2;
|
||||
$dateInPast = $this->dateTime->formatDate((int) ($this->authSession->getUpdatedAt() - $prolongsDiff));
|
||||
$this->adminSessionsManager->getCurrentSession()
|
||||
->setData(
|
||||
'updated_at',
|
||||
$dateInPast
|
||||
)
|
||||
->save();
|
||||
$this->adminSessionInfo->load($adminSessionInfoId, 'id');
|
||||
$this->authSession->prolong();
|
||||
static::assertFalse($this->auth->isLoggedIn());
|
||||
$user->reload();
|
||||
static::assertFalse((bool)$user->getIsActive());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user