PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/hwi/oauth-bundle/OAuth/ResourceOwner/GenericOAuth1ResourceOwner.php

https://gitlab.com/Snizer/PI-DEV-TUNISIAMALL3A6-WEB
PHP | 244 lines | 159 code | 41 blank | 44 comment | 13 complexity | c2806044c3dff24226e178f21db9aa09 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the HWIOAuthBundle package.
  4. *
  5. * (c) Hardware.Info <opensource@hardware.info>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace HWI\Bundle\OAuthBundle\OAuth\ResourceOwner;
  11. use Buzz\Message\RequestInterface as HttpRequestInterface;
  12. use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
  13. use HWI\Bundle\OAuthBundle\Security\OAuthUtils;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  17. /**
  18. * GenericOAuth1ResourceOwner
  19. *
  20. * @author Francisco Facioni <fran6co@gmail.com>
  21. */
  22. class GenericOAuth1ResourceOwner extends AbstractResourceOwner
  23. {
  24. /**
  25. * {@inheritDoc}
  26. */
  27. public function getUserInformation(array $accessToken, array $extraParameters = array())
  28. {
  29. $parameters = array_merge(array(
  30. 'oauth_consumer_key' => $this->options['client_id'],
  31. 'oauth_timestamp' => time(),
  32. 'oauth_nonce' => $this->generateNonce(),
  33. 'oauth_version' => '1.0',
  34. 'oauth_signature_method' => $this->options['signature_method'],
  35. 'oauth_token' => $accessToken['oauth_token'],
  36. ), $extraParameters);
  37. $url = $this->options['infos_url'];
  38. $parameters['oauth_signature'] = OAuthUtils::signRequest(
  39. HttpRequestInterface::METHOD_GET,
  40. $url,
  41. $parameters,
  42. $this->options['client_secret'],
  43. $accessToken['oauth_token_secret'],
  44. $this->options['signature_method']
  45. );
  46. $content = $this->doGetUserInformationRequest($url, $parameters)->getContent();
  47. $response = $this->getUserResponse();
  48. $response->setResponse($content);
  49. $response->setResourceOwner($this);
  50. $response->setOAuthToken(new OAuthToken($accessToken));
  51. return $response;
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. public function getAuthorizationUrl($redirectUri, array $extraParameters = array())
  57. {
  58. $token = $this->getRequestToken($redirectUri, $extraParameters);
  59. return $this->normalizeUrl($this->options['authorization_url'], array('oauth_token' => $token['oauth_token']));
  60. }
  61. /**
  62. * {@inheritDoc}
  63. */
  64. public function getAccessToken(Request $request, $redirectUri, array $extraParameters = array())
  65. {
  66. try {
  67. if (null === $requestToken = $this->storage->fetch($this, $request->query->get('oauth_token'))) {
  68. throw new \RuntimeException('No request token found in the storage.');
  69. }
  70. } catch (\InvalidArgumentException $e) {
  71. throw new AuthenticationException('Given token is not valid.');
  72. }
  73. $parameters = array_merge(array(
  74. 'oauth_consumer_key' => $this->options['client_id'],
  75. 'oauth_timestamp' => time(),
  76. 'oauth_nonce' => $this->generateNonce(),
  77. 'oauth_version' => '1.0',
  78. 'oauth_signature_method' => $this->options['signature_method'],
  79. 'oauth_token' => $requestToken['oauth_token'],
  80. 'oauth_verifier' => $request->query->get('oauth_verifier'),
  81. ), $extraParameters);
  82. $url = $this->options['access_token_url'];
  83. $parameters['oauth_signature'] = OAuthUtils::signRequest(
  84. HttpRequestInterface::METHOD_POST,
  85. $url,
  86. $parameters,
  87. $this->options['client_secret'],
  88. $requestToken['oauth_token_secret'],
  89. $this->options['signature_method']
  90. );
  91. $response = $this->doGetTokenRequest($url, $parameters);
  92. $response = $this->getResponseContent($response);
  93. if (isset($response['oauth_problem'])) {
  94. throw new AuthenticationException(sprintf('OAuth error: "%s"', $response['oauth_problem']));
  95. }
  96. if (!isset($response['oauth_token']) || !isset($response['oauth_token_secret'])) {
  97. throw new AuthenticationException('Not a valid request token.');
  98. }
  99. return $response;
  100. }
  101. /**
  102. * {@inheritDoc}
  103. */
  104. public function handles(Request $request)
  105. {
  106. return $request->query->has('oauth_token');
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function isCsrfTokenValid($csrfToken)
  112. {
  113. // OAuth1.0a passes token with every call
  114. return true;
  115. }
  116. /**
  117. * {@inheritDoc}
  118. */
  119. public function getRequestToken($redirectUri, array $extraParameters = array())
  120. {
  121. $timestamp = time();
  122. $parameters = array_merge(array(
  123. 'oauth_consumer_key' => $this->options['client_id'],
  124. 'oauth_timestamp' => $timestamp,
  125. 'oauth_nonce' => $this->generateNonce(),
  126. 'oauth_version' => '1.0',
  127. 'oauth_callback' => $redirectUri,
  128. 'oauth_signature_method' => $this->options['signature_method'],
  129. ), $extraParameters);
  130. $url = $this->options['request_token_url'];
  131. $parameters['oauth_signature'] = OAuthUtils::signRequest(
  132. HttpRequestInterface::METHOD_POST,
  133. $url,
  134. $parameters,
  135. $this->options['client_secret'],
  136. '',
  137. $this->options['signature_method']
  138. );
  139. $apiResponse = $this->httpRequest($url, null, $parameters, array(), HttpRequestInterface::METHOD_POST);
  140. $response = $this->getResponseContent($apiResponse);
  141. if (isset($response['oauth_problem'])) {
  142. throw new AuthenticationException(sprintf('OAuth error: "%s"', $response['oauth_problem']));
  143. }
  144. if (isset($response['oauth_callback_confirmed']) && ($response['oauth_callback_confirmed'] != 'true')) {
  145. throw new AuthenticationException('Defined OAuth callback was not confirmed.');
  146. }
  147. if (!isset($response['oauth_token']) || !isset($response['oauth_token_secret'])) {
  148. throw new AuthenticationException('Not a valid request token.');
  149. }
  150. $response['timestamp'] = $timestamp;
  151. $this->storage->save($this, $response);
  152. return $response;
  153. }
  154. /**
  155. * {@inheritDoc}
  156. */
  157. protected function httpRequest($url, $content = null, $parameters = array(), $headers = array(), $method = null)
  158. {
  159. foreach ($parameters as $key => $value) {
  160. $parameters[$key] = $key . '="' . rawurlencode($value) . '"';
  161. }
  162. if (!$this->options['realm']) {
  163. array_unshift($parameters, 'realm="' . rawurlencode($this->options['realm']) . '"');
  164. }
  165. $headers[] = 'Authorization: OAuth ' . implode(', ', $parameters);
  166. return parent::httpRequest($url, $content, $headers, $method);
  167. }
  168. /**
  169. * {@inheritDoc}
  170. */
  171. protected function doGetTokenRequest($url, array $parameters = array())
  172. {
  173. return $this->httpRequest($url, null, $parameters, array(), HttpRequestInterface::METHOD_POST);
  174. }
  175. /**
  176. * {@inheritDoc}
  177. */
  178. protected function doGetUserInformationRequest($url, array $parameters = array())
  179. {
  180. return $this->httpRequest($url, null, $parameters);
  181. }
  182. /**
  183. * {@inheritDoc}
  184. */
  185. protected function configureOptions(OptionsResolverInterface $resolver)
  186. {
  187. parent::configureOptions($resolver);
  188. $resolver->setRequired(array(
  189. 'request_token_url',
  190. ));
  191. $resolver->setDefaults(array(
  192. 'realm' => null,
  193. 'signature_method' => 'HMAC-SHA1',
  194. ));
  195. if (method_exists($resolver, 'setDefined')) {
  196. $resolver->setAllowedValues('signature_method', array('HMAC-SHA1', 'RSA-SHA1', 'PLAINTEXT'));
  197. } else {
  198. $resolver->setAllowedValues(array(
  199. 'signature_method' => array('HMAC-SHA1', 'RSA-SHA1', 'PLAINTEXT'),
  200. ));
  201. }
  202. }
  203. }