diff --git a/app/code/Magento/Checkout/Model/Type/Onepage.php b/app/code/Magento/Checkout/Model/Type/Onepage.php
index fbf014927c3d1..c49669e912c33 100644
--- a/app/code/Magento/Checkout/Model/Type/Onepage.php
+++ b/app/code/Magento/Checkout/Model/Type/Onepage.php
@@ -530,7 +530,7 @@ protected function _validateCustomerData(array $data)
$quote = $this->getQuote();
$isCustomerNew = !$quote->getCustomerId();
$customer = $quote->getCustomer();
- $customerData = $this->extensibleDataObjectConverter->toFlatArray($customer);
+ $customerData = $this->extensibleDataObjectConverter->toFlatArray($customer, [], '\Magento\Customer\Api\Data\CustomerInterface');
/** @var Form $customerForm */
$customerForm = $this->_formFactory->create(
@@ -594,7 +594,7 @@ protected function _validateCustomerData(array $data)
$this->_objectCopyService->copyFieldsetToTarget(
'customer_account',
'to_quote',
- $this->extensibleDataObjectConverter->toFlatArray($customer),
+ $this->extensibleDataObjectConverter->toFlatArray($customer, [], '\Magento\Customer\Api\Data\CustomerInterface'),
$quote
);
diff --git a/app/code/Magento/Customer/Block/Adminhtml/Edit/Form.php b/app/code/Magento/Customer/Block/Adminhtml/Edit/Form.php
index 48f82f167702c..554d71463d59a 100644
--- a/app/code/Magento/Customer/Block/Adminhtml/Edit/Form.php
+++ b/app/code/Magento/Customer/Block/Adminhtml/Edit/Form.php
@@ -72,7 +72,11 @@ protected function _prepareForm()
$form->addField('id', 'hidden', ['name' => 'customer_id']);
$customer = $this->_customerRepository->getById($customerId);
$form->setValues(
- $this->_extensibleDataObjectConverter->toFlatArray($customer)
+ $this->_extensibleDataObjectConverter->toFlatArray(
+ $customer,
+ [],
+ '\Magento\Customer\Api\Data\CustomerInterface'
+ )
)->addValues(
['customer_id' => $customerId]
);
diff --git a/app/code/Magento/Customer/Block/Adminhtml/Edit/Tab/Account.php b/app/code/Magento/Customer/Block/Adminhtml/Edit/Tab/Account.php
index 1c42b1674ee7b..c459b24d4377b 100644
--- a/app/code/Magento/Customer/Block/Adminhtml/Edit/Tab/Account.php
+++ b/app/code/Magento/Customer/Block/Adminhtml/Edit/Tab/Account.php
@@ -172,7 +172,11 @@ protected function _customizeFieldset($fieldset)
);
$form->getElement('website_id')->setRenderer($renderer);
- $accountData = $this->_extensibleDataObjectConverter->toFlatArray($this->_getCustomerDataObject());
+ $accountData = $this->_extensibleDataObjectConverter->toFlatArray(
+ $this->_getCustomerDataObject(),
+ [],
+ '\Magento\Customer\Api\Data\CustomerInterface'
+ );
if ($this->_getCustomerDataObject()->getId()) {
$customerFormFields = $this->_addEditCustomerFormFields($fieldset);
diff --git a/app/code/Magento/Customer/Controller/Adminhtml/Index/Validate.php b/app/code/Magento/Customer/Controller/Adminhtml/Index/Validate.php
index 3ba77a04f16a1..de2ad486e6b3d 100644
--- a/app/code/Magento/Customer/Controller/Adminhtml/Index/Validate.php
+++ b/app/code/Magento/Customer/Controller/Adminhtml/Index/Validate.php
@@ -29,7 +29,11 @@ protected function _validateCustomer($response)
$customerForm = $this->_formFactory->create(
'customer',
'adminhtml_customer',
- $this->_extensibleDataObjectConverter->toFlatArray($customer),
+ $this->_extensibleDataObjectConverter->toFlatArray(
+ $customer,
+ [],
+ '\Magento\Customer\Api\Data\CustomerInterface'
+ ),
true
);
$customerForm->setInvisibleIgnored(true);
diff --git a/app/code/Magento/Customer/Model/AccountManagement.php b/app/code/Magento/Customer/Model/AccountManagement.php
index 28448a745f80a..56871b94cebd5 100644
--- a/app/code/Magento/Customer/Model/AccountManagement.php
+++ b/app/code/Magento/Customer/Model/AccountManagement.php
@@ -683,7 +683,7 @@ protected function createPasswordHash($password)
public function validate(\Magento\Customer\Api\Data\CustomerInterface $customer)
{
$customerErrors = $this->validator->validateData(
- $this->extensibleDataObjectConverter->toFlatArray($customer),
+ $this->extensibleDataObjectConverter->toFlatArray($customer, [], '\Magento\Customer\Api\Data\CustomerInterface'),
[],
'customer'
);
diff --git a/app/code/Magento/Customer/Model/Address/Mapper.php b/app/code/Magento/Customer/Model/Address/Mapper.php
index 00e916c8d64f3..fe19174210d14 100644
--- a/app/code/Magento/Customer/Model/Address/Mapper.php
+++ b/app/code/Magento/Customer/Model/Address/Mapper.php
@@ -37,7 +37,7 @@ public function __construct(ExtensibleDataObjectConverter $extensibleDataObjectC
*/
public function toFlatArray($addressDataObject)
{
- $flatAddressArray = $this->extensibleDataObjectConverter->toFlatArray($addressDataObject);
+ $flatAddressArray = $this->extensibleDataObjectConverter->toFlatArray($addressDataObject, [], '\Magento\Customer\Api\Data\AddressInterface');
//preserve street
$street = $addressDataObject->getStreet();
if (!empty($street) && is_array($street)) {
diff --git a/app/code/Magento/Customer/Model/Customer/Mapper.php b/app/code/Magento/Customer/Model/Customer/Mapper.php
index f2b17fde98ca9..92b618edb243d 100644
--- a/app/code/Magento/Customer/Model/Customer/Mapper.php
+++ b/app/code/Magento/Customer/Model/Customer/Mapper.php
@@ -36,7 +36,7 @@ public function __construct(ExtensibleDataObjectConverter $extensibleDataObjectC
*/
public function toFlatArray(CustomerInterface $customer)
{
- $flatArray = $this->extensibleDataObjectConverter->toNestedArray($customer);
+ $flatArray = $this->extensibleDataObjectConverter->toNestedArray($customer, [], '\Magento\Customer\Api\Data\CustomerInterface');
unset($flatArray["addresses"]);
return ConvertArray::toFlatArray($flatArray);
}
diff --git a/app/code/Magento/Customer/Model/Resource/CustomerRepository.php b/app/code/Magento/Customer/Model/Resource/CustomerRepository.php
index bafb724910396..47b67411fafbd 100644
--- a/app/code/Magento/Customer/Model/Resource/CustomerRepository.php
+++ b/app/code/Magento/Customer/Model/Resource/CustomerRepository.php
@@ -125,7 +125,9 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
{
$this->validate($customer);
$customerData = $this->extensibleDataObjectConverter->toFlatArray(
- $this->customerBuilder->populate($customer)->setAddresses([])->create()
+ $this->customerBuilder->populate($customer)->setAddresses([])->create(),
+ [],
+ '\Magento\Customer\Api\Data\CustomerInterface'
);
$customerModel = $this->customerFactory->create(['data' => $customerData]);
$storeId = $customerModel->getStoreId();
diff --git a/app/code/Magento/Customer/Model/Resource/Group/Grid/ServiceCollection.php b/app/code/Magento/Customer/Model/Resource/Group/Grid/ServiceCollection.php
index 53b90c7b0346a..22980e9d14f0c 100644
--- a/app/code/Magento/Customer/Model/Resource/Group/Grid/ServiceCollection.php
+++ b/app/code/Magento/Customer/Model/Resource/Group/Grid/ServiceCollection.php
@@ -67,7 +67,7 @@ public function loadData($printQuery = false, $logQuery = false)
$groups = $searchResults->getItems();
foreach ($groups as $group) {
$groupItem = new \Magento\Framework\Object();
- $groupItem->addData($this->simpleDataObjectConverter->toFlatArray($group));
+ $groupItem->addData($this->simpleDataObjectConverter->toFlatArray($group, '\Magento\Customer\Api\Data\GroupInterface'));
$this->_addItem($groupItem);
}
$this->_setIsLoaded();
diff --git a/app/code/Magento/Integration/Model/Oauth/Consumer.php b/app/code/Magento/Integration/Model/Oauth/Consumer.php
index d81963d6a98b2..768eb4e78884d 100644
--- a/app/code/Magento/Integration/Model/Oauth/Consumer.php
+++ b/app/code/Magento/Integration/Model/Oauth/Consumer.php
@@ -38,11 +38,17 @@ class Consumer extends \Magento\Framework\Model\AbstractModel implements Consume
*/
protected $_keyLengthFactory;
+ /**
+ * @var \Magento\Integration\Helper\Oauth\Data
+ */
+ protected $dataHelper;
+
/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Integration\Model\Oauth\Consumer\Validator\KeyLengthFactory $keyLengthFactory
* @param \Magento\Framework\Url\Validator $urlValidator
+ * @param \Magento\Integration\Helper\Oauth\Data $dataHelper
* @param \Magento\Framework\Model\Resource\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\Db $resourceCollection
* @param array $data
@@ -52,12 +58,14 @@ public function __construct(
\Magento\Framework\Registry $registry,
\Magento\Integration\Model\Oauth\Consumer\Validator\KeyLengthFactory $keyLengthFactory,
\Magento\Framework\Url\Validator $urlValidator,
+ \Magento\Integration\Helper\Oauth\Data $dataHelper,
\Magento\Framework\Model\Resource\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\Db $resourceCollection = null,
array $data = []
) {
$this->_keyLengthFactory = $keyLengthFactory;
$this->_urlValidator = $urlValidator;
+ $this->dataHelper = $dataHelper;
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}
@@ -166,4 +174,13 @@ public function getCreatedAt()
{
return $this->getData('created_at');
}
+
+ /**
+ * {@inheritdoc}
+ */
+ public function isValidForTokenExchange()
+ {
+ $expiry = $this->dataHelper->getConsumerExpirationPeriod();
+ return $expiry > $this->getResource()->getTimeInSecondsSinceCreation($this->getId());
+ }
}
diff --git a/app/code/Magento/Integration/Model/Oauth/Token/Provider.php b/app/code/Magento/Integration/Model/Oauth/Token/Provider.php
index d9d452740300f..5d9137f6c162e 100644
--- a/app/code/Magento/Integration/Model/Oauth/Token/Provider.php
+++ b/app/code/Magento/Integration/Model/Oauth/Token/Provider.php
@@ -22,32 +22,16 @@ class Provider implements TokenProviderInterface
*/
protected $_tokenFactory;
- /**
- * @var \Magento\Integration\Helper\Oauth\Data
- */
- protected $_dataHelper;
-
- /**
- * @var \Magento\Framework\Stdlib\DateTime\DateTime
- */
- protected $_date;
-
/**
* @param \Magento\Integration\Model\Oauth\Consumer\Factory $consumerFactory
* @param \Magento\Integration\Model\Oauth\TokenFactory $tokenFactory
- * @param \Magento\Integration\Helper\Oauth\Data $dataHelper
- * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
*/
public function __construct(
\Magento\Integration\Model\Oauth\Consumer\Factory $consumerFactory,
- \Magento\Integration\Model\Oauth\TokenFactory $tokenFactory,
- \Magento\Integration\Helper\Oauth\Data $dataHelper,
- \Magento\Framework\Stdlib\DateTime\DateTime $date
+ \Magento\Integration\Model\Oauth\TokenFactory $tokenFactory
) {
$this->_consumerFactory = $consumerFactory;
$this->_tokenFactory = $tokenFactory;
- $this->_dataHelper = $dataHelper;
- $this->_date = $date;
}
/**
@@ -56,9 +40,7 @@ public function __construct(
public function validateConsumer($consumer)
{
// Must use consumer within expiration period.
- $consumerTS = strtotime($consumer->getCreatedAt());
- $expiry = $this->_dataHelper->getConsumerExpirationPeriod();
- if ($this->_date->timestamp() - $consumerTS > $expiry) {
+ if (!$consumer->isValidForTokenExchange()) {
throw new \Magento\Framework\Oauth\Exception(
'Consumer key has expired'
);
diff --git a/app/code/Magento/Integration/Model/Resource/Oauth/Consumer.php b/app/code/Magento/Integration/Model/Resource/Oauth/Consumer.php
index 9dbf856debf74..fa7d3be9807d8 100644
--- a/app/code/Magento/Integration/Model/Resource/Oauth/Consumer.php
+++ b/app/code/Magento/Integration/Model/Resource/Oauth/Consumer.php
@@ -57,4 +57,22 @@ public function _afterDelete(\Magento\Framework\Model\AbstractModel $object)
$adapter->delete($this->getTable('oauth_token'), ['consumer_id' => $object->getId()]);
return parent::_afterDelete($object);
}
+
+ /**
+ * Compute time in seconds since consumer was created.
+ *
+ * @param int $consumerId - The consumer id
+ * @return int - time lapsed in seconds
+ */
+ public function getTimeInSecondsSinceCreation($consumerId)
+ {
+ $adapter = $this->_getReadAdapter();
+ $select = $adapter->select()
+ ->from($this->getMainTable())
+ ->reset(\Zend_Db_Select::COLUMNS)
+ ->columns('CURRENT_TIMESTAMP() - created_at')
+ ->where('entity_id = ?', $consumerId);
+
+ return $adapter->fetchOne($select);
+ }
}
diff --git a/app/code/Magento/Integration/etc/adminhtml/system.xml b/app/code/Magento/Integration/etc/adminhtml/system.xml
index d4b11016d71d7..70b08082df7e1 100644
--- a/app/code/Magento/Integration/etc/adminhtml/system.xml
+++ b/app/code/Magento/Integration/etc/adminhtml/system.xml
@@ -26,7 +26,7 @@
- Disable consumer key/secret credentials if not used within X seconds.
+ Consumer key/secret will expire if not used within X seconds after Oauth token exchange starts.
diff --git a/app/code/Magento/Sales/Block/Adminhtml/Order/Create/Form/Account.php b/app/code/Magento/Sales/Block/Adminhtml/Order/Create/Form/Account.php
index 63a466cca2b8f..aaa9f93d05561 100644
--- a/app/code/Magento/Sales/Block/Adminhtml/Order/Create/Form/Account.php
+++ b/app/code/Magento/Sales/Block/Adminhtml/Order/Create/Form/Account.php
@@ -161,7 +161,7 @@ public function getFormValues()
} catch (\Exception $e) {
/** If customer does not exist do nothing. */
}
- $data = isset($customer) ? $this->_extensibleDataObjectConverter->toFlatArray($customer) : [];
+ $data = isset($customer) ? $this->_extensibleDataObjectConverter->toFlatArray($customer, [], '\Magento\Customer\Api\Data\CustomerInterface') : [];
foreach ($this->getQuote()->getData() as $key => $value) {
if (strpos($key, 'customer_') === 0) {
$data[substr($key, 9)] = $value;
diff --git a/app/code/Magento/Sales/Model/Quote.php b/app/code/Magento/Sales/Model/Quote.php
index e80697e6d628e..a34cc20245be5 100644
--- a/app/code/Magento/Sales/Model/Quote.php
+++ b/app/code/Magento/Sales/Model/Quote.php
@@ -684,7 +684,9 @@ public function setCustomer(\Magento\Customer\Api\Data\CustomerInterface $custom
$this->setCustomerId($customer->getId());
$customerData = $this->objectFactory->create(
$this->extensibleDataObjectConverter->toFlatArray(
- $this->customerBuilder->populate($customer)->setAddresses([])->create()
+ $this->customerBuilder->populate($customer)->setAddresses([])->create(),
+ [],
+ '\Magento\Customer\Api\Data\CustomerInterface'
)
);
$this->_objectCopyService->copyFieldsetToTarget('customer_account', 'to_quote', $customerData, $this);
diff --git a/dev/tests/api-functional/testsuite/Magento/Webapi/Authentication/RestTest.php b/dev/tests/api-functional/testsuite/Magento/Webapi/Authentication/RestTest.php
index a730843207886..29b5f27cad377 100644
--- a/dev/tests/api-functional/testsuite/Magento/Webapi/Authentication/RestTest.php
+++ b/dev/tests/api-functional/testsuite/Magento/Webapi/Authentication/RestTest.php
@@ -176,7 +176,7 @@ public function testGetAccessTokenConsumerMismatch()
/**
* @expectedException \Exception
- * @expectedExceptionMessage HTTP/1.1 401
+ * @expectedExceptionMessage HTTP/1.1 400
*/
public function testAccessApiInvalidAccessToken()
{
diff --git a/dev/tests/integration/testsuite/Magento/Customer/Controller/Adminhtml/GroupTest.php b/dev/tests/integration/testsuite/Magento/Customer/Controller/Adminhtml/GroupTest.php
index b2e0a0faeee5a..c2bfd2d17ffec 100644
--- a/dev/tests/integration/testsuite/Magento/Customer/Controller/Adminhtml/GroupTest.php
+++ b/dev/tests/integration/testsuite/Magento/Customer/Controller/Adminhtml/GroupTest.php
@@ -146,7 +146,8 @@ public function testSaveActionExistingGroup()
$simpleDataObjectConverter = Bootstrap::getObjectManager()
->get('Magento\Framework\Api\SimpleDataObjectConverter');
$customerGroupData = $simpleDataObjectConverter->toFlatArray(
- $this->groupRepository->getById($groupId)
+ $this->groupRepository->getById($groupId),
+ 'Magento\Customer\Api\Data\GroupInterface'
);
ksort($customerGroupData);
diff --git a/dev/tests/integration/testsuite/Magento/Customer/Model/AccountManagementTest.php b/dev/tests/integration/testsuite/Magento/Customer/Model/AccountManagementTest.php
index dcf4887456609..515ddf30ea4b2 100755
--- a/dev/tests/integration/testsuite/Magento/Customer/Model/AccountManagementTest.php
+++ b/dev/tests/integration/testsuite/Magento/Customer/Model/AccountManagementTest.php
@@ -599,8 +599,16 @@ public function testCreateNonexistingCustomer()
'aPassword',
true
);
- $attributesBefore = $this->extensibleDataObjectConverter->toFlatArray($existingCustomer);
- $attributesAfter = $this->extensibleDataObjectConverter->toFlatArray($customerAfter);
+ $attributesBefore = $this->extensibleDataObjectConverter->toFlatArray(
+ $existingCustomer,
+ [],
+ '\Magento\Customer\Api\Data\CustomerInterface'
+ );
+ $attributesAfter = $this->extensibleDataObjectConverter->toFlatArray(
+ $customerAfter,
+ [],
+ '\Magento\Customer\Api\Data\CustomerInterface'
+ );
// ignore 'updated_at'
unset($attributesBefore['updated_at']);
unset($attributesAfter['updated_at']);
@@ -670,7 +678,10 @@ public function testCreateCustomerInServiceVsInModel()
$simpleDataObjectConverter = Bootstrap::getObjectManager()
->get('Magento\Framework\Api\SimpleDataObjectConverter');
- $dataInService = $simpleDataObjectConverter->toFlatArray($savedCustomer);
+ $dataInService = $simpleDataObjectConverter->toFlatArray(
+ $savedCustomer,
+ 'Magento\Customer\Api\Data\CustomerInterface'
+ );
$expectedDifferences = [
'created_at',
'updated_at',
diff --git a/dev/tests/integration/testsuite/Magento/Customer/Model/CustomerMetadataTest.php b/dev/tests/integration/testsuite/Magento/Customer/Model/CustomerMetadataTest.php
index 995c704225ffb..b18d1c2b598e8 100644
--- a/dev/tests/integration/testsuite/Magento/Customer/Model/CustomerMetadataTest.php
+++ b/dev/tests/integration/testsuite/Magento/Customer/Model/CustomerMetadataTest.php
@@ -120,7 +120,7 @@ public function testGetCustomerAttributeMetadata()
'id' => 1,
'website_id' => 1,
'store_id' => 1,
- 'group_id' => '1',
+ 'group_id' => 1,
'firstname' => 'John',
'lastname' => 'Smith',
'email' => 'customer@example.com',
@@ -132,7 +132,11 @@ public function testGetCustomerAttributeMetadata()
$customer = $this->customerRepository->getById(1);
$this->assertNotNull($customer);
- $attributes = $this->_extensibleDataObjectConverter->toFlatArray($customer);
+ $attributes = $this->_extensibleDataObjectConverter->toFlatArray(
+ $customer,
+ [],
+ '\Magento\Customer\Api\Data\CustomerInterface'
+ );
$this->assertNotEmpty($attributes);
foreach ($attributes as $attributeCode => $attributeValue) {
diff --git a/dev/tests/integration/testsuite/Magento/Customer/Model/Resource/CustomerRepositoryTest.php b/dev/tests/integration/testsuite/Magento/Customer/Model/Resource/CustomerRepositoryTest.php
index 09a16ec2ba0cc..7597cd2e846c0 100644
--- a/dev/tests/integration/testsuite/Magento/Customer/Model/Resource/CustomerRepositoryTest.php
+++ b/dev/tests/integration/testsuite/Magento/Customer/Model/Resource/CustomerRepositoryTest.php
@@ -141,8 +141,16 @@ public function testUpdateCustomer()
$this->assertEquals('Admin', $customerAfter->getCreatedIn());
$passwordFromFixture = 'password';
$this->accountManagement->authenticate($customerAfter->getEmail(), $passwordFromFixture);
- $attributesBefore = $this->converter->toFlatArray($customerBefore);
- $attributesAfter = $this->converter->toFlatArray($customerAfter);
+ $attributesBefore = $this->converter->toFlatArray(
+ $customerBefore,
+ [],
+ '\Magento\Customer\Api\Data\CustomerInterface'
+ );
+ $attributesAfter = $this->converter->toFlatArray(
+ $customerAfter,
+ [],
+ '\Magento\Customer\Api\Data\CustomerInterface'
+ );
// ignore 'updated_at'
unset($attributesBefore['updated_at']);
unset($attributesAfter['updated_at']);
diff --git a/dev/tests/integration/testsuite/Magento/Weee/Model/TaxTest.php b/dev/tests/integration/testsuite/Magento/Weee/Model/TaxTest.php
index b6a0a0df886ec..8a94168c895cf 100644
--- a/dev/tests/integration/testsuite/Magento/Weee/Model/TaxTest.php
+++ b/dev/tests/integration/testsuite/Magento/Weee/Model/TaxTest.php
@@ -58,7 +58,7 @@ public function testGetProductWeeeAttributes()
['metadataService' => $customerMetadataService]
);
$expected = $this->_extensibleDataObjectConverter->toFlatArray(
- $customerRepository->getById(1)
+ $customerRepository->getById(1), [], '\Magento\Customer\Api\Data\CustomerInterface'
);
$customerBuilder->populateWithArray($expected);
$customerDataSet = $customerBuilder->create();
diff --git a/dev/tests/unit/testsuite/Magento/Customer/Block/Adminhtml/Edit/Tab/AccountTest.php b/dev/tests/unit/testsuite/Magento/Customer/Block/Adminhtml/Edit/Tab/AccountTest.php
index fe8dd904a4eff..c815cf90e997e 100644
--- a/dev/tests/unit/testsuite/Magento/Customer/Block/Adminhtml/Edit/Tab/AccountTest.php
+++ b/dev/tests/unit/testsuite/Magento/Customer/Block/Adminhtml/Edit/Tab/AccountTest.php
@@ -187,6 +187,7 @@ private function _setupStoreMode($customerData, $isSingleStoreMode, $canModifyCu
'adminhtml_customer',
$this->extensibleDataObjectConverterMock->toFlatArray(
$customerObject,
+ [],
'\Magento\Customer\Api\Data\CustomerInterface'
)
)
diff --git a/dev/tests/unit/testsuite/Magento/Framework/Oauth/OauthInputExceptionTest.php b/dev/tests/unit/testsuite/Magento/Framework/Oauth/OauthInputExceptionTest.php
new file mode 100644
index 0000000000000..6ed73ee0b7f97
--- /dev/null
+++ b/dev/tests/unit/testsuite/Magento/Framework/Oauth/OauthInputExceptionTest.php
@@ -0,0 +1,29 @@
+addError(OauthInputException::REQUIRED_FIELD, ['fieldName' => $param]);
+ }
+ $exception->addError('Message with period.');
+
+ $this->assertEquals(
+ 'field1 is a required field, field2 is a required field, Message with period',
+ $exception->getAggregatedErrorMessage()
+ );
+ }
+
+ public function testGetAggregatedErrorMessageNoError()
+ {
+ $exception = new OauthInputException();
+ $this->assertEquals('', $exception->getAggregatedErrorMessage());
+ }
+}
diff --git a/dev/tests/unit/testsuite/Magento/Integration/Oauth/OauthTest.php b/dev/tests/unit/testsuite/Magento/Integration/Oauth/OauthTest.php
index 128b0723f5e14..19fb812b7d402 100644
--- a/dev/tests/unit/testsuite/Magento/Integration/Oauth/OauthTest.php
+++ b/dev/tests/unit/testsuite/Magento/Integration/Oauth/OauthTest.php
@@ -66,6 +66,7 @@ public function setUp()
'getCallbackUrl',
'save',
'getData',
+ 'isValidForTokenExchange',
'__wakeup',
]
)
@@ -122,8 +123,7 @@ public function setUp()
$tokenProvider = new \Magento\Integration\Model\Oauth\Token\Provider(
$this->_consumerFactory,
$this->_tokenFactory,
- $this->_dataHelperMock,
- $this->_dateMock
+ $this->_dataHelperMock
);
$this->_oauth = new \Magento\Framework\Oauth\Oauth(
$this->_oauthHelperMock,
@@ -218,14 +218,11 @@ public function testGetRequestTokenConsumerKeyNotFound()
public function testGetRequestTokenOutdatedConsumerKey()
{
$this->_setupConsumer();
- $this->_dateMock->expects($this->any())->method('timestamp')->will($this->returnValue(9999999999));
- $this->_dataHelperMock->expects(
- $this->once()
- )->method(
- 'getConsumerExpirationPeriod'
- )->will(
- $this->returnValue(0)
- );
+ $this->_setupNonce();
+ $this->_consumerMock
+ ->expects($this->any())
+ ->method('isValidForTokenExchange')
+ ->will($this->returnValue(false));
$this->_oauth->getRequestToken($this->_getRequestTokenParams(), self::REQUEST_URL);
}
@@ -267,14 +264,10 @@ protected function _setupConsumer($isLoadable = true)
protected function _makeValidExpirationPeriod()
{
- $this->_dateMock->expects($this->any())->method('timestamp')->will($this->returnValue(0));
- $this->_dataHelperMock->expects(
- $this->once()
- )->method(
- 'getConsumerExpirationPeriod'
- )->will(
- $this->returnValue(300)
- );
+ $this->_consumerMock
+ ->expects($this->any())
+ ->method('isValidForTokenExchange')
+ ->will($this->returnValue(true));
}
/**
@@ -530,7 +523,7 @@ public function testGetAccessTokenParameterAbsent()
/**
* \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REJECTED
*
- * @expectedException \Magento\Framework\Oauth\Exception
+ * @expectedException \Magento\Framework\Oauth\OauthInputException
*/
public function testGetAccessTokenTokenRejected()
{
diff --git a/lib/internal/Magento/Framework/Api/ExtensibleDataObjectConverter.php b/lib/internal/Magento/Framework/Api/ExtensibleDataObjectConverter.php
index 64aaa76d07589..22cad52d286c5 100644
--- a/lib/internal/Magento/Framework/Api/ExtensibleDataObjectConverter.php
+++ b/lib/internal/Magento/Framework/Api/ExtensibleDataObjectConverter.php
@@ -32,11 +32,17 @@ public function __construct(DataObjectProcessor $dataObjectProcessor)
*
* @param ExtensibleDataInterface $dataObject
* @param string[] $skipCustomAttributes
+ * @param string $dataObjectType
* @return array
*/
- public function toNestedArray(ExtensibleDataInterface $dataObject, $skipCustomAttributes = [])
- {
- $dataObjectType = get_class($dataObject);
+ public function toNestedArray(
+ ExtensibleDataInterface $dataObject,
+ $skipCustomAttributes = [],
+ $dataObjectType = null
+ ) {
+ if ($dataObjectType == null) {
+ $dataObjectType = get_class($dataObject);
+ }
$dataObjectArray = $this->dataObjectProcessor->buildOutputDataArray($dataObject, $dataObjectType);
//process custom attributes if present
if (!empty($dataObjectArray[AbstractExtensibleObject::CUSTOM_ATTRIBUTES_KEY])) {
@@ -58,11 +64,15 @@ public function toNestedArray(ExtensibleDataInterface $dataObject, $skipCustomAt
*
* @param ExtensibleDataInterface $dataObject
* @param string[] $skipCustomAttributes
+ * @param string $dataObjectType
* @return array
*/
- public function toFlatArray(ExtensibleDataInterface $dataObject, $skipCustomAttributes = [])
- {
- $dataObjectArray = $this->toNestedArray($dataObject, $skipCustomAttributes);
+ public function toFlatArray(
+ ExtensibleDataInterface $dataObject,
+ $skipCustomAttributes = [],
+ $dataObjectType = null
+ ) {
+ $dataObjectArray = $this->toNestedArray($dataObject, $skipCustomAttributes, $dataObjectType);
return ConvertArray::toFlatArray($dataObjectArray);
}
diff --git a/lib/internal/Magento/Framework/Api/SimpleDataObjectConverter.php b/lib/internal/Magento/Framework/Api/SimpleDataObjectConverter.php
index 4dec0a5f2d322..47e40937f0c95 100644
--- a/lib/internal/Magento/Framework/Api/SimpleDataObjectConverter.php
+++ b/lib/internal/Magento/Framework/Api/SimpleDataObjectConverter.php
@@ -27,11 +27,14 @@ public function __construct(DataObjectProcessor $dataObjectProcessor)
* Convert nested array into flat array.
*
* @param ExtensibleDataInterface $dataObject
+ * @param string $dataObjectType
* @return array
*/
- public function toFlatArray(ExtensibleDataInterface $dataObject)
+ public function toFlatArray(ExtensibleDataInterface $dataObject, $dataObjectType = null)
{
- $dataObjectType = get_class($dataObject);
+ if ($dataObjectType === null) {
+ $dataObjectType = get_class($dataObject);
+ }
$data = $this->dataObjectProcessor->buildOutputDataArray($dataObject, $dataObjectType);
return ConvertArray::toFlatArray($data);
}
diff --git a/lib/internal/Magento/Framework/Oauth/ConsumerInterface.php b/lib/internal/Magento/Framework/Oauth/ConsumerInterface.php
index 9b9dda33c75a2..a6bab9c9dff4a 100644
--- a/lib/internal/Magento/Framework/Oauth/ConsumerInterface.php
+++ b/lib/internal/Magento/Framework/Oauth/ConsumerInterface.php
@@ -55,4 +55,11 @@ public function getCallbackUrl();
* @return string
*/
public function getCreatedAt();
+
+ /**
+ * Check if the consumer key has not expired for Oauth token exchange usage
+ *
+ * @return bool
+ */
+ public function isValidForTokenExchange();
}
diff --git a/lib/internal/Magento/Framework/Oauth/Helper/Request.php b/lib/internal/Magento/Framework/Oauth/Helper/Request.php
index 84051adfc4e48..cf7dc348230bc 100644
--- a/lib/internal/Magento/Framework/Oauth/Helper/Request.php
+++ b/lib/internal/Magento/Framework/Oauth/Helper/Request.php
@@ -197,6 +197,9 @@ public function prepareErrorResponse(\Exception $exception, \Zend_Controller_Res
$responseCode = self::HTTP_UNAUTHORIZED;
} elseif ($exception instanceof \Magento\Framework\Oauth\OauthInputException) {
$responseCode = self::HTTP_BAD_REQUEST;
+ if ($errorMsg == \Magento\Framework\Oauth\OauthInputException::DEFAULT_MESSAGE) {
+ $errorMsg = $exception->getAggregatedErrorMessage();
+ }
} else {
$errorMsg = 'internal_error&message=' . ($errorMsg ? $errorMsg : 'empty_message');
$responseCode = self::HTTP_INTERNAL_ERROR;
diff --git a/lib/internal/Magento/Framework/Oauth/Oauth.php b/lib/internal/Magento/Framework/Oauth/Oauth.php
index dbdae85be1fde..88c2392922221 100644
--- a/lib/internal/Magento/Framework/Oauth/Oauth.php
+++ b/lib/internal/Magento/Framework/Oauth/Oauth.php
@@ -61,11 +61,9 @@ public static function getSupportedSignatureMethods()
*/
public function getRequestToken($params, $requestUrl, $httpMethod = 'POST')
{
- $this->_validateVersionParam($params['oauth_version']);
+ $this->_validateProtocolParams($params);
$consumer = $this->_tokenProvider->getConsumerByKey($params['oauth_consumer_key']);
$this->_tokenProvider->validateConsumer($consumer);
- $this->_nonceGenerator->validateNonce($consumer, $params['oauth_nonce'], $params['oauth_timestamp']);
-
$this->_validateSignature($params, $consumer->getSecret(), $httpMethod, $requestUrl);
return $this->_tokenProvider->createRequestToken($consumer);
@@ -220,9 +218,9 @@ protected function _validateVersionParam($version)
* @param array $protocolParams
* @param array $requiredParams
* @return void
- * @throws Exception|OauthInputException
+ * @throws OauthInputException
*/
- protected function _validateProtocolParams($protocolParams, $requiredParams)
+ protected function _validateProtocolParams($protocolParams, $requiredParams = [])
{
// validate version if specified.
if (isset($protocolParams['oauth_version'])) {
@@ -247,7 +245,7 @@ protected function _validateProtocolParams($protocolParams, $requiredParams)
$protocolParams['oauth_token']
)
) {
- throw new Exception('Token is not the correct length');
+ throw new OauthInputException('Token is not the correct length');
}
// Validate signature method.
@@ -276,10 +274,14 @@ protected function _validateProtocolParams($protocolParams, $requiredParams)
*/
protected function _checkRequiredParams($protocolParams, $requiredParams)
{
+ $exception = new OauthInputException();
foreach ($requiredParams as $param) {
if (!isset($protocolParams[$param])) {
- throw new OauthInputException(OauthInputException::REQUIRED_FIELD, ['fieldName' => $param]);
+ $exception->addError(OauthInputException::REQUIRED_FIELD, ['fieldName' => $param]);
}
}
+ if ($exception->wasErrorAdded()) {
+ throw $exception;
+ }
}
}
diff --git a/lib/internal/Magento/Framework/Oauth/OauthInputException.php b/lib/internal/Magento/Framework/Oauth/OauthInputException.php
index e568bab6c69e2..48410e27c8b6f 100644
--- a/lib/internal/Magento/Framework/Oauth/OauthInputException.php
+++ b/lib/internal/Magento/Framework/Oauth/OauthInputException.php
@@ -13,4 +13,22 @@
*/
class OauthInputException extends InputException
{
+ /**
+ * Get error messages as a single comma separated string
+ *
+ * @return string
+ */
+ public function getAggregatedErrorMessage()
+ {
+ $errors = [];
+ foreach ($this->getErrors() as $error) {
+ // Clean up any trailing period
+ $errors[] = rtrim($error->getMessage(), '.');
+ }
+ $errorMsg = '';
+ if (!empty($errors)) {
+ $errorMsg = implode(', ', $errors);
+ }
+ return $errorMsg;
+ }
}