-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IBX-8356: Reworked Ibexa\Core\MVC\Symfony\Security\Authentication\Aut…
…henticatorInterface usages to comply with Symfony-based authentication
- Loading branch information
1 parent
4fb3e4a
commit 1205c27
Showing
7 changed files
with
177 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/lib/Security/EventSubscriber/JsonLoginPayloadSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\GraphQL\Security\EventSubscriber; | ||
|
||
use Exception; | ||
use GraphQL\Language\AST\ArgumentNode; | ||
use GraphQL\Language\AST\NodeKind; | ||
use GraphQL\Language\Parser; | ||
use GraphQL\Language\Visitor; | ||
use Ibexa\GraphQL\Security\NonAdminGraphqlRequestSpecification; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
|
||
final readonly class JsonLoginPayloadSubscriber implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @param string[][] $siteAccessGroups | ||
*/ | ||
public function __construct( | ||
private URLGeneratorInterface $urlGenerator, | ||
private array $siteAccessGroups, | ||
) { | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
RequestEvent::class => ['mapToJsonLoginPayload', 10], | ||
]; | ||
} | ||
|
||
/** | ||
* @throws \JsonException | ||
* @throws \Ibexa\AdminUi\Exception\InvalidArgumentException | ||
*/ | ||
public function mapToJsonLoginPayload(RequestEvent $event): void | ||
{ | ||
$request = $event->getRequest(); | ||
if (!$this->isNonAdminGraphqlRequest($request)) { | ||
return; | ||
} | ||
|
||
$payload = json_decode($request->getContent(), true); | ||
if (!isset($payload['query'])) { | ||
return; | ||
} | ||
|
||
$redirectResponse = new RedirectResponse( | ||
$this->urlGenerator->generate('overblog_graphql_endpoint') | ||
); | ||
|
||
$credentials = []; | ||
try { | ||
$credentials = $this->extractCredentials($payload['query']); | ||
} catch (Exception) { | ||
//do nothing, empty credentials are sent further | ||
} | ||
|
||
$request->initialize( | ||
$request->query->all(), | ||
$request->request->all(), | ||
$request->attributes->all(), | ||
$request->cookies->all(), | ||
$request->files->all(), | ||
$request->server->all(), | ||
json_encode($credentials, JSON_THROW_ON_ERROR), | ||
); | ||
} | ||
|
||
/** | ||
* @throws \Ibexa\AdminUi\Exception\InvalidArgumentException | ||
*/ | ||
private function isNonAdminGraphqlRequest(Request $request): bool | ||
{ | ||
return (new NonAdminGraphqlRequestSpecification($this->siteAccessGroups))->isSatisfiedBy($request); | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
* @throws \GraphQL\Error\SyntaxError | ||
* | ||
* @return array<string, string> | ||
*/ | ||
private function extractCredentials(string $graphqlQuery): array | ||
{ | ||
$parsed = Parser::parse($graphqlQuery); | ||
$credentials = []; | ||
|
||
Visitor::visit( | ||
$parsed, | ||
[ | ||
NodeKind::ARGUMENT => static function (ArgumentNode $node) use (&$credentials): void { | ||
$credentials[$node->name->value] = (string)$node->value->value; | ||
}, | ||
] | ||
); | ||
|
||
return $credentials; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\GraphQL\Security; | ||
|
||
use Ibexa\AdminUi\Specification\SiteAccess\IsAdmin; | ||
use Ibexa\Contracts\Core\Specification\AbstractSpecification; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
final class NonAdminGraphqlRequestSpecification extends AbstractSpecification | ||
{ | ||
/** | ||
* @param string[][] $siteAccessGroups | ||
*/ | ||
public function __construct( | ||
private array $siteAccessGroups | ||
) { | ||
} | ||
|
||
/** | ||
* @throws \Ibexa\AdminUi\Exception\InvalidArgumentException | ||
*/ | ||
public function isSatisfiedBy($item): bool | ||
{ | ||
if (!$item instanceof Request) { | ||
return false; | ||
} | ||
|
||
return | ||
$item->attributes->get('_route') === 'overblog_graphql_endpoint' && | ||
!$this->isAdminSiteAccess($item); | ||
} | ||
|
||
/** | ||
* @throws \Ibexa\AdminUi\Exception\InvalidArgumentException | ||
*/ | ||
private function isAdminSiteAccess(Request $request): bool | ||
{ | ||
return (new IsAdmin($this->siteAccessGroups))->isSatisfiedBy( | ||
$request->attributes->get('siteaccess') | ||
); | ||
} | ||
} |