Initial commit: CloudOps infrastructure platform
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\ApiBundle\Controller\oAuth2;
|
||||
|
||||
use FOS\OAuthServerBundle\Form\Handler\AuthorizeFormHandler;
|
||||
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
|
||||
use OAuth2\OAuth2;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Form\Form;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
||||
use Twig\Environment;
|
||||
use Twig\Error\LoaderError;
|
||||
use Twig\Error\RuntimeError;
|
||||
use Twig\Error\SyntaxError;
|
||||
|
||||
class AuthorizeController extends \FOS\OAuthServerBundle\Controller\AuthorizeController
|
||||
{
|
||||
private TokenStorageInterface $tokenStorage;
|
||||
|
||||
/**
|
||||
* This constructor must be duplicated from the extended class so our custom code could access the properties.
|
||||
*/
|
||||
public function __construct(
|
||||
RequestStack $requestStack,
|
||||
Form $authorizeForm,
|
||||
OAuth2 $oAuth2Server,
|
||||
TokenStorageInterface $tokenStorage,
|
||||
UrlGeneratorInterface $router,
|
||||
ClientManagerInterface $clientManager,
|
||||
EventDispatcherInterface $eventDispatcher,
|
||||
) {
|
||||
parent::__construct(
|
||||
$requestStack,
|
||||
$authorizeForm,
|
||||
$oAuth2Server,
|
||||
$tokenStorage,
|
||||
$router,
|
||||
$clientManager,
|
||||
$eventDispatcher
|
||||
);
|
||||
|
||||
$this->tokenStorage = $tokenStorage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string , mixed> $data Various data to be passed to the twig template
|
||||
*
|
||||
* @throws LoaderError
|
||||
* @throws RuntimeError
|
||||
* @throws SyntaxError
|
||||
*/
|
||||
protected function renderAuthorize(array $data, Environment $twig): Response
|
||||
{
|
||||
$response = $twig->render(
|
||||
'@MauticApi/Authorize/oAuth2/authorize.html.twig',
|
||||
$data
|
||||
);
|
||||
|
||||
return new Response($response);
|
||||
}
|
||||
|
||||
public function authorizeAction(Request $request, AuthorizeFormHandler $formHandler, Environment $twig): Response
|
||||
{
|
||||
// The parent bundle does not care about token being empty.
|
||||
if (null === $this->tokenStorage->getToken()) {
|
||||
throw new AccessDeniedException('This user does not have access to this section. No token.');
|
||||
}
|
||||
|
||||
return parent::authorizeAction($request, $formHandler, $twig);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\ApiBundle\Controller\oAuth2;
|
||||
|
||||
use Mautic\CoreBundle\Controller\CommonController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Security\Core\Exception;
|
||||
use Symfony\Component\Security\Http\SecurityRequestAttributes;
|
||||
|
||||
class SecurityController extends CommonController
|
||||
{
|
||||
public function loginAction(Request $request): Response
|
||||
{
|
||||
$session = $request->getSession();
|
||||
|
||||
// get the login error if there is one
|
||||
if ($request->attributes->has(SecurityRequestAttributes::AUTHENTICATION_ERROR)) {
|
||||
$error = $request->attributes->get(SecurityRequestAttributes::AUTHENTICATION_ERROR);
|
||||
} else {
|
||||
$error = $session->get(SecurityRequestAttributes::AUTHENTICATION_ERROR);
|
||||
$session->remove(SecurityRequestAttributes::AUTHENTICATION_ERROR);
|
||||
}
|
||||
if (!empty($error)) {
|
||||
if ($error instanceof Exception\BadCredentialsException) {
|
||||
$msg = 'mautic.user.auth.error.invalidlogin';
|
||||
} else {
|
||||
$msg = $error->getMessage();
|
||||
}
|
||||
$this->addFlashMessage($msg, [], 'error', null, false);
|
||||
}
|
||||
|
||||
if ($session->has('_security.target_path')) {
|
||||
if (str_contains($session->get('_security.target_path'), $this->generateUrl('fos_oauth_server_authorize'))) {
|
||||
$session->set('_fos_oauth_server.ensure_logout', true);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render(
|
||||
'@MauticApi/Security/login.html.twig',
|
||||
[
|
||||
'last_username' => $session->get(SecurityRequestAttributes::LAST_USERNAME),
|
||||
'route' => 'mautic_oauth2_server_auth_login_check',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function loginCheckAction(): Response
|
||||
{
|
||||
return new Response('', 400);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user