<?php
namespace Platform\SecurityBundle\Controller;
use Cms\CoreBundle\Util\Controller;
use Platform\SecurityBundle\Model\OAuth\OAuthOptions;
use Platform\SecurityBundle\Service\Login\LoginSystem;
use Platform\SecurityBundle\Service\OAuth\OAuthProviderService;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Class SingleSignOnController
* @package Platform\SecurityBundle\Controller
*
* @Route(
* "/oauth"
* )
*/
final class SingleSignOnController extends Controller
{
const ROUTES__START = 'app.platform.security.sso.start';
const ROUTES__FINISH = 'app.platform.security.sso.finish';
/**
* @param Request $request
* @param string $id
* @return RedirectResponse
*
* @Route(
* "/start/{id}",
* name = SingleSignOnController::ROUTES__START
* )
*/
public function startAction(Request $request, string $id): RedirectResponse
{
// load up the applicable provider and ensure it is enabled
$provider = $this->getOAuthProviderService()->getProvider($id);
if ( ! $provider->isEnabled($this->getGlobalContext()->getTenant())) {
throw new NotFoundHttpException();
}
// do the redirect
return new RedirectResponse($provider->getAuthorizationUri(
$this->getGlobalContext()->getTenant(),
new OAuthOptions([
'state' => [
'redirect' => $this->generateUrl(
self::ROUTES__FINISH,
[
'provider' => $provider->getId(),
],
UrlGeneratorInterface::ABSOLUTE_URL,
),
'data' => [
'redirect' => $request->query->get('redirect', null),
],
],
])
));
}
/**
* @param Request $request
* @return RedirectResponse
*
* @Route(
* "/finish",
* name = SingleSignOnController::ROUTES__FINISH
* )
*/
public function finishAction(Request $request)
{
// NOOP
}
/**
* @return OAuthProviderService|object
*/
private function getOAuthProviderService(): OAuthProviderService
{
return $this->get(__METHOD__);
}
/**
* @return LoginSystem|object
*/
private function getLoginSystem(): LoginSystem
{
return $this->get(__METHOD__);
}
}