Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Provider.php
Go to the documentation of this file.
1 <?php
8 
13 
15 {
19  protected $_consumerFactory;
20 
24  protected $_tokenFactory;
25 
29  protected $logger;
30 
36  public function __construct(
37  \Magento\Integration\Model\Oauth\ConsumerFactory $consumerFactory,
38  \Magento\Integration\Model\Oauth\TokenFactory $tokenFactory,
39  \Psr\Log\LoggerInterface $logger
40  ) {
41  $this->_consumerFactory = $consumerFactory;
42  $this->_tokenFactory = $tokenFactory;
43  $this->logger = $logger;
44  }
45 
49  public function validateConsumer($consumer)
50  {
51  // Must use consumer within expiration period.
52  if (!$consumer->isValidForTokenExchange()) {
53  throw new \Magento\Framework\Oauth\Exception(
54  __('Consumer key has expired')
55  );
56  }
57  return true;
58  }
59 
63  public function createRequestToken($consumer)
64  {
65  $token = $this->getIntegrationTokenByConsumerId($consumer->getId());
66  if ($token->getType() != Token::TYPE_VERIFIER) {
67  throw new \Magento\Framework\Oauth\Exception(
68  __('Cannot create request token because consumer token is not a verifier token')
69  );
70  }
71  $requestToken = $token->createRequestToken($token->getId(), $consumer->getCallbackUrl());
72  return ['oauth_token' => $requestToken->getToken(), 'oauth_token_secret' => $requestToken->getSecret()];
73  }
74 
78  public function validateRequestToken($requestToken, $consumer, $oauthVerifier)
79  {
80  $token = $this->_getToken($requestToken);
81 
82  if (!$this->_isTokenAssociatedToConsumer($token, $consumer)) {
83  throw new \Magento\Framework\Oauth\Exception(
84  __('Request token is not associated with the specified consumer')
85  );
86  }
87 
88  // The pre-auth token has a value of "request" in the type when it is requested and created initially.
89  // In this flow (token flow) the token has to be of type "request" else its marked as reused.
90  if (Token::TYPE_REQUEST != $token->getType()) {
91  throw new \Magento\Framework\Oauth\Exception(
92  __('Token is already being used')
93  );
94  }
95 
96  $this->_validateVerifierParam($oauthVerifier, $token->getVerifier());
97 
98  return $token->getSecret();
99  }
100 
104  public function getAccessToken($consumer)
105  {
106  $consumerId = $consumer->getId();
107  $token = $this->getIntegrationTokenByConsumerId($consumerId);
108  if (Token::TYPE_REQUEST != $token->getType()) {
109  throw new \Magento\Framework\Oauth\Exception(
110  __('Cannot get access token because consumer token is not a request token')
111  );
112  }
113  $accessToken = $token->convertToAccess();
114  $this->logger->info(
115  'Request token ' . $token->getToken() . ' was exchanged to obtain access token for consumer ' . $consumerId
116  );
117  return ['oauth_token' => $accessToken->getToken(), 'oauth_token_secret' => $accessToken->getSecret()];
118  }
119 
123  public function validateAccessTokenRequest($accessToken, $consumer)
124  {
125  $token = $this->_getToken($accessToken);
126 
127  if (!$this->_isTokenAssociatedToConsumer($token, $consumer)) {
128  throw new \Magento\Framework\Oauth\Exception(
129  __('Token is not associated with the specified consumer')
130  );
131  }
132  if (Token::TYPE_ACCESS != $token->getType()) {
133  throw new \Magento\Framework\Oauth\Exception(
134  __('Token is not an access token')
135  );
136  }
137  if ($token->getRevoked()) {
138  throw new \Magento\Framework\Oauth\Exception(
139  __('Access token has been revoked')
140  );
141  }
142 
143  return $token->getSecret();
144  }
145 
149  public function validateAccessToken($accessToken)
150  {
151  $token = $this->_getToken($accessToken);
152  // Make sure a consumer is associated with the token.
153  $this->_getConsumer($token->getConsumerId());
154 
155  if (Token::TYPE_ACCESS != $token->getType()) {
156  throw new \Magento\Framework\Oauth\Exception(
157  __('Token is not an access token')
158  );
159  }
160 
161  if ($token->getRevoked()) {
162  throw new \Magento\Framework\Oauth\Exception(
163  __('Access token has been revoked')
164  );
165  }
166 
167  return $token->getConsumerId();
168  }
169 
173  public function validateOauthToken($oauthToken)
174  {
175  return strlen($oauthToken) == \Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN;
176  }
177 
181  public function getConsumerByKey($consumerKey)
182  {
183  if (strlen($consumerKey) != \Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_KEY) {
184  throw new \Magento\Framework\Oauth\Exception(
185  __('Consumer key is not the correct length')
186  );
187  }
188 
189  $consumer = $this->_consumerFactory->create()->loadByKey($consumerKey);
190 
191  if (!$consumer->getId()) {
192  throw new \Magento\Framework\Oauth\Exception(
193  __('A consumer having the specified key does not exist')
194  );
195  }
196 
197  return $consumer;
198  }
199 
208  protected function _validateVerifierParam($oauthVerifier, $tokenVerifier)
209  {
210  if (!is_string($oauthVerifier)) {
211  throw new \Magento\Framework\Oauth\Exception(
212  __('Verifier is invalid')
213  );
214  }
215  if (!$this->validateOauthToken($oauthVerifier)) {
216  throw new \Magento\Framework\Oauth\Exception(
217  __('Verifier is not the correct length')
218  );
219  }
220  if (!Security::compareStrings($tokenVerifier, $oauthVerifier)) {
221  throw new \Magento\Framework\Oauth\Exception(
222  __('Token verifier and verifier token do not match')
223  );
224  }
225  }
226 
234  protected function _getConsumer($consumerId)
235  {
236  $consumer = $this->_consumerFactory->create()->load($consumerId);
237 
238  if (!$consumer->getId()) {
239  throw new \Magento\Framework\Oauth\Exception(
240  __(
241  'A consumer with the ID %1 does not exist',
242  [$consumerId]
243  )
244  );
245  }
246 
247  return $consumer;
248  }
249 
257  protected function _getToken($token)
258  {
259  if (!$this->validateOauthToken($token)) {
260  throw new \Magento\Framework\Oauth\Exception(
261  __('The token length is invalid. Check the length and try again.')
262  );
263  }
264 
265  $tokenObj = $this->_tokenFactory->create()->load($token, 'token');
266 
267  if (!$tokenObj->getId()) {
268  throw new \Magento\Framework\Oauth\Exception(
269  __('Specified token does not exist')
270  );
271  }
272 
273  return $tokenObj;
274  }
275 
283  public function getIntegrationTokenByConsumerId($consumerId)
284  {
286  $token = $this->_tokenFactory->create();
287  $token->loadByConsumerIdAndUserType($consumerId, UserContextInterface::USER_TYPE_INTEGRATION);
288 
289  if (!$token->getId()) {
290  throw new \Magento\Framework\Oauth\Exception(
291  __(
292  'A token with consumer ID %1 does not exist',
293  [$consumerId]
294  )
295  );
296  }
297 
298  return $token;
299  }
300 
308  protected function _isTokenAssociatedToConsumer($token, $consumer)
309  {
310  return $token->getConsumerId() == $consumer->getId();
311  }
312 }
__()
Definition: __.php:13
_validateVerifierParam($oauthVerifier, $tokenVerifier)
Definition: Provider.php:208
__construct(\Magento\Integration\Model\Oauth\ConsumerFactory $consumerFactory, \Magento\Integration\Model\Oauth\TokenFactory $tokenFactory, \Psr\Log\LoggerInterface $logger)
Definition: Provider.php:36
validateAccessTokenRequest($accessToken, $consumer)
Definition: Provider.php:123
validateRequestToken($requestToken, $consumer, $oauthVerifier)
Definition: Provider.php:78