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

Open in your IDE?
  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 HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
  12. use HWI\Bundle\OAuthBundle\Security\Helper\NonceGenerator;
  13. use HWI\Bundle\OAuthBundle\Security\OAuthErrorHandler;
  14. use HWI\Bundle\OAuthBundle\Security\OAuthUtils;
  15. use Psr\Http\Message\ResponseInterface;
  16. use Symfony\Component\HttpFoundation\Request as HttpRequest;
  17. use Symfony\Component\OptionsResolver\OptionsResolver;
  18. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  19. /**
  20.  * GenericOAuth1ResourceOwner.
  21.  *
  22.  * @author Francisco Facioni <fran6co@gmail.com>
  23.  */
  24. class GenericOAuth1ResourceOwner extends AbstractResourceOwner
  25. {
  26.     /**
  27.      * {@inheritdoc}
  28.      */
  29.     public function getUserInformation(array $accessToken, array $extraParameters = [])
  30.     {
  31.         $parameters array_merge([
  32.             'oauth_consumer_key' => $this->options['client_id'],
  33.             'oauth_timestamp' => time(),
  34.             'oauth_nonce' => NonceGenerator::generate(),
  35.             'oauth_version' => '1.0',
  36.             'oauth_signature_method' => $this->options['signature_method'],
  37.             'oauth_token' => $accessToken['oauth_token'],
  38.         ], $extraParameters);
  39.         $url $this->options['infos_url'];
  40.         $parameters['oauth_signature'] = OAuthUtils::signRequest(
  41.             'GET',
  42.             $url,
  43.             $parameters,
  44.             $this->options['client_secret'],
  45.             $accessToken['oauth_token_secret'],
  46.             $this->options['signature_method']
  47.         );
  48.         $content $this->doGetUserInformationRequest($url$parameters);
  49.         $response $this->getUserResponse();
  50.         $response->setData($content instanceof ResponseInterface ? (string) $content->getBody() : $content);
  51.         $response->setResourceOwner($this);
  52.         $response->setOAuthToken(new OAuthToken($accessToken));
  53.         return $response;
  54.     }
  55.     /**
  56.      * {@inheritdoc}
  57.      */
  58.     public function getAuthorizationUrl($redirectUri, array $extraParameters = [])
  59.     {
  60.         $token $this->getRequestToken($redirectUri$extraParameters);
  61.         return $this->normalizeUrl($this->options['authorization_url'], ['oauth_token' => $token['oauth_token']]);
  62.     }
  63.     /**
  64.      * {@inheritdoc}
  65.      */
  66.     public function getAccessToken(HttpRequest $request$redirectUri, array $extraParameters = [])
  67.     {
  68.         OAuthErrorHandler::handleOAuthError($request);
  69.         try {
  70.             if (null === $requestToken $this->storage->fetch($this$request->query->get('oauth_token'))) {
  71.                 throw new \RuntimeException('No request token found in the storage.');
  72.             }
  73.         } catch (\InvalidArgumentException $e) {
  74.             throw new AuthenticationException('Given token is not valid.');
  75.         }
  76.         $parameters array_merge([
  77.             'oauth_consumer_key' => $this->options['client_id'],
  78.             'oauth_timestamp' => time(),
  79.             'oauth_nonce' => NonceGenerator::generate(),
  80.             'oauth_version' => '1.0',
  81.             'oauth_signature_method' => $this->options['signature_method'],
  82.             'oauth_token' => $requestToken['oauth_token'],
  83.             'oauth_verifier' => $request->query->get('oauth_verifier'),
  84.         ], $extraParameters);
  85.         $url $this->options['access_token_url'];
  86.         $parameters['oauth_signature'] = OAuthUtils::signRequest(
  87.             'POST',
  88.             $url,
  89.             $parameters,
  90.             $this->options['client_secret'],
  91.             $requestToken['oauth_token_secret'],
  92.             $this->options['signature_method']
  93.         );
  94.         $response $this->doGetTokenRequest($url$parameters);
  95.         $response $this->getResponseContent($response);
  96.         if (isset($response['oauth_problem'])) {
  97.             throw new AuthenticationException(sprintf('OAuth error: "%s"'$response['oauth_problem']));
  98.         }
  99.         if (!isset($response['oauth_token'], $response['oauth_token_secret'])) {
  100.             throw new AuthenticationException('Not a valid request token.');
  101.         }
  102.         return $response;
  103.     }
  104.     /**
  105.      * {@inheritdoc}
  106.      */
  107.     public function handles(HttpRequest $request)
  108.     {
  109.         return $request->query->has('oauth_token');
  110.     }
  111.     /**
  112.      * {@inheritdoc}
  113.      */
  114.     public function isCsrfTokenValid($csrfToken)
  115.     {
  116.         // OAuth1.0a passes token with every call
  117.         return true;
  118.     }
  119.     /**
  120.      * {@inheritdoc}
  121.      */
  122.     public function getRequestToken($redirectUri, array $extraParameters = [])
  123.     {
  124.         $timestamp time();
  125.         $parameters array_merge([
  126.             'oauth_consumer_key' => $this->options['client_id'],
  127.             'oauth_timestamp' => $timestamp,
  128.             'oauth_nonce' => NonceGenerator::generate(),
  129.             'oauth_version' => '1.0',
  130.             'oauth_callback' => $redirectUri,
  131.             'oauth_signature_method' => $this->options['signature_method'],
  132.         ], $extraParameters);
  133.         $url $this->options['request_token_url'];
  134.         $parameters['oauth_signature'] = OAuthUtils::signRequest(
  135.             'POST',
  136.             $url,
  137.             $parameters,
  138.             $this->options['client_secret'],
  139.             '',
  140.             $this->options['signature_method']
  141.         );
  142.         $apiResponse $this->httpRequest($urlnull, [], 'POST'$parameters);
  143.         $response $this->getResponseContent($apiResponse);
  144.         if (isset($response['oauth_problem'])) {
  145.             throw new AuthenticationException(sprintf('OAuth error: "%s"'$response['oauth_problem']));
  146.         }
  147.         if (isset($response['oauth_callback_confirmed']) && 'true' !== $response['oauth_callback_confirmed']) {
  148.             throw new AuthenticationException('Defined OAuth callback was not confirmed.');
  149.         }
  150.         if (!isset($response['oauth_token'], $response['oauth_token_secret'])) {
  151.             throw new AuthenticationException('Not a valid request token.');
  152.         }
  153.         $response['timestamp'] = $timestamp;
  154.         $this->storage->save($this$response);
  155.         return $response;
  156.     }
  157.     /**
  158.      * {@inheritdoc}
  159.      */
  160.     protected function doGetTokenRequest($url, array $parameters = [])
  161.     {
  162.         return $this->httpRequest($urlnull, [], 'POST'$parameters);
  163.     }
  164.     /**
  165.      * {@inheritdoc}
  166.      */
  167.     protected function doGetUserInformationRequest($url, array $parameters = [])
  168.     {
  169.         return $this->httpRequest($urlnull, [], null$parameters);
  170.     }
  171.     /**
  172.      * {@inheritdoc}
  173.      */
  174.     protected function httpRequest($url$content null, array $headers = [], $method null, array $parameters = [])
  175.     {
  176.         foreach ($parameters as $key => $value) {
  177.             $parameters[$key] = $key.'="'.rawurlencode($value ?: '').'"';
  178.         }
  179.         if (!$this->options['realm']) {
  180.             array_unshift($parameters'realm="'.rawurlencode($this->options['realm'] ?: '').'"');
  181.         }
  182.         $headers['Authorization'] = 'OAuth '.implode(', '$parameters);
  183.         return parent::httpRequest($url$content$headers$method);
  184.     }
  185.     /**
  186.      * {@inheritdoc}
  187.      */
  188.     protected function configureOptions(OptionsResolver $resolver)
  189.     {
  190.         parent::configureOptions($resolver);
  191.         $resolver->setRequired([
  192.             'request_token_url',
  193.         ]);
  194.         $resolver->setDefaults([
  195.             'realm' => null,
  196.             'signature_method' => 'HMAC-SHA1',
  197.         ]);
  198.         $resolver->setAllowedValues('signature_method', ['HMAC-SHA1''RSA-SHA1''PLAINTEXT']);
  199.     }
  200. }