vendor/hwi/oauth-bundle/OAuth/RequestDataStorage/SessionStorage.php line 73

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\RequestDataStorage;
  11. use HWI\Bundle\OAuthBundle\OAuth\RequestDataStorageInterface;
  12. use HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. /**
  16.  * Request token storage implementation using the Symfony session.
  17.  *
  18.  * @author Alexander <iam.asm89@gmail.com>
  19.  * @author Francisco Facioni <fran6co@gmail.com>
  20.  * @author Joseph Bielawski <stloyd@gmail.com>
  21.  *
  22.  * @final since 1.4
  23.  */
  24. class SessionStorage implements RequestDataStorageInterface
  25. {
  26.     /**
  27.      * @var RequestStack
  28.      */
  29.     private $requestStack;
  30.     public function __construct(RequestStack $requestStack)
  31.     {
  32.         $this->requestStack $requestStack;
  33.     }
  34.     /**
  35.      * {@inheritdoc}
  36.      */
  37.     public function fetch(ResourceOwnerInterface $resourceOwner$key$type 'token')
  38.     {
  39.         $key $this->generateKey($resourceOwner$key$type);
  40.         if (null === $data $this->getSession()->get($key)) {
  41.             throw new \InvalidArgumentException('No data available in storage.');
  42.         }
  43.         // Request tokens are one time use only
  44.         if (\in_array($type, ['token''csrf_state'], true)) {
  45.             $this->getSession()->remove($key);
  46.         }
  47.         return $data;
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      */
  52.     public function save(ResourceOwnerInterface $resourceOwner$value$type 'token')
  53.     {
  54.         if ('token' === $type) {
  55.             if (!\is_array($value) || !isset($value['oauth_token'])) {
  56.                 throw new \InvalidArgumentException('Invalid request token.');
  57.             }
  58.             $key $this->generateKey($resourceOwner$value['oauth_token'], 'token');
  59.         } else {
  60.             $key $this->generateKey($resourceOwner$this->getStorageKey($value), $type);
  61.         }
  62.         $this->getSession()->set($key$this->getStorageValue($value));
  63.     }
  64.     /**
  65.      * Key to for fetching or saving a token.
  66.      *
  67.      * @param string $key
  68.      * @param string $type
  69.      *
  70.      * @return string
  71.      */
  72.     protected function generateKey(ResourceOwnerInterface $resourceOwner$key$type)
  73.     {
  74.         return sprintf('_hwi_oauth.%s.%s.%s.%s'$resourceOwner->getName(), $resourceOwner->getOption('client_id'), $type$key);
  75.     }
  76.     /**
  77.      * @param array|string|object $value
  78.      *
  79.      * @return array|string
  80.      */
  81.     private function getStorageValue($value)
  82.     {
  83.         if (\is_object($value)) {
  84.             $value serialize($value);
  85.         }
  86.         return $value;
  87.     }
  88.     /**
  89.      * @param array|string|object $value
  90.      */
  91.     private function getStorageKey($value): string
  92.     {
  93.         if (\is_array($value)) {
  94.             $storageKey reset($value);
  95.         } elseif (\is_object($value)) {
  96.             $storageKey \get_class($value);
  97.         } else {
  98.             $storageKey $value;
  99.         }
  100.         return (string) $storageKey;
  101.     }
  102.     private function getSession(): SessionInterface
  103.     {
  104.         if (method_exists($this->requestStack'getSession')) {
  105.             return $this->requestStack->getSession();
  106.         }
  107.         if ((null !== $request $this->requestStack->getCurrentRequest()) && $request->hasSession()) {
  108.             return $request->getSession();
  109.         }
  110.         throw new \LogicException('There is currently no session available.');
  111.     }
  112. }