src/Platform/SecurityBundle/Service/OAuth/OAuthServiceFactory.php line 64

Open in your IDE?
  1. <?php
  2. namespace Platform\SecurityBundle\Service\OAuth;
  3. use Cms\CoreBundle\Service\ContextManager;
  4. use OAuth\Common\Consumer\Credentials;
  5. use OAuth\Common\Http\Client\CurlClient;
  6. use OAuth\Common\Service\ServiceInterface;
  7. use OAuth\Common\Storage\SymfonySession;
  8. use OAuth\ServiceFactory;
  9. use Platform\SecurityBundle\Model\OAuth\OAuthOptions;
  10. use Platform\SecurityBundle\Service\OAuth\Providers\AbstractOAuthProvider;
  11. use Platform\SecurityBundle\Service\OAuth\Providers as OAuthProviders;
  12. use Platform\SecurityBundle\Util\OAuth\ServiceFactory as ServiceFactories;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use OAuth\Common\Exception\Exception as OAuthException;
  15. /**
  16.  * Manages the OAuth core services that pertain to logins.
  17.  *
  18.  * Class OAuthServiceFactory
  19.  * @package Platform\SecurityBundle\Service\OAuth
  20.  */
  21. final class OAuthServiceFactory extends ServiceFactory
  22. {
  23.     /**
  24.      * Defines the set of services that this system will support.
  25.      * Keys are the IDs of the OAuth providers in the system.
  26.      * Values are the classnames of the service factory tied to that provider.
  27.      */
  28.     private const SERVICES = [
  29.         OAuthProviders\ClassLinkOAuthProvider::ID => ServiceFactories\ClassLinkOAuth2ServiceFactory::class,
  30.         OAuthProviders\CleverOAuthProvider::ID => ServiceFactories\CleverOAuth2ServiceFactory::class,
  31.         OAuthProviders\GlobalGridOAuthProvider::ID => ServiceFactories\GlobalGridOAuth2ServiceFactory::class,
  32.         OAuthProviders\GoogleOAuthProvider::ID => ServiceFactories\GoogleOAuth2ServiceFactory::class,
  33.         OAuthProviders\MicrosoftOAuthProvider::ID => ServiceFactories\MicrosoftOAuth2ServiceFactory::class,
  34.     ];
  35.     /**
  36.      * @var RequestStack
  37.      */
  38.     private RequestStack $requestStack;
  39.     /**
  40.      * @var ContextManager
  41.      */
  42.     private ContextManager $cm;
  43.     /**
  44.      * @param RequestStack $requestStack
  45.      * @param ContextManager $cm
  46.      * @throws OAuthException
  47.      */
  48.     public function __construct(RequestStack $requestStackContextManager $cm)
  49.     {
  50.         $this->requestStack $requestStack;
  51.         $this->cm $cm;
  52.         // we need to set up the client that handles the http api calls
  53.         $this->setHttpClient(new CurlClient());
  54.         // go ahead and register each service
  55.         foreach (self::SERVICES as $name => $class) {
  56.             $this->registerService($name$class);
  57.         }
  58.     }
  59.     /**
  60.      * Gets the core OAuth service.
  61.      * Services are loosely tied to the providers, so a provider is required to be passed in order to obtain one.
  62.      * An optional callback can be set for when configuring the "credentials" for the service.
  63.      * This is useful for when handling things like Clever "Instant Login" requests as the OAuth callback differs.
  64.      *
  65.      * @param AbstractOAuthProvider $provider
  66.      * @param OAuthOptions $options
  67.      * @return ServiceInterface
  68.      */
  69.     public function getService(AbstractOAuthProvider $providerOAuthOptions $options): ServiceInterface
  70.     {
  71.         return $this->createService(
  72.             $provider->getId(),
  73.             new Credentials(
  74.                 $provider->getClient(),
  75.                 $provider->getSecret(),
  76.                 $options->getCallback() ?? $this->getDefaultCallbackUrl()
  77.             ),
  78.             new SymfonySession($this->requestStack->getSession()),
  79.             array_merge($options->getScopes(), $provider->getScopes())
  80.         );
  81.     }
  82.     /**
  83.      * Gets a default value to use for the OAuth callbacks.
  84.      *
  85.      * TODO: this should use the router to properly generate the url
  86.      *
  87.      * @return string
  88.      */
  89.     private function getDefaultCallbackUrl(): string
  90.     {
  91.         return sprintf(
  92.             'https://%s/_oauth',
  93.             $this->cm->getGlobalContext()->getDashboard(true)
  94.         );
  95.     }
  96. }