/** * {@inheritdoc} */ public function handle(Record $recordToHandle, Record $oppositeRecord) { $value = $recordToHandle->getValue('value'); if (!$value) { return; } $this->validate($recordToHandle); $parts = explode(':', $value, 4); $partsCount = count($parts); if ($partsCount == 4) { $initVector = $parts[2]; $encryptedValue = $parts[3]; $mode = MCRYPT_MODE_CBC; $cypher = MCRYPT_RIJNDAEL_128; } else { $initVector = false; $encryptedValue = $value; $mode = MCRYPT_MODE_ECB; $cypher = MCRYPT_BLOWFISH; } $crypt = $this->cryptFactory->create(['key' => $this->cryptKey, 'cipher' => $cypher, 'mode' => $mode, 'initVector' => $initVector]); $decryptedValue = trim($crypt->decrypt(base64_decode((string) $encryptedValue))); $encodedValue = $this->encryptor->encrypt($decryptedValue); $recordToHandle->setValue('value', $encodedValue); }
public function testGetValidateHash() { $password = uniqid(); $hash = $this->_model->getHash($password); $this->assertTrue(is_string($hash)); $this->assertTrue($this->_model->validateHash($password, $hash)); }
public function testValidateKeyDefault() { $crypt = $this->getMock('Magento\\Framework\\Encryption\\Crypt', array(), array(), '', false); $this->_cryptFactory->expects($this->once())->method('create')->with(array('key' => 'cryptKey'))->will($this->returnValue($crypt)); $this->assertSame($crypt, $this->_model->validateKey(null)); // Ensure crypt factory is invoked only once $this->assertSame($crypt, $this->_model->validateKey(null)); }
public function testValidateKey() { $actual = $this->_model->validateKey('some_key'); $crypt = new Crypt('some_key', MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC, $actual->getInitVector()); $expectedEncryptedData = base64_encode($crypt->encrypt('data')); $actualEncryptedData = base64_encode($actual->encrypt('data')); $this->assertEquals($expectedEncryptedData, $actualEncryptedData); $this->assertEquals($crypt->decrypt($expectedEncryptedData), $actual->decrypt($actualEncryptedData)); }
/** * @inheritdoc */ protected function execute(InputInterface $input, OutputInterface $output) { $this->collection->addAttributeToSelect('*'); $customerCollection = $this->collection->getItems(); /** @var $customer Customer */ foreach ($customerCollection as $customer) { $customer->load($customer->getId()); if (!$this->encryptor->validateHashVersion($customer->getPasswordHash())) { list($hash, $salt, $version) = explode(Encryptor::DELIMITER, $customer->getPasswordHash(), 3); $version .= Encryptor::DELIMITER . Encryptor::HASH_VERSION_LATEST; $customer->setPasswordHash($this->encryptor->getHash($hash, $salt, $version)); $customer->save(); $output->write("."); } } $output->writeln("."); $output->writeln("<info>Finished</info>"); }
/** * @param array $attributeData * @param array $expected * * @dataProvider dataProviderEncryptionData * @return void */ public function testHandle($attributeData, $expected) { $fieldName = 'value'; list($key, $dbValue, $initVector, $encryptedValue, $cypher, $mode) = array_values($attributeData); list($decryptedValue, $newValue) = array_values($expected); /** @var \Migration\ResourceModel\Record|\PHPUnit_Framework_MockObject_MockObject $recordToHandle */ $recordToHandle = $this->getMock('Migration\\ResourceModel\\Record', ['getValue', 'setValue', 'getFields'], [], '', false); $recordToHandle->expects($this->once())->method('getValue')->with($fieldName)->willReturn($dbValue); $recordToHandle->expects($this->once())->method('setValue')->with($fieldName, $newValue); $recordToHandle->expects($this->once())->method('getFields')->will($this->returnValue([$fieldName])); $oppositeRecord = $this->getMockBuilder('Migration\\ResourceModel\\Record')->disableOriginalConstructor()->getMock(); $crypt = $this->getMock('\\Magento\\Framework\\Encryption\\Crypt', ['decrypt'], [$key, $cypher, $mode, $initVector], '', true); $crypt->expects($this->once())->method('decrypt')->with(base64_decode((string) $encryptedValue))->willReturn($decryptedValue); $this->configReader->expects($this->once())->method('getOption')->with(self::CRYPT_KEY)->will($this->returnValue($key)); $this->cryptFactory->expects($this->once())->method('create')->with(['key' => $key, 'cipher' => $cypher, 'mode' => $mode, 'initVector' => $initVector])->will($this->returnValue($crypt)); $this->encryptor->expects($this->once())->method('encrypt')->with($decryptedValue)->willReturn($newValue); $handler = new \Migration\Handler\Settings\Encrypt($this->encryptor, $this->cryptFactory, $this->configReader); $handler->setField($fieldName); $handler->handle($recordToHandle, $oppositeRecord); }
public function testUpgradeCustomerPassword() { $customerId = '1'; $password = '******'; $passwordHash = 'hash:salt:999'; $model = $this->getMockBuilder('Magento\\Customer\\Model\\Customer')->disableOriginalConstructor()->setMethods(['getId'])->getMock(); $customer = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMockForAbstractClass(); $customerSecure = $this->getMockBuilder('Magento\\Customer\\Model\\Data\\CustomerSecure')->disableOriginalConstructor()->setMethods(['getPasswordHash', 'setPasswordHash'])->getMock(); $model->expects($this->exactly(2))->method('getId')->willReturn($customerId); $this->customerRepository->expects($this->once())->method('getById')->with($customerId)->willReturn($customer); $this->customerRegistry->expects($this->once())->method('retrieveSecureData')->with($customerId)->willReturn($customerSecure); $customerSecure->expects($this->once())->method('getPasswordHash')->willReturn($passwordHash); $this->encryptorMock->expects($this->once())->method('validateHashVersion')->with($passwordHash)->willReturn(false); $this->encryptorMock->expects($this->once())->method('getHash')->with($password, true)->willReturn($passwordHash); $customerSecure->expects($this->once())->method('setPasswordHash')->with($passwordHash); $this->customerRepository->expects($this->once())->method('save')->with($customer); $event = new \Magento\Framework\DataObject(); $event->setData(['password' => 'password', 'model' => $model]); $observerMock = new \Magento\Framework\Event\Observer(); $observerMock->setEvent($event); $this->model->execute($observerMock); }
public function testEncryptDecrypt() { $this->assertEquals('', $this->_model->decrypt($this->_model->encrypt(''))); $this->assertEquals('test', $this->_model->decrypt($this->_model->encrypt('test'))); }
public function testEncryptDecryptNewKeyAdded() { $deploymentConfigMock = $this->getMock('\\Magento\\Framework\\App\\DeploymentConfig', [], [], '', false); $deploymentConfigMock->expects($this->at(0))->method('get')->with(Encryptor::PARAM_CRYPT_KEY)->will($this->returnValue("cryptKey1")); $deploymentConfigMock->expects($this->at(1))->method('get')->with(Encryptor::PARAM_CRYPT_KEY)->will($this->returnValue("cryptKey1\ncryptKey2")); $model1 = new Encryptor($this->_randomGenerator, $deploymentConfigMock); // simulate an encryption key is being added $model2 = new Encryptor($this->_randomGenerator, $deploymentConfigMock); // sample data to encrypt $data = 'Mares eat oats and does eat oats, but little lambs eat ivy.'; // encrypt with old key $encryptedData = $model1->encrypt($data); $decryptedData = $model2->decrypt($encryptedData); $this->assertSame($data, $decryptedData, 'Encryptor failed to decrypt data encrypted by old keys.'); }