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

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 class SessionStorage implements RequestDataStorageInterface
  23. {
  24.     private RequestStack $requestStack;
  25.     public function __construct(RequestStack $requestStack)
  26.     {
  27.         $this->requestStack $requestStack;
  28.     }
  29.     /**
  30.      * {@inheritdoc}
  31.      */
  32.     public function fetch(ResourceOwnerInterface $resourceOwner$key$type 'token')
  33.     {
  34.         $key $this->generateKey($resourceOwner$key$type);
  35.         if (null === $data $this->getSession()->get($key)) {
  36.             throw new \InvalidArgumentException('No data available in storage.');
  37.         }
  38.         // Request tokens are one time use only
  39.         if (\in_array($type, ['token''csrf_state'], true)) {
  40.             $this->getSession()->remove($key);
  41.         }
  42.         return $data;
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function save(ResourceOwnerInterface $resourceOwner$value$type 'token')
  48.     {
  49.         if ('token' === $type) {
  50.             if (!\is_array($value) || !isset($value['oauth_token'])) {
  51.                 throw new \InvalidArgumentException('Invalid request token.');
  52.             }
  53.             $key $this->generateKey($resourceOwner$value['oauth_token'], 'token');
  54.         } else {
  55.             $key $this->generateKey($resourceOwner$this->getStorageKey($value), $type);
  56.         }
  57.         $this->getSession()->set($key$this->getStorageValue($value));
  58.     }
  59.     /**
  60.      * Key to for fetching or saving a token.
  61.      */
  62.     private function generateKey(ResourceOwnerInterface $resourceOwnerstring $keystring $type): string
  63.     {
  64.         return sprintf('_hwi_oauth.%s.%s.%s.%s'$resourceOwner->getName(), $resourceOwner->getOption('client_id'), $type$key);
  65.     }
  66.     /**
  67.      * @param array|string|object $value
  68.      *
  69.      * @return array|string
  70.      */
  71.     private function getStorageValue($value)
  72.     {
  73.         if (\is_object($value)) {
  74.             $value serialize($value);
  75.         }
  76.         return $value;
  77.     }
  78.     /**
  79.      * @param array|string|object $value
  80.      */
  81.     private function getStorageKey($value): string
  82.     {
  83.         if (\is_array($value)) {
  84.             $storageKey reset($value);
  85.         } elseif (\is_object($value)) {
  86.             $storageKey $value::class;
  87.         } else {
  88.             $storageKey $value;
  89.         }
  90.         return (string) $storageKey;
  91.     }
  92.     private function getSession(): SessionInterface
  93.     {
  94.         if (method_exists($this->requestStack'getSession')) {
  95.             return $this->requestStack->getSession();
  96.         }
  97.         if ((null !== $request $this->requestStack->getCurrentRequest()) && $request->hasSession()) {
  98.             return $request->getSession();
  99.         }
  100.         throw new \LogicException('There is currently no session available.');
  101.     }
  102. }