src/Platform/SecurityBundle/Controller/SingleSignOnController.php line 38

Open in your IDE?
  1. <?php
  2. namespace Platform\SecurityBundle\Controller;
  3. use Cms\CoreBundle\Util\Controller;
  4. use Platform\SecurityBundle\Model\OAuth\OAuthOptions;
  5. use Platform\SecurityBundle\Service\Login\LoginSystem;
  6. use Platform\SecurityBundle\Service\OAuth\OAuthProviderService;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  12. /**
  13.  * Class SingleSignOnController
  14.  * @package Platform\SecurityBundle\Controller
  15.  *
  16.  * @Route(
  17.  *     "/oauth"
  18.  * )
  19.  */
  20. final class SingleSignOnController extends Controller
  21. {
  22.     const ROUTES__START 'app.platform.security.sso.start';
  23.     const ROUTES__FINISH 'app.platform.security.sso.finish';
  24.     /**
  25.      * @param Request $request
  26.      * @param string $id
  27.      * @return RedirectResponse
  28.      *
  29.      * @Route(
  30.      *     "/start/{id}",
  31.      *     name = SingleSignOnController::ROUTES__START
  32.      * )
  33.      */
  34.     public function startAction(Request $requeststring $id): RedirectResponse
  35.     {
  36.         // load up the applicable provider and ensure it is enabled
  37.         $provider $this->getOAuthProviderService()->getProvider($id);
  38.         if ( ! $provider->isEnabled($this->getGlobalContext()->getTenant())) {
  39.             throw new NotFoundHttpException();
  40.         }
  41.         // do the redirect
  42.         return new RedirectResponse($provider->getAuthorizationUri(
  43.             $this->getGlobalContext()->getTenant(),
  44.             new OAuthOptions([
  45.                 'state' => [
  46.                     'redirect' => $this->generateUrl(
  47.                         self::ROUTES__FINISH,
  48.                         [
  49.                             'provider' => $provider->getId(),
  50.                         ],
  51.                         UrlGeneratorInterface::ABSOLUTE_URL,
  52.                     ),
  53.                     'data' => [
  54.                         'redirect' => $request->query->get('redirect'null),
  55.                     ],
  56.                 ],
  57.             ])
  58.         ));
  59.     }
  60.     /**
  61.      * @param Request $request
  62.      * @return RedirectResponse
  63.      *
  64.      * @Route(
  65.      *     "/finish",
  66.      *     name = SingleSignOnController::ROUTES__FINISH
  67.      * )
  68.      */
  69.     public function finishAction(Request $request)
  70.     {
  71.         // NOOP
  72.     }
  73.     /**
  74.      * @return OAuthProviderService|object
  75.      */
  76.     private function getOAuthProviderService(): OAuthProviderService
  77.     {
  78.         return $this->get(__METHOD__);
  79.     }
  80.     /**
  81.      * @return LoginSystem|object
  82.      */
  83.     private function getLoginSystem(): LoginSystem
  84.     {
  85.         return $this->get(__METHOD__);
  86.     }
  87. }