/** * Retrieve Session Form Key * * @return string A 16 bit unique key for forms */ public function getFormKey() { if (!$this->isPresent()) { $this->set($this->mathRandom->getRandomString(16)); } return $this->escaper->escapeHtmlAttr($this->session->getData(self::FORM_KEY)); }
/** * Retrieve Session Form Key * * @return string A 16 bit unique key for forms */ public function getFormKey() { if (!$this->session->getData(self::FORM_KEY)) { $this->session->setData(self::FORM_KEY, $this->mathRandom->getRandomString(16)); } return $this->session->getData(self::FORM_KEY); }
/** * Change encryption key * * @param string|null $key * @return null|string * @throws \Exception */ public function changeEncryptionKey($key = null) { // prepare new key, encryptor and new configuration segment if (!$this->writer->checkIfWritable()) { throw new \Exception(__('Deployment configuration file is not writable.')); } if (null === $key) { $key = md5($this->random->getRandomString(ConfigOptionsListConstants::STORE_KEY_RANDOM_STRING_SIZE)); } $this->encryptor->setNewKey($key); $encryptSegment = new ConfigData(ConfigFilePool::APP_ENV); $encryptSegment->set(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY, $this->encryptor->exportKeys()); $configData = [$encryptSegment->getFileKey() => $encryptSegment->getData()]; // update database and config.php $this->beginTransaction(); try { $this->_reEncryptSystemConfigurationValues(); $this->_reEncryptCreditCardNumbers(); $this->writer->saveConfig($configData); $this->commit(); return $key; } catch (\Exception $e) { $this->rollBack(); throw $e; } }
/** * @return string */ public function getPaymentDetailsId() { if ($this->isInMiniCart()) { return 'braintree_paypal_payment_details_minicart'; } else { return 'braintree_paypal_payment_details' . $this->mathRandom->getRandomString(5); } }
/** * Creates encryption key config data * @param array $data * @return ConfigData */ public function createCryptConfig(array $data) { $currentKey = $this->deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY); $configData = new ConfigData(ConfigFilePool::APP_ENV); if (isset($data[ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY])) { if ($currentKey !== null) { $key = $currentKey . "\n" . $data[ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY]; } else { $key = $data[ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY]; } $configData->set(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY, $key); } else { if ($currentKey === null) { $configData->set(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY, md5($this->random->getRandomString(ConfigOptionsListConstants::STORE_KEY_RANDOM_STRING_SIZE))); } } return $configData; }
/** * Generate a [salted] hash. * * $salt can be: * false - salt is not used * true - random salt of the default length will be generated * integer - random salt of specified length will be generated * string - actual salt value to be used * * @param string $password * @param bool|int|string $salt * @return string */ public function getHash($password, $salt = false) { if ($salt === false) { return $this->hash($password); } if ($salt === true) { $salt = self::DEFAULT_SALT_LENGTH; } if (is_integer($salt)) { $salt = $this->_randomGenerator->getRandomString($salt); } return $this->hash($salt . $password) . ':' . $salt; }
/** * @return JsonModel */ public function indexAction() { $this->logger->clear(); $data = Json::decode($this->getRequest()->getContent(), Json::TYPE_ARRAY); $this->config->setConfigData($data); $this->config->install(); $this->setupFactory->setConfig($this->config->getConfigData()); $moduleNames = array_keys($this->moduleList); foreach ($moduleNames as $moduleName) { $setup = $this->setupFactory->create($moduleName); $setup->applyUpdates(); $this->logger->logSuccess($moduleName); } $this->logger->logSuccess('Artifact'); // Set data to config $setup->addConfigData('web/seo/use_rewrites', isset($data['config']['rewrites']['allowed']) ? $data['config']['rewrites']['allowed'] : 0); $setup->addConfigData('web/unsecure/base_url', isset($data['config']['address']['web']) ? $data['config']['address']['web'] : '{{unsecure_base_url}}'); $setup->addConfigData('web/secure/use_in_frontend', isset($data['config']['https']['front']) ? $data['config']['https']['front'] : 0); $setup->addConfigData('web/secure/base_url', isset($data['config']['address']['web']) ? $data['config']['address']['web'] : '{{secure_base_url}}'); $setup->addConfigData('web/secure/use_in_adminhtml', isset($data['config']['https']['admin']) ? $data['config']['https']['admin'] : 0); $setup->addConfigData('general/locale/code', isset($data['store']['language']) ? $data['store']['language'] : 'en_US'); $setup->addConfigData('general/locale/timezone', isset($data['store']['timezone']) ? $data['store']['timezone'] : 'America/Los_Angeles'); $currencyCode = isset($data['store']['currency']) ? $data['store']['currency'] : 'USD'; $setup->addConfigData('currency/options/base', $currencyCode); $setup->addConfigData('currency/options/default', $currencyCode); $setup->addConfigData('currency/options/allow', $currencyCode); // Create administrator account $this->adminAccountFactory->setConfig($this->config->getConfigData()); $adminAccount = $this->adminAccountFactory->create($setup); $adminAccount->save(); $this->logger->logSuccess('Admin User'); if ($data['config']['encrypt']['type'] == 'magento') { $key = md5($this->random->getRandomString(10)); } else { $key = $data['config']['encrypt']['key']; } $this->config->replaceTmpEncryptKey($key); $this->config->replaceTmpInstallDate(date('r')); $phpPath = $this->phpExecutablePath(); exec($phpPath . 'php -f ' . escapeshellarg($this->systemConfig->create()->getMagentoBasePath() . '/dev/shell/run_data_fixtures.php'), $output, $exitCode); if ($exitCode !== 0) { $outputMsg = implode(PHP_EOL, $output); $this->logger->logError(new \Exception('Data Update Failed with Exit Code: ' . $exitCode . PHP_EOL . $outputMsg)); $this->json->setVariable('success', false); } else { $this->logger->logSuccess('Data Updates'); $this->json->setVariable('success', true); } $this->json->setVariable('key', $key); return $this->json; }
/** * Processing object before save data * * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function beforeSave() { $pageGroupIds = []; $tmpPageGroups = []; $pageGroups = $this->getData('page_groups'); if ($pageGroups) { foreach ($pageGroups as $pageGroup) { if (isset($pageGroup[$pageGroup['page_group']])) { $pageGroupData = $pageGroup[$pageGroup['page_group']]; if ($pageGroupData['page_id']) { $pageGroupIds[] = $pageGroupData['page_id']; } if (in_array($pageGroup['page_group'], ['pages', 'page_layouts'])) { $layoutHandle = $pageGroupData['layout_handle']; } else { $layoutHandle = $this->_layoutHandles[$pageGroup['page_group']]; } if (!isset($pageGroupData['template'])) { $pageGroupData['template'] = ''; } $tmpPageGroup = ['page_id' => $pageGroupData['page_id'], 'group' => $pageGroup['page_group'], 'layout_handle' => $layoutHandle, 'for' => $pageGroupData['for'], 'block_reference' => $pageGroupData['block'], 'entities' => '', 'layout_handle_updates' => [$layoutHandle], 'template' => $pageGroupData['template'] ? $pageGroupData['template'] : '']; if ($pageGroupData['for'] == self::SPECIFIC_ENTITIES) { $layoutHandleUpdates = []; foreach (explode(',', $pageGroupData['entities']) as $entity) { $layoutHandleUpdates[] = str_replace('{{ID}}', $entity, $this->_specificEntitiesLayoutHandles[$pageGroup['page_group']]); } $tmpPageGroup['entities'] = $pageGroupData['entities']; $tmpPageGroup['layout_handle_updates'] = $layoutHandleUpdates; } $tmpPageGroups[] = $tmpPageGroup; } } } if (is_array($this->getData('store_ids'))) { $this->setData('store_ids', implode(',', $this->getData('store_ids'))); } $parameters = $this->getData('widget_parameters'); if (is_array($parameters)) { if (array_key_exists('show_pager', $parameters) && !array_key_exists('page_var_name', $parameters)) { $parameters['page_var_name'] = 'p' . $this->mathRandom->getRandomString(5, \Magento\Framework\Math\Random::CHARS_LOWERS); } $this->setData('widget_parameters', serialize($parameters)); } $this->setData('page_groups', $tmpPageGroups); $this->setData('page_group_ids', $pageGroupIds); return parent::beforeSave(); }
/** * @return JsonModel */ public function indexAction() { $this->logger->clear(); $data = Json::decode($this->getRequest()->getContent(), Json::TYPE_ARRAY); $this->config->setConfigData($data); $this->config->install(); $this->setupFactory->setConfig($this->config->getConfigData()); $moduleNames = array_keys($this->moduleList); foreach ($moduleNames as $moduleName) { $setup = $this->setupFactory->create($moduleName); $setup->applyUpdates(); $this->logger->logSuccess($moduleName); } $this->logger->logSuccess('Artifact'); // Set data to config $setup->addConfigData('web/seo/use_rewrites', isset($data['config']['rewrites']['allowed']) ? $data['config']['rewrites']['allowed'] : 0); $setup->addConfigData('web/unsecure/base_url', isset($data['config']['address']['web']) ? $data['config']['address']['web'] : '{{unsecure_base_url}}'); $setup->addConfigData('web/secure/use_in_frontend', isset($data['config']['https']['front']) ? $data['config']['https']['front'] : 0); $setup->addConfigData('web/secure/base_url', isset($data['config']['address']['web']) ? $data['config']['address']['web'] : '{{secure_base_url}}'); $setup->addConfigData('web/secure/use_in_adminhtml', isset($data['config']['https']['admin']) ? $data['config']['https']['admin'] : 0); $setup->addConfigData('general/locale/code', isset($data['store']['language']) ? $data['store']['language'] : 'en_US'); $setup->addConfigData('general/locale/timezone', isset($data['store']['timezone']) ? $data['store']['timezone'] : 'America/Los_Angeles'); $currencyCode = isset($data['store']['currency']) ? $data['store']['currency'] : 'USD'; $setup->addConfigData('currency/options/base', $currencyCode); $setup->addConfigData('currency/options/default', $currencyCode); $setup->addConfigData('currency/options/allow', $currencyCode); // Create administrator account $this->adminAccountFactory->setConfig($this->config->getConfigData()); $adminAccount = $this->adminAccountFactory->create($setup); $adminAccount->save(); $this->logger->logSuccess('Admin User'); if ($data['config']['encrypt']['type'] == 'magento') { $key = md5($this->random->getRandomString(10)); } else { $key = $data['config']['encrypt']['key']; } $this->config->replaceTmpEncryptKey($key); $this->config->replaceTmpInstallDate(date('r')); $this->json->setVariable('success', true); $this->json->setVariable('key', $key); return $this->json; }
/** * Serialize info for Resource Model to save * For new model check and set available cookie key * * @return $this */ public function beforeSave() { parent::beforeSave(); // Setting info $info = []; foreach ($this->getData() as $index => $value) { if (!in_array($index, $this->_unserializableFields)) { $info[$index] = $value; } } $this->setInfo($this->jsonHelper->jsonEncode($info)); if ($this->isObjectNew()) { $this->setWebsiteId($this->_storeManager->getStore()->getWebsiteId()); // Setting cookie key do { $this->setKey($this->mathRandom->getRandomString(self::KEY_LENGTH)); } while (!$this->getResource()->isKeyAllowed($this->getKey())); } return $this; }
/** * {@inheritdoc} */ public function createCustomer(Data\CustomerDetails $customerDetails, $password = null, $redirectUrl = '') { //Generate password hash $password = $password ? $password : $this->mathRandom->getRandomString(self::DEFAULT_PASSWORD_LENGTH); $hash = $this->getPasswordHash($password); return $this->createCustomerWithPasswordHash($customerDetails, $hash, $redirectUrl); }
/** * Generate new login credentials * @param int $adminId * @return $this */ public function generate($adminId) { return $this->setData(['customer_id' => $this->getCustomerId(), 'admin_id' => $adminId, 'secret' => $this->_random->getRandomString(64), 'used' => 0, 'created_at' => $this->_dateTime->gmtTimestamp()])->save(); }
/** * Generate end return new secure hash value * * @param \Magento\Sales\Model\Order\Payment $payment * @return string */ protected function _generateSecureSilentPostHash($payment) { $secureHash = md5($this->mathRandom->getRandomString(10)); $payment->setAdditionalInformation($this->_secureSilentPostHashKey, $secureHash); return $secureHash; }
/** * {@inheritdoc} */ public function createAccount(CustomerInterface $customer, $password = null, $redirectUrl = '') { if (!is_null($password)) { $this->checkPasswordStrength($password); } else { $password = $this->mathRandom->getRandomString(self::MIN_PASSWORD_LENGTH); } $hash = $this->createPasswordHash($password); return $this->createAccountWithPasswordHash($customer, $hash, $redirectUrl); }
/** * Get random string * * @param int $length * @param string|null $chars * @return string */ public function getRandomString($length, $chars = null) { return $this->mathRandom->getRandomString($length, $chars); }
/** * Return a validated encryption key, generating a random one, if no value was initially provided * * @param string|null $key * @return string */ public function getValidEncryptionKey($key = null) { if (!$key) { $key = md5($this->mathRandom->getRandomString(10)); } $this->_encryptor->validateKey($key); return $key; }
/** * Generate password string * * @return string */ protected function generatePassword() { $salt = $this->random->getRandomString(32); return md5($salt . $this->config['admin_password']) . ':' . $salt; }
/** * Generate random string for token or secret or verifier * * @param int $length String length * @return string */ public function generateRandomString($length) { return $this->_mathRandom->getRandomString($length, \Magento\Framework\Math\Random::CHARS_DIGITS . \Magento\Framework\Math\Random::CHARS_LOWERS); }
/** * Generate password string * * @return string */ protected function generatePassword() { $salt = $this->random->getRandomString(32); return md5($salt . $this->data[self::KEY_PASSWORD]) . ':' . $salt; }
/** * Creates encrypt deployment configuration segment * No new encryption key will be added if there is an existing deployment config file unless user provides one. * Old encryption keys will persist. * A new encryption key will be generated if there is no existing deployment config file. * * @param \ArrayObject|array $data * @return \Magento\Framework\App\DeploymentConfig\SegmentInterface */ private function createEncryptConfig($data) { $key = ''; if (isset($data[DeploymentConfigMapper::KEY_ENCRYPTION_KEY])) { $key = $data[DeploymentConfigMapper::KEY_ENCRYPTION_KEY]; } // retrieve old encryption keys if ($this->deploymentConfig->isAvailable()) { $encryptInfo = $this->deploymentConfig->getSegment(EncryptConfig::CONFIG_KEY); $oldKeys = $encryptInfo[EncryptConfig::KEY_ENCRYPTION_KEY]; $key = empty($key) ? $oldKeys : $oldKeys . "\n" . $key; } else { if (empty($key)) { $key = md5($this->random->getRandomString(10)); } } $cryptConfigData = [DeploymentConfigMapper::$paramMap[DeploymentConfigMapper::KEY_ENCRYPTION_KEY] => $key]; // find the latest key to display $keys = explode("\n", $key); $this->installInfo[EncryptConfig::KEY_ENCRYPTION_KEY] = array_pop($keys); return new EncryptConfig($cryptConfigData); }