1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Integration\Model;
use Magento\Framework\Oauth\Helper\Oauth as OauthHelper;
use Magento\Integration\Helper\Oauth\Data as IntegrationOauthHelper;
use Magento\Integration\Model\Oauth\Consumer as ConsumerModel;
use Magento\Integration\Model\Oauth\ConsumerFactory;
use Magento\Integration\Model\Oauth\Token as OauthTokenModel;
use Magento\Integration\Model\Oauth\TokenFactory as TokenFactory;
use Magento\Integration\Model\Oauth\Token\Provider as TokenProvider;
use Magento\Framework\Exception\IntegrationException;
/**
* Integration oAuth service.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class OauthService implements \Magento\Integration\Api\OauthServiceInterface
{
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
/**
* @var ConsumerFactory
*/
protected $_consumerFactory;
/**
* @var TokenFactory
*/
protected $_tokenFactory;
/**
* @var IntegrationOauthHelper
*/
protected $_dataHelper;
/**
* @var \Magento\Framework\HTTP\ZendClient
*/
protected $_httpClient;
/**
* @var \Psr\Log\LoggerInterface
*/
protected $_logger;
/**
* @var OauthHelper
*/
protected $_oauthHelper;
/**
* @var TokenProvider
*/
protected $_tokenProvider;
/**
* @var \Magento\Framework\Stdlib\DateTime\DateTime
*/
private $_dateHelper;
/**
* Initialize dependencies.
*
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param ConsumerFactory $consumerFactory
* @param TokenFactory $tokenFactory
* @param IntegrationOauthHelper $dataHelper
* @param \Magento\Framework\HTTP\ZendClient $httpClient
* @param \Psr\Log\LoggerInterface $logger
* @param OauthHelper $oauthHelper
* @param TokenProvider $tokenProvider
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
ConsumerFactory $consumerFactory,
TokenFactory $tokenFactory,
IntegrationOauthHelper $dataHelper,
\Magento\Framework\HTTP\ZendClient $httpClient,
\Psr\Log\LoggerInterface $logger,
OauthHelper $oauthHelper,
TokenProvider $tokenProvider
) {
$this->_storeManager = $storeManager;
$this->_consumerFactory = $consumerFactory;
$this->_tokenFactory = $tokenFactory;
$this->_dataHelper = $dataHelper;
$this->_httpClient = $httpClient;
$this->_logger = $logger;
$this->_oauthHelper = $oauthHelper;
$this->_tokenProvider = $tokenProvider;
}
/**
* The getter function to get the new DateTime dependency
*
* @return \Magento\Framework\Stdlib\DateTime\DateTime
*
* @deprecated 100.0.6
*/
private function getDateHelper()
{
if ($this->_dateHelper === null) {
$this->_dateHelper = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Stdlib\DateTime\DateTime::class);
}
return $this->_dateHelper;
}
/**
* {@inheritdoc}
*/
public function createConsumer($consumerData)
{
try {
$consumerData['key'] = $this->_oauthHelper->generateConsumerKey();
$consumerData['secret'] = $this->_oauthHelper->generateConsumerSecret();
$consumer = $this->_consumerFactory->create()->setData($consumerData);
$consumer->save();
return $consumer;
} catch (\Magento\Framework\Exception\LocalizedException $exception) {
throw $exception;
} catch (\Exception $exception) {
throw new \Magento\Framework\Oauth\Exception(
__(
"The oAuth consumer account couldn't be created due to an unexpected error. Please try again later."
)
);
}
}
/**
* {@inheritdoc}
*/
public function createAccessToken($consumerId, $clearExistingToken = false)
{
try {
$consumer = $this->_consumerFactory->create()->load($consumerId);
$existingToken = $this->_tokenProvider->getIntegrationTokenByConsumerId($consumer->getId());
if ($existingToken && $clearExistingToken) {
$existingToken->delete();
unset($existingToken);
}
} catch (\Exception $e) {
}
if (!isset($existingToken)) {
$consumer = $this->_consumerFactory->create()->load($consumerId);
$this->_tokenFactory->create()->createVerifierToken($consumerId);
$this->_tokenProvider->createRequestToken($consumer);
$this->_tokenProvider->getAccessToken($consumer);
return true;
}
return false;
}
/**
* {@inheritdoc}
*/
public function getAccessToken($consumerId)
{
try {
$consumer = $this->_consumerFactory->create()->load($consumerId);
$token = $this->_tokenProvider->getIntegrationTokenByConsumerId($consumer->getId());
if ($token->getType() != OauthTokenModel::TYPE_ACCESS) {
return false;
}
} catch (\Exception $e) {
return false;
}
return $token;
}
/**
* {@inheritdoc}
*/
public function loadConsumer($consumerId)
{
try {
return $this->_consumerFactory->create()->load($consumerId);
} catch (\Magento\Framework\Exception\LocalizedException $exception) {
throw $exception;
} catch (\Exception $exception) {
throw new \Magento\Framework\Oauth\Exception(
__("The oAuth consumer account couldn't be loaded due to an unexpected error. Please try again later.")
);
}
}
/**
* {@inheritdoc}
*/
public function loadConsumerByKey($key)
{
try {
return $this->_consumerFactory->create()->load($key, 'key');
} catch (\Magento\Framework\Exception\LocalizedException $exception) {
throw $exception;
} catch (\Exception $exception) {
throw new \Magento\Framework\Oauth\Exception(
__("The oAuth consumer account couldn't be loaded due to an unexpected error. Please try again later.")
);
}
}
/**
* {@inheritdoc}
*/
public function postToConsumer($consumerId, $endpointUrl)
{
try {
$consumer = $this->loadConsumer($consumerId);
$consumer->setUpdatedAt($this->getDateHelper()->gmtDate());
$consumer->save();
if (!$consumer->getId()) {
throw new \Magento\Framework\Oauth\Exception(
__('A consumer with "%1" ID doesn\'t exist. Verify the ID and try again.', $consumerId)
);
}
$consumerData = $consumer->getData();
$verifier = $this->_tokenFactory->create()->createVerifierToken($consumerId);
$storeBaseUrl = $this->_storeManager->getStore()->getBaseUrl();
$this->_httpClient->setUri($endpointUrl);
$this->_httpClient->setParameterPost(
[
'oauth_consumer_key' => $consumerData['key'],
'oauth_consumer_secret' => $consumerData['secret'],
'store_base_url' => $storeBaseUrl,
'oauth_verifier' => $verifier->getVerifier(),
]
);
$maxredirects = $this->_dataHelper->getConsumerPostMaxRedirects();
$timeout = $this->_dataHelper->getConsumerPostTimeout();
$this->_httpClient->setConfig(['maxredirects' => $maxredirects, 'timeout' => $timeout]);
$this->_httpClient->request(\Magento\Framework\HTTP\ZendClient::POST);
return $verifier->getVerifier();
} catch (\Magento\Framework\Exception\LocalizedException $exception) {
throw $exception;
} catch (\Magento\Framework\Oauth\Exception $exception) {
throw $exception;
} catch (\Exception $exception) {
$this->_logger->critical($exception);
throw new \Magento\Framework\Oauth\Exception(
__('The attempt to post data to consumer failed due to an unexpected error. Please try again later.')
);
}
}
/**
* {@inheritdoc}
*/
public function deleteConsumer($consumerId)
{
$consumer = $this->_loadConsumerById($consumerId);
$data = $consumer->getData();
$consumer->delete();
return $data;
}
/**
* {@inheritdoc}
*/
public function deleteIntegrationToken($consumerId)
{
try {
$consumer = $this->_consumerFactory->create()->load($consumerId);
$existingToken = $this->_tokenProvider->getIntegrationTokenByConsumerId($consumer->getId());
$existingToken->delete();
return true;
} catch (\Exception $e) {
return false;
}
}
/**
* Load consumer by id.
*
* @param int $consumerId
* @return ConsumerModel
* @throws \Magento\Framework\Exception\IntegrationException
*/
protected function _loadConsumerById($consumerId)
{
$consumer = $this->_consumerFactory->create()->load($consumerId);
if (!$consumer->getId()) {
throw new IntegrationException(
__('A consumer with ID "%1" doesn\'t exist. Verify the ID and try again.', $consumerId)
);
}
return $consumer;
}
}