Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OauthHelper.php
Go to the documentation of this file.
1 <?php
9 
12 use OAuth\Common\Consumer\Credentials;
13 use Zend\Stdlib\Exception\LogicException;
15 
17 {
19  protected static $_apiCredentials;
20 
35  public static function getConsumerCredentials($date = null)
36  {
37  $integration = self::_createIntegration('all');
40  $oauthService = $objectManager->get(\Magento\Integration\Api\OauthServiceInterface::class);
41  $consumer = $oauthService->loadConsumer($integration->getConsumerId());
42  $url = TESTS_BASE_URL;
43  $consumer->setCallbackUrl($url);
44  $consumer->setRejectedCallbackUrl($url);
45  if ($date !== null) {
46  $consumer->setCreatedAt($date);
47  }
48  $consumer->save();
49  $token = $objectManager->create(\Magento\Integration\Model\Oauth\Token::class);
50  $verifier = $token->createVerifierToken($consumer->getId())->getVerifier();
51 
52  return [
53  'key' => $consumer->getKey(),
54  'secret' => $consumer->getSecret(),
55  'verifier' => $verifier,
56  'consumer' => $consumer,
57  'token' => $token
58  ];
59  }
60 
73  public static function getAccessToken()
74  {
75  $consumerCredentials = self::getConsumerCredentials();
76  $credentials = new Credentials($consumerCredentials['key'], $consumerCredentials['secret'], TESTS_BASE_URL);
77  $oAuthClient = new OauthClient($credentials);
78  $requestToken = $oAuthClient->requestRequestToken();
79  $accessToken = $oAuthClient->requestAccessToken(
80  $requestToken->getRequestToken(),
81  $consumerCredentials['verifier'],
82  $requestToken->getRequestTokenSecret()
83  );
84 
86  return [
87  'key' => $accessToken->getAccessToken(),
88  'secret' => $accessToken->getAccessTokenSecret(),
89  'oauth_client' => $oAuthClient
90  ];
91  }
92 
109  public static function getApiAccessCredentials($resources = null, Integration $integrationModel = null)
110  {
111  if (!self::$_apiCredentials) {
112  $integration = $integrationModel === null ? self::_createIntegration($resources) : $integrationModel;
115  $oauthService = $objectManager->get(\Magento\Integration\Api\OauthServiceInterface::class);
116  $oauthService->createAccessToken($integration->getConsumerId());
117  $accessToken = $oauthService->getAccessToken($integration->getConsumerId());
118  if (!$accessToken) {
119  throw new LogicException('Access token was not created.');
120  }
121  $consumer = $oauthService->loadConsumer($integration->getConsumerId());
122  $credentials = new Credentials($consumer->getKey(), $consumer->getSecret(), TESTS_BASE_URL);
124  $oAuthClient = new OauthClient($credentials);
125 
126  self::$_apiCredentials = [
127  'key' => $accessToken->getToken(),
128  'secret' => $accessToken->getSecret(),
129  'oauth_client' => $oAuthClient,
130  'integration' => $integration,
131  ];
132  }
133  return self::$_apiCredentials;
134  }
135 
139  public static function clearApiAccessCredentials()
140  {
141  self::$_apiCredentials = false;
142  }
143 
150  protected static function _rmRecursive($dir, $doSaveRoot = false)
151  {
152  if (is_dir($dir)) {
153  foreach (glob($dir . '/*') as $object) {
154  if (is_dir($object)) {
155  self::_rmRecursive($object);
156  } else {
157  unlink($object);
158  }
159  }
160  if (!$doSaveRoot) {
161  rmdir($dir);
162  }
163  } else {
164  unlink($dir);
165  }
166  }
167 
175  protected static function _createIntegration($resources)
176  {
179  $integrationService = $objectManager->get(\Magento\Integration\Api\IntegrationServiceInterface::class);
180 
181  $params = ['name' => 'Integration' . microtime()];
182 
183  if ($resources === null || $resources == 'all') {
184  $params['all_resources'] = true;
185  } else {
186  $params['resource'] = $resources;
187  }
188  $integration = $integrationService->create($params);
189  $integration->setStatus(\Magento\Integration\Model\Integration::STATUS_ACTIVE)->save();
190 
192  $varPath = realpath(BP . '/var');
193  if (!$varPath) {
194  throw new LogicException("Magento cache cannot be cleared after new ACL role creation.");
195  } else {
196  $cachePath = $varPath . '/cache';
197  if (is_dir($cachePath)) {
198  self::_rmRecursive($cachePath, true);
199  }
200  }
201  return $integration;
202  }
203 }
$objectManager
Definition: bootstrap.php:17
const BP
Definition: autoload.php:14
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
static _rmRecursive($dir, $doSaveRoot=false)