-
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
514f0e5
commit 36238c6
Showing
8 changed files
with
210 additions
and
163 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?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 Exception; | ||
use GraphQL\Language\AST\ArgumentNode; | ||
use GraphQL\Language\AST\NodeKind; | ||
use GraphQL\Language\Parser; | ||
use GraphQL\Language\Visitor; | ||
use Ibexa\Contracts\Core\Repository\PermissionResolver; | ||
use Ibexa\Core\MVC\Symfony\Security\User\APIUserProviderInterface; | ||
use Ibexa\Core\MVC\Symfony\Security\UserInterface as IbexaUser; | ||
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator; | ||
use Symfony\Component\Security\Http\Authenticator\InteractiveAuthenticatorInterface; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Passport; | ||
|
||
final class JWTAuthenticator extends AbstractAuthenticator implements InteractiveAuthenticatorInterface | ||
{ | ||
private string $username; | ||
|
||
private string $password; | ||
|
||
public function __construct( | ||
private readonly JWTTokenManagerInterface $tokenManager, | ||
private readonly APIUserProviderInterface $userProvider, | ||
private readonly PermissionResolver $permissionResolver, | ||
) { | ||
} | ||
|
||
public function supports(Request $request): ?bool | ||
{ | ||
$payload = json_decode($request->getContent(), true); | ||
if (!isset($payload['query'])) { | ||
return false; | ||
} | ||
|
||
try { | ||
$credentials = $this->extractCredentials($payload['query']); | ||
} catch (Exception) { | ||
return false; | ||
} | ||
|
||
if (isset($credentials['username'], $credentials['password'])) { | ||
$this->username = $credentials['username']; | ||
$this->password = $credentials['password']; | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function authenticate(Request $request): Passport | ||
{ | ||
$passport = new Passport( | ||
new UserBadge($this->username, [$this->userProvider, 'loadUserByUsername']), | ||
new PasswordCredentials($this->password) | ||
); | ||
|
||
$user = $passport->getUser(); | ||
if ($user instanceof IbexaUser) { | ||
$this->permissionResolver->setCurrentUserReference($user->getAPIUser()); | ||
} | ||
|
||
$passport->setAttribute('token', $this->tokenManager->create($user)); | ||
|
||
return $passport; | ||
} | ||
|
||
/** | ||
* @throws \JsonException | ||
*/ | ||
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response | ||
{ | ||
return new Response( | ||
json_encode( | ||
[ | ||
'token' => $this->tokenManager->create($token->getUser()), | ||
'message' => null, | ||
], | ||
JSON_THROW_ON_ERROR | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @throws \JsonException | ||
*/ | ||
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response | ||
{ | ||
return new Response( | ||
json_encode( | ||
[ | ||
'token' => null, | ||
'message' => $exception->getMessageKey(), | ||
], | ||
JSON_THROW_ON_ERROR | ||
), | ||
Response::HTTP_FORBIDDEN | ||
); | ||
} | ||
|
||
public function isInteractive(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
* | ||
* @throws \Exception | ||
*/ | ||
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; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/lib/Security/JWTTokenMutationFormatEventSubscriber.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,50 @@ | ||
<?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 Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Security\Http\Event\LoginFailureEvent; | ||
use Symfony\Component\Security\Http\Event\LoginSuccessEvent; | ||
|
||
/** | ||
* Needed only to properly adjust authorization (either successful or failed) response to meet former JWT Token Mutation format. | ||
*/ | ||
final readonly class JWTTokenMutationFormatEventSubscriber implements EventSubscriberInterface | ||
{ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
LoginSuccessEvent::class => ['onAuthorizationFinishes', 10], | ||
LoginFailureEvent::class => ['onAuthorizationFinishes', 10], | ||
]; | ||
} | ||
|
||
/** | ||
* @throws \JsonException | ||
*/ | ||
public function onAuthorizationFinishes(LoginSuccessEvent|LoginFailureEvent $event): void | ||
{ | ||
$response = $event->getResponse(); | ||
$response->setContent( | ||
$this->formatMutationResponseData($response->getContent()) | ||
); | ||
} | ||
|
||
/** | ||
* @throws \JsonException | ||
*/ | ||
private function formatMutationResponseData(mixed $data): string | ||
{ | ||
return json_encode([ | ||
'data' => [ | ||
'CreateToken' => json_decode($data, true, 512, JSON_THROW_ON_ERROR), | ||
], | ||
]); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.